Skip to content

Commit

Permalink
Make OpenAI and Groq and OpenAILike into the new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkbrnd committed Feb 5, 2025
1 parent 110077d commit 4e16e76
Show file tree
Hide file tree
Showing 10 changed files with 618 additions and 706 deletions.
Empty file.
12 changes: 12 additions & 0 deletions cookbook/models/azure/openai/async/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import asyncio

from agno.agent import Agent
from agno.models.azure import AzureOpenAI

agent = Agent(
model=AzureOpenAI(id="gpt-4o"),
description="You help people with their health and fitness goals.",
instructions=["Recipes should be under 5 ingredients"],
)
# -*- Print a response to the cli
asyncio.run(agent.aprint_response("Share a breakfast recipe.", markdown=True))
14 changes: 14 additions & 0 deletions cookbook/models/azure/openai/async/basic_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import asyncio

from agno.agent import Agent
from agno.models.azure import AzureOpenAI

assistant = Agent(
model=AzureOpenAI(id="gpt-4o"),
description="You help people with their health and fitness goals.",
instructions=["Recipes should be under 5 ingredients"],
)
# -*- Print a response to the cli
asyncio.run(
assistant.aprint_response("Share a breakfast recipe.", markdown=True, stream=True)
)
9 changes: 8 additions & 1 deletion libs/agno/agno/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ def __init__(
# Agent session
self.agent_session: Optional[AgentSession] = None

self._tools_for_model: Optional[List[Dict]] = None
self._functions_for_model: Optional[Dict[str, Function]] = None

self._formatter: Optional[SafeFormatter] = None
Expand Down Expand Up @@ -1345,7 +1346,7 @@ def get_tools(self) -> Optional[List[Union[Toolkit, Callable, Function]]]:
def add_tools_to_model(self, model: Model) -> None:

# Skip if functions_for_model is not None
if self._functions_for_model is None:
if self._functions_for_model is None or self._tools_for_model is None:

# Get Agent tools
agent_tools = self.get_tools()
Expand All @@ -1356,6 +1357,7 @@ def add_tools_to_model(self, model: Model) -> None:
if self.response_model is not None and self.structured_outputs and model.supports_structured_outputs:
strict = True

self._tools_for_model = []
self._functions_for_model = {}

for tool in agent_tools:
Expand All @@ -1369,6 +1371,7 @@ def add_tools_to_model(self, model: Model) -> None:
if strict:
func.strict = True
self._functions_for_model[name] = func
self._tools_for_model.append({"type": "function", "function": func.to_dict()})
logger.debug(f"Included function {name} from {tool.name}")

elif isinstance(tool, Function):
Expand All @@ -1378,6 +1381,7 @@ def add_tools_to_model(self, model: Model) -> None:
if strict:
tool.strict = True
self._functions_for_model[tool.name] = tool
self._tools_for_model.append({"type": "function", "function": tool.to_dict()})
logger.debug(f"Included function {tool.name}")

elif callable(tool):
Expand All @@ -1389,10 +1393,13 @@ def add_tools_to_model(self, model: Model) -> None:
if strict:
func.strict = True
self._functions_for_model[func.name] = func
self._tools_for_model.append({"type": "function", "function": func.to_dict()})
logger.debug(f"Included function {func.name}")
except Exception as e:
logger.warning(f"Could not add function {tool}: {e}")

# Set tools on the model
model.set_tools(tools=self._tools_for_model)
# Set functions on the model
model.set_functions(functions=self._functions_for_model)

Expand Down
2 changes: 1 addition & 1 deletion libs/agno/agno/memory/db/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sqlalchemy.sql.expression import delete, select, text
from sqlalchemy.types import DateTime, String
except ImportError:
raise ImportError("`sqlalchemy` not installed")
raise ImportError("`sqlalchemy` not installed. Please install using `pip install sqlalchemy 'psycopg[binary]'`")

from agno.memory.db import MemoryDb
from agno.memory.row import MemoryRow
Expand Down
18 changes: 13 additions & 5 deletions libs/agno/agno/models/azure/openai_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class AzureOpenAI(OpenAILike):
azure_deployment: Optional[str] = getenv("AZURE_DEPLOYMENT")
azure_ad_token: Optional[str] = None
azure_ad_token_provider: Optional[Any] = None
openai_client: Optional[AzureOpenAIClient] = None

client: Optional[AzureOpenAIClient] = None
async_client: Optional[AsyncAzureOpenAIClient] = None

def get_client(self) -> AzureOpenAIClient:
"""
Expand All @@ -54,12 +56,14 @@ def get_client(self) -> AzureOpenAIClient:
AzureOpenAIClient: The OpenAI client.
"""
if self.openai_client:
return self.openai_client
if self.client is not None:
return self.client

_client_params: Dict[str, Any] = self._get_client_params()

return AzureOpenAIClient(**_client_params)
# -*- Create client
self.client = AzureOpenAIClient(**_client_params)
return self.client

def get_async_client(self) -> AsyncAzureOpenAIClient:
"""
Expand All @@ -68,6 +72,8 @@ def get_async_client(self) -> AsyncAzureOpenAIClient:
Returns:
AsyncAzureOpenAIClient: An instance of the asynchronous OpenAI client.
"""
if self.async_client:
return self.async_client

_client_params: Dict[str, Any] = self._get_client_params()

Expand All @@ -78,7 +84,9 @@ def get_async_client(self) -> AsyncAzureOpenAIClient:
_client_params["http_client"] = httpx.AsyncClient(
limits=httpx.Limits(max_connections=1000, max_keepalive_connections=100)
)
return AsyncAzureOpenAIClient(**_client_params)

self.async_client = AsyncAzureOpenAIClient(**_client_params)
return self.async_client

def _get_client_params(self) -> Dict[str, Any]:
_client_params: Dict[str, Any] = {}
Expand Down
Loading

0 comments on commit 4e16e76

Please sign in to comment.