Skip to content

Commit

Permalink
Cache model clients (#1949)
Browse files Browse the repository at this point in the history
## Description

- Cache model clients

---

## Type of change

Please check the options that are relevant:

- [x] Model update (Addition or modification of models)

---

## Checklist

- [x] Adherence to standards: Code complies with Agno’s style guidelines
and best practices.
- [x] Formatting and validation: You have run `./scripts/format.sh` and
`./scripts/validate.sh` to ensure code is formatted and linted.
- [x] Self-review completed: A thorough review has been performed by the
contributor(s).
- [x] Documentation: Docstrings and comments have been added or updated
for any complex logic.
- [x] Examples and guides: Relevant cookbook examples have been included
or updated (if applicable).
- [x] Tested in a clean environment: Changes have been tested in a clean
environment to confirm expected behavior.
- [x] Tests (optional): Tests have been added or updated to cover any
new or changed functionality.

---------

Co-authored-by: Dirk Brand <[email protected]>
  • Loading branch information
ashpreetbedi and dirkbrnd authored Jan 31, 2025
1 parent 29885e1 commit 6653576
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 25 deletions.
8 changes: 7 additions & 1 deletion cookbook/models/anthropic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ export OPENAI_API_KEY=***
python cookbook/models/anthropic/knowledge.py
```

### 9. Run Agent that analyzes an image
### 9. Run Agent that uses memory

```shell
python cookbook/models/anthropic/memory.py
```

### 10. Run Agent that analyzes an image

```shell
python cookbook/models/anthropic/image_agent.py
Expand Down
44 changes: 44 additions & 0 deletions cookbook/models/anthropic/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `pip install anthropic sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/models/anthropic/memory.py` to run the agent
"""

from agno.agent import Agent, AgentMemory
from agno.memory.db.postgres import PgMemoryDb
from agno.models.anthropic import Claude
from agno.storage.agent.postgres import PostgresAgentStorage
from rich.pretty import pprint

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=Claude(id="claude-3-5-sonnet-20241022"),
# Store the memories and summary in a database
memory=AgentMemory(
db=PgMemoryDb(table_name="agent_memory", db_url=db_url),
create_user_memories=True,
create_session_summary=True,
),
# Store agent sessions in a database
storage=PostgresAgentStorage(
table_name="personalized_agent_sessions", db_url=db_url
),
# Show debug logs so, you can see the memory being created
# debug_mode=True,
)

# -*- Share personal information
agent.print_response("My name is john billings?", stream=True)

# -*- Share personal information
agent.print_response("I live in nyc?", stream=True)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow?", stream=True)

# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)
6 changes: 6 additions & 0 deletions cookbook/models/cohere/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ python cookbook/models/cohere/storage.py
```shell
python cookbook/models/cohere/knowledge.py
```

### 9. Run Agent that uses memory

```shell
python cookbook/models/cohere/memory.py
```
43 changes: 43 additions & 0 deletions cookbook/models/cohere/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `pip install cohere sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/models/cohere/memory.py` to run the agent
"""

from agno.agent import Agent, AgentMemory
from agno.memory.db.postgres import PgMemoryDb
from agno.models.cohere import Cohere
from agno.storage.agent.postgres import PostgresAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=Cohere(id="command-r-08-2024"),
# Store the memories and summary in a database
memory=AgentMemory(
db=PgMemoryDb(table_name="agent_memory", db_url=db_url),
create_user_memories=True,
create_session_summary=True,
),
# Store agent sessions in a database
storage=PostgresAgentStorage(
table_name="personalized_agent_sessions", db_url=db_url
),
# Show debug logs so, you can see the memory being created
# debug_mode=True,
)

# -*- Share personal information
agent.print_response("My name is john billings?", stream=True)

# -*- Share personal information
agent.print_response("I live in nyc?", stream=True)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow?", stream=True)

# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)
6 changes: 6 additions & 0 deletions cookbook/models/mistral/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ python cookbook/models/mistral/tool_use.py
```shell
python cookbook/models/mistral/structured_output.py
```

### 7. Run Agent that uses memory

```shell
python cookbook/models/mistral/memory.py
```
8 changes: 1 addition & 7 deletions cookbook/models/mistral/basic.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import os

from agno.agent import Agent, RunResponse # noqa
from agno.models.mistral import MistralChat

mistral_api_key = os.getenv("MISTRAL_API_KEY")

agent = Agent(
model=MistralChat(
id="mistral-large-latest",
api_key=mistral_api_key,
),
model=MistralChat(id="mistral-large-latest"),
markdown=True,
)

Expand Down
43 changes: 43 additions & 0 deletions cookbook/models/mistral/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `pip install mistralai sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/models/mistral/memory.py` to run the agent
"""

from agno.agent import Agent, AgentMemory
from agno.memory.db.postgres import PgMemoryDb
from agno.models.mistral.mistral import MistralChat
from agno.storage.agent.postgres import PostgresAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=MistralChat(id="mistral-large-latest"),
# Store the memories and summary in a database
memory=AgentMemory(
db=PgMemoryDb(table_name="agent_memory", db_url=db_url),
create_user_memories=True,
create_session_summary=True,
),
# Store agent sessions in a database
storage=PostgresAgentStorage(
table_name="personalized_agent_sessions", db_url=db_url
),
# Show debug logs so, you can see the memory being created
# debug_mode=True,
)

# -*- Share personal information
agent.print_response("My name is john billings?", stream=True)

# -*- Share personal information
agent.print_response("I live in nyc?", stream=True)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow?", stream=True)

# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)
2 changes: 1 addition & 1 deletion cookbook/models/mistral/mistral_small.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
show_tool_calls=True,
markdown=True,
)
agent.print_response("Whats happening in France?", stream=True)
agent.print_response("Tell me about mistrall small, any news", stream=True)
13 changes: 9 additions & 4 deletions cookbook/models/ollama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ python cookbook/models/ollama/storage.py
python cookbook/models/ollama/knowledge.py
```

### 9. Run Agent that interprets an image
### 9. Run Agent that uses memory

```shell
python cookbook/models/ollama/memory.py
```

### 10. Run Agent that interprets an image

Pull the llama3.2 vision model

Expand All @@ -75,14 +81,13 @@ ollama pull llama3.2-vision
python cookbook/models/ollama/image_agent.py
```

### 10. Run Agent that manually sets the Ollama client
### 11. Run Agent that manually sets the Ollama client

```shell
python cookbook/models/ollama/set_client.py
```


### 11. See demos of some well-known Ollama models
### 12. See demos of some well-known Ollama models

```shell
python cookbook/models/ollama/demo_deepseek_r1.py
Expand Down
43 changes: 43 additions & 0 deletions cookbook/models/ollama/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `pip install ollama sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/models/ollama/memory.py` to run the agent
"""

from agno.agent import Agent, AgentMemory
from agno.memory.db.postgres import PgMemoryDb
from agno.models.ollama.chat import Ollama
from agno.storage.agent.postgres import PostgresAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=Ollama(id="qwen2.5:latest"),
# Store the memories and summary in a database
memory=AgentMemory(
db=PgMemoryDb(table_name="agent_memory", db_url=db_url),
create_user_memories=True,
create_session_summary=True,
),
# Store agent sessions in a database
storage=PostgresAgentStorage(
table_name="personalized_agent_sessions", db_url=db_url
),
# Show debug logs so, you can see the memory being created
# debug_mode=True,
)

# -*- Share personal information
agent.print_response("My name is john billings?", stream=True)

# -*- Share personal information
agent.print_response("I live in nyc?", stream=True)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow?", stream=True)

# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)
2 changes: 1 addition & 1 deletion cookbook/models/openai/memory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/run_pgvector.sh` to start a postgres container with pgvector
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `pip install openai sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/agents/personalized_memories_and_summaries.py` to run the agent
"""
Expand Down
4 changes: 3 additions & 1 deletion libs/agno/agno/models/anthropic/claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def get_client(self) -> AnthropicClient:
_client_params["api_key"] = self.api_key
if self.client_params:
_client_params.update(self.client_params)
return AnthropicClient(**_client_params)

self.client = AnthropicClient(**_client_params)
return self.client

@property
def request_kwargs(self) -> Dict[str, Any]:
Expand Down
10 changes: 6 additions & 4 deletions libs/agno/agno/models/cohere/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class Cohere(Model):
api_key: Optional[str] = None
client_params: Optional[Dict[str, Any]] = None
# -*- Provide the Cohere client manually
cohere_client: Optional[CohereClient] = None
client: Optional[CohereClient] = None

def get_client(self) -> CohereClient:
if self.cohere_client:
return self.cohere_client
if self.client:
return self.client

_client_params: Dict[str, Any] = {}

Expand All @@ -68,7 +68,9 @@ def get_client(self) -> CohereClient:

if self.api_key:
_client_params["api_key"] = self.api_key
return CohereClient(**_client_params)

self.client = CohereClient(**_client_params)
return self.client

@property
def request_kwargs(self) -> Dict[str, Any]:
Expand Down
4 changes: 3 additions & 1 deletion libs/agno/agno/models/google/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ def get_client(self) -> GenerativeModel:
if self.client_params:
client_params.update(self.client_params)
genai.configure(**client_params)
return genai.GenerativeModel(model_name=self.id, **self.request_kwargs)

self.client = genai.GenerativeModel(model_name=self.id, **self.request_kwargs)
return self.client

@property
def request_kwargs(self) -> Dict[str, Any]:
Expand Down
4 changes: 3 additions & 1 deletion libs/agno/agno/models/groq/groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def get_client(self) -> GroqClient:
client_params: Dict[str, Any] = self.get_client_params()
if self.http_client is not None:
client_params["http_client"] = self.http_client
return GroqClient(**client_params)

self.client = GroqClient(**client_params)
return self.client

def get_async_client(self) -> AsyncGroqClient:
"""
Expand Down
3 changes: 2 additions & 1 deletion libs/agno/agno/models/huggingface/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def get_client(self) -> InferenceClient:
_client_params: Dict[str, Any] = self.get_client_params()
if self.http_client is not None:
_client_params["http_client"] = self.http_client
return InferenceClient(**_client_params)
self.client = InferenceClient(**_client_params)
return self.client

def get_async_client(self) -> AsyncInferenceClient:
"""
Expand Down
1 change: 1 addition & 0 deletions libs/agno/agno/models/mistral/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def client(self) -> MistralClient:
_client_params["timeout"] = self.timeout
if self.client_params:
_client_params.update(self.client_params)

self.mistral_client = MistralClient(**_client_params)
return self.mistral_client

Expand Down
3 changes: 2 additions & 1 deletion libs/agno/agno/models/ollama/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def get_client(self) -> OllamaClient:
if self.client is not None:
return self.client

return OllamaClient(**self.get_client_params())
self.client = OllamaClient(**self.get_client_params())
return self.client

def get_async_client(self) -> AsyncOllamaClient:
"""
Expand Down
4 changes: 3 additions & 1 deletion libs/agno/agno/models/openai/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def get_client(self) -> OpenAIClient:
client_params: Dict[str, Any] = self._get_client_params()
if self.http_client is not None:
client_params["http_client"] = self.http_client
return OpenAIClient(**client_params)

self.client = OpenAIClient(**client_params)
return self.client

def get_async_client(self) -> AsyncOpenAIClient:
"""
Expand Down
2 changes: 1 addition & 1 deletion libs/agno/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ firecrawl = ["firecrawl-py"]

# Dependencies for Storage
sql = ["sqlalchemy"]
postgres = ["psycopg"]
postgres = ["psycopg-binary"]

# Dependencies for Vector databases
pgvector = ["pgvector"]
Expand Down

0 comments on commit 6653576

Please sign in to comment.