Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LiteLLM support in line with AsyncOpenAI and with reproducible examples #318

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
26 changes: 26 additions & 0 deletions examples/model_providers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,29 @@ Loops within themselves,
Function calls its own being,
Depth without ending.
```


## LiteLLM Proxy Server integration

LiteLLM integration helps out switch between models easily and rapidly. This is easy to integrate via `AsyncOpenAI`:

```bash
# launch server proxy with your configuration
litellm --config examples/model_providers/litellm_config.yaml

# then use the proxy via the SDK
python3 examples/model_providers/litellm.py
```

### Testing the proxy server
Testing some basic models against proxy to verify it's operational:
```bash
# qwen2.5:14b
curl -s http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "qwen2.5:14b", "messages": [{"role": "user", "content": "Say hi"}], "max_tokens": 10}' | jq

# claude-3-7
curl -s http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "claude-3-7", "messages": [{"role": "user", "content": "Say hi"}], "max_tokens": 10}' | jq

# gpt-4o
curl -s http://localhost:4000/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "Say hi"}], "max_tokens": 10}' | jq
```
36 changes: 36 additions & 0 deletions examples/model_providers/litellm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from dotenv import load_dotenv
from openai import AsyncOpenAI
from agents import OpenAIChatCompletionsModel,Agent,Runner
from agents.model_settings import ModelSettings
from agents import set_default_openai_client, set_tracing_disabled

# Load environment variables from .env file
load_dotenv()

external_client = AsyncOpenAI(
base_url = os.getenv('LITELLM_BASE_URL', 'http://localhost:4000'),
api_key=os.getenv('LITELLM_API_KEY', 'key'))

set_default_openai_client(external_client)
set_tracing_disabled(True)

llm_model=os.getenv('LLM_MODEL', 'gpt-4o')
# llm_model=os.getenv('LLM_MODEL', 'claude-3-7')
# llm_model=os.getenv('LLM_MODEL', 'qwen2.5:14b')

# For Qwen models, we need to skip system instructions as they're not supported
instructions = None if "qwen" in llm_model.lower() else "You are a helpful assistant"

agent = Agent(
name="Assistant",
instructions=instructions,
model=OpenAIChatCompletionsModel(
model=llm_model,
openai_client=external_client,
)
)


result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)
23 changes: 23 additions & 0 deletions examples/model_providers/litellm_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
model_list:
- model_name: gpt-4o
litellm_params:
model: gpt-4o
api_key: "os.environ/OPENAI_API_KEY"
api_base: https://api.openai.com/v1
- model_name: claude-3-7
litellm_params:
model: claude-3-7-sonnet-20250219
api_key: "os.environ/ANTHROPIC_API_KEY" # does os.getenv("ANTHROPIC_API_KEY")
base_url: 'https://api.anthropic.com'
- model_name: qwen2.5:14b
litellm_params:
model: ollama/qwen2.5:14b
api_base: http://localhost:8000
api_key: "os.environ/OLLAMA"

litellm_settings: # module level litellm settings - https://github.com/BerriAI/litellm/blob/main/litellm/__init__.py
drop_params: True
# set_verbose: True

general_settings:
port: 4000
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ requires-python = ">=3.9"
license = "MIT"
authors = [{ name = "OpenAI", email = "[email protected]" }]
dependencies = [
"openai>=1.66.5",
"openai>=1.68.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should not be part of the PR, as it has nothing to do with the added example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my local testing, it was neccessary, otherwise it wouldn't work with 1.66.5.

Happy to change it though if that gets this through.

"pydantic>=2.10, <3",
"griffe>=1.5.6, <2",
"typing-extensions>=4.12.2, <5",
Expand Down