Skip to content

Commit bd404e0

Browse files
authored
Litellm integration (#524)
litellm is a library that abstracts away details/differences for a lot of model providers. Adding an extension, so that any provider can easily be integrated. --- [//]: # (BEGIN SAPLING FOOTER) * #532 * __->__ #524
1 parent 0faadf7 commit bd404e0

File tree

10 files changed

+1701
-381
lines changed

10 files changed

+1701
-381
lines changed

Diff for: examples/model_providers/litellm_provider.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from __future__ import annotations
2+
3+
import asyncio
4+
5+
from agents import Agent, Runner, function_tool, set_tracing_disabled
6+
from agents.extensions.models.litellm_model import LitellmModel
7+
8+
"""This example uses the LitellmModel directly, to hit any model provider.
9+
You can run it like this:
10+
uv run examples/model_providers/litellm_provider.py --model anthropic/claude-3-5-sonnet-20240620
11+
or
12+
uv run examples/model_providers/litellm_provider.py --model gemini/gemini-2.0-flash
13+
14+
Find more providers here: https://docs.litellm.ai/docs/providers
15+
"""
16+
17+
set_tracing_disabled(disabled=True)
18+
19+
20+
@function_tool
21+
def get_weather(city: str):
22+
print(f"[debug] getting weather for {city}")
23+
return f"The weather in {city} is sunny."
24+
25+
26+
async def main(model: str, api_key: str):
27+
agent = Agent(
28+
name="Assistant",
29+
instructions="You only respond in haikus.",
30+
model=LitellmModel(model=model, api_key=api_key),
31+
tools=[get_weather],
32+
)
33+
34+
result = await Runner.run(agent, "What's the weather in Tokyo?")
35+
print(result.final_output)
36+
37+
38+
if __name__ == "__main__":
39+
# First try to get model/api key from args
40+
import argparse
41+
42+
parser = argparse.ArgumentParser()
43+
parser.add_argument("--model", type=str, required=False)
44+
parser.add_argument("--api-key", type=str, required=False)
45+
args = parser.parse_args()
46+
47+
model = args.model
48+
if not model:
49+
model = input("Enter a model name for Litellm: ")
50+
51+
api_key = args.api_key
52+
if not api_key:
53+
api_key = input("Enter an API key for Litellm: ")
54+
55+
asyncio.run(main(model, api_key))

Diff for: pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Repository = "https://github.com/openai/openai-agents-python"
3636
[project.optional-dependencies]
3737
voice = ["numpy>=2.2.0, <3; python_version>='3.10'", "websockets>=15.0, <16"]
3838
viz = ["graphviz>=0.17"]
39+
litellm = ["litellm>=1.65.0, <2"]
3940

4041
[dependency-groups]
4142
dev = [
@@ -44,7 +45,7 @@ dev = [
4445
"pytest",
4546
"pytest-asyncio",
4647
"pytest-mock>=3.14.0",
47-
"rich",
48+
"rich>=13.1.0, <14",
4849
"mkdocs>=1.6.0",
4950
"mkdocs-material>=9.6.0",
5051
"mkdocstrings[python]>=0.28.0",

Diff for: src/agents/extensions/models/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)