agno 2.2.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agno/__init__.py +8 -0
- agno/agent/__init__.py +51 -0
- agno/agent/agent.py +10405 -0
- agno/api/__init__.py +0 -0
- agno/api/agent.py +28 -0
- agno/api/api.py +40 -0
- agno/api/evals.py +22 -0
- agno/api/os.py +17 -0
- agno/api/routes.py +13 -0
- agno/api/schemas/__init__.py +9 -0
- agno/api/schemas/agent.py +16 -0
- agno/api/schemas/evals.py +16 -0
- agno/api/schemas/os.py +14 -0
- agno/api/schemas/response.py +6 -0
- agno/api/schemas/team.py +16 -0
- agno/api/schemas/utils.py +21 -0
- agno/api/schemas/workflows.py +16 -0
- agno/api/settings.py +53 -0
- agno/api/team.py +30 -0
- agno/api/workflow.py +28 -0
- agno/cloud/aws/base.py +214 -0
- agno/cloud/aws/s3/__init__.py +2 -0
- agno/cloud/aws/s3/api_client.py +43 -0
- agno/cloud/aws/s3/bucket.py +195 -0
- agno/cloud/aws/s3/object.py +57 -0
- agno/culture/__init__.py +3 -0
- agno/culture/manager.py +956 -0
- agno/db/__init__.py +24 -0
- agno/db/async_postgres/__init__.py +3 -0
- agno/db/base.py +598 -0
- agno/db/dynamo/__init__.py +3 -0
- agno/db/dynamo/dynamo.py +2042 -0
- agno/db/dynamo/schemas.py +314 -0
- agno/db/dynamo/utils.py +743 -0
- agno/db/firestore/__init__.py +3 -0
- agno/db/firestore/firestore.py +1795 -0
- agno/db/firestore/schemas.py +140 -0
- agno/db/firestore/utils.py +376 -0
- agno/db/gcs_json/__init__.py +3 -0
- agno/db/gcs_json/gcs_json_db.py +1335 -0
- agno/db/gcs_json/utils.py +228 -0
- agno/db/in_memory/__init__.py +3 -0
- agno/db/in_memory/in_memory_db.py +1160 -0
- agno/db/in_memory/utils.py +230 -0
- agno/db/json/__init__.py +3 -0
- agno/db/json/json_db.py +1328 -0
- agno/db/json/utils.py +230 -0
- agno/db/migrations/__init__.py +0 -0
- agno/db/migrations/v1_to_v2.py +635 -0
- agno/db/mongo/__init__.py +17 -0
- agno/db/mongo/async_mongo.py +2026 -0
- agno/db/mongo/mongo.py +1982 -0
- agno/db/mongo/schemas.py +87 -0
- agno/db/mongo/utils.py +259 -0
- agno/db/mysql/__init__.py +3 -0
- agno/db/mysql/mysql.py +2308 -0
- agno/db/mysql/schemas.py +138 -0
- agno/db/mysql/utils.py +355 -0
- agno/db/postgres/__init__.py +4 -0
- agno/db/postgres/async_postgres.py +1927 -0
- agno/db/postgres/postgres.py +2260 -0
- agno/db/postgres/schemas.py +139 -0
- agno/db/postgres/utils.py +442 -0
- agno/db/redis/__init__.py +3 -0
- agno/db/redis/redis.py +1660 -0
- agno/db/redis/schemas.py +123 -0
- agno/db/redis/utils.py +346 -0
- agno/db/schemas/__init__.py +4 -0
- agno/db/schemas/culture.py +120 -0
- agno/db/schemas/evals.py +33 -0
- agno/db/schemas/knowledge.py +40 -0
- agno/db/schemas/memory.py +46 -0
- agno/db/schemas/metrics.py +0 -0
- agno/db/singlestore/__init__.py +3 -0
- agno/db/singlestore/schemas.py +130 -0
- agno/db/singlestore/singlestore.py +2272 -0
- agno/db/singlestore/utils.py +384 -0
- agno/db/sqlite/__init__.py +4 -0
- agno/db/sqlite/async_sqlite.py +2293 -0
- agno/db/sqlite/schemas.py +133 -0
- agno/db/sqlite/sqlite.py +2288 -0
- agno/db/sqlite/utils.py +431 -0
- agno/db/surrealdb/__init__.py +3 -0
- agno/db/surrealdb/metrics.py +292 -0
- agno/db/surrealdb/models.py +309 -0
- agno/db/surrealdb/queries.py +71 -0
- agno/db/surrealdb/surrealdb.py +1353 -0
- agno/db/surrealdb/utils.py +147 -0
- agno/db/utils.py +116 -0
- agno/debug.py +18 -0
- agno/eval/__init__.py +14 -0
- agno/eval/accuracy.py +834 -0
- agno/eval/performance.py +773 -0
- agno/eval/reliability.py +306 -0
- agno/eval/utils.py +119 -0
- agno/exceptions.py +161 -0
- agno/filters.py +354 -0
- agno/guardrails/__init__.py +6 -0
- agno/guardrails/base.py +19 -0
- agno/guardrails/openai.py +144 -0
- agno/guardrails/pii.py +94 -0
- agno/guardrails/prompt_injection.py +52 -0
- agno/integrations/__init__.py +0 -0
- agno/integrations/discord/__init__.py +3 -0
- agno/integrations/discord/client.py +203 -0
- agno/knowledge/__init__.py +5 -0
- agno/knowledge/chunking/__init__.py +0 -0
- agno/knowledge/chunking/agentic.py +79 -0
- agno/knowledge/chunking/document.py +91 -0
- agno/knowledge/chunking/fixed.py +57 -0
- agno/knowledge/chunking/markdown.py +151 -0
- agno/knowledge/chunking/recursive.py +63 -0
- agno/knowledge/chunking/row.py +39 -0
- agno/knowledge/chunking/semantic.py +86 -0
- agno/knowledge/chunking/strategy.py +165 -0
- agno/knowledge/content.py +74 -0
- agno/knowledge/document/__init__.py +5 -0
- agno/knowledge/document/base.py +58 -0
- agno/knowledge/embedder/__init__.py +5 -0
- agno/knowledge/embedder/aws_bedrock.py +343 -0
- agno/knowledge/embedder/azure_openai.py +210 -0
- agno/knowledge/embedder/base.py +23 -0
- agno/knowledge/embedder/cohere.py +323 -0
- agno/knowledge/embedder/fastembed.py +62 -0
- agno/knowledge/embedder/fireworks.py +13 -0
- agno/knowledge/embedder/google.py +258 -0
- agno/knowledge/embedder/huggingface.py +94 -0
- agno/knowledge/embedder/jina.py +182 -0
- agno/knowledge/embedder/langdb.py +22 -0
- agno/knowledge/embedder/mistral.py +206 -0
- agno/knowledge/embedder/nebius.py +13 -0
- agno/knowledge/embedder/ollama.py +154 -0
- agno/knowledge/embedder/openai.py +195 -0
- agno/knowledge/embedder/sentence_transformer.py +63 -0
- agno/knowledge/embedder/together.py +13 -0
- agno/knowledge/embedder/vllm.py +262 -0
- agno/knowledge/embedder/voyageai.py +165 -0
- agno/knowledge/knowledge.py +1988 -0
- agno/knowledge/reader/__init__.py +7 -0
- agno/knowledge/reader/arxiv_reader.py +81 -0
- agno/knowledge/reader/base.py +95 -0
- agno/knowledge/reader/csv_reader.py +166 -0
- agno/knowledge/reader/docx_reader.py +82 -0
- agno/knowledge/reader/field_labeled_csv_reader.py +292 -0
- agno/knowledge/reader/firecrawl_reader.py +201 -0
- agno/knowledge/reader/json_reader.py +87 -0
- agno/knowledge/reader/markdown_reader.py +137 -0
- agno/knowledge/reader/pdf_reader.py +431 -0
- agno/knowledge/reader/pptx_reader.py +101 -0
- agno/knowledge/reader/reader_factory.py +313 -0
- agno/knowledge/reader/s3_reader.py +89 -0
- agno/knowledge/reader/tavily_reader.py +194 -0
- agno/knowledge/reader/text_reader.py +115 -0
- agno/knowledge/reader/web_search_reader.py +372 -0
- agno/knowledge/reader/website_reader.py +455 -0
- agno/knowledge/reader/wikipedia_reader.py +59 -0
- agno/knowledge/reader/youtube_reader.py +78 -0
- agno/knowledge/remote_content/__init__.py +0 -0
- agno/knowledge/remote_content/remote_content.py +88 -0
- agno/knowledge/reranker/__init__.py +3 -0
- agno/knowledge/reranker/base.py +14 -0
- agno/knowledge/reranker/cohere.py +64 -0
- agno/knowledge/reranker/infinity.py +195 -0
- agno/knowledge/reranker/sentence_transformer.py +54 -0
- agno/knowledge/types.py +39 -0
- agno/knowledge/utils.py +189 -0
- agno/media.py +462 -0
- agno/memory/__init__.py +3 -0
- agno/memory/manager.py +1327 -0
- agno/models/__init__.py +0 -0
- agno/models/aimlapi/__init__.py +5 -0
- agno/models/aimlapi/aimlapi.py +45 -0
- agno/models/anthropic/__init__.py +5 -0
- agno/models/anthropic/claude.py +757 -0
- agno/models/aws/__init__.py +15 -0
- agno/models/aws/bedrock.py +701 -0
- agno/models/aws/claude.py +378 -0
- agno/models/azure/__init__.py +18 -0
- agno/models/azure/ai_foundry.py +485 -0
- agno/models/azure/openai_chat.py +131 -0
- agno/models/base.py +2175 -0
- agno/models/cerebras/__init__.py +12 -0
- agno/models/cerebras/cerebras.py +501 -0
- agno/models/cerebras/cerebras_openai.py +112 -0
- agno/models/cohere/__init__.py +5 -0
- agno/models/cohere/chat.py +389 -0
- agno/models/cometapi/__init__.py +5 -0
- agno/models/cometapi/cometapi.py +57 -0
- agno/models/dashscope/__init__.py +5 -0
- agno/models/dashscope/dashscope.py +91 -0
- agno/models/deepinfra/__init__.py +5 -0
- agno/models/deepinfra/deepinfra.py +28 -0
- agno/models/deepseek/__init__.py +5 -0
- agno/models/deepseek/deepseek.py +61 -0
- agno/models/defaults.py +1 -0
- agno/models/fireworks/__init__.py +5 -0
- agno/models/fireworks/fireworks.py +26 -0
- agno/models/google/__init__.py +5 -0
- agno/models/google/gemini.py +1085 -0
- agno/models/groq/__init__.py +5 -0
- agno/models/groq/groq.py +556 -0
- agno/models/huggingface/__init__.py +5 -0
- agno/models/huggingface/huggingface.py +491 -0
- agno/models/ibm/__init__.py +5 -0
- agno/models/ibm/watsonx.py +422 -0
- agno/models/internlm/__init__.py +3 -0
- agno/models/internlm/internlm.py +26 -0
- agno/models/langdb/__init__.py +1 -0
- agno/models/langdb/langdb.py +48 -0
- agno/models/litellm/__init__.py +14 -0
- agno/models/litellm/chat.py +468 -0
- agno/models/litellm/litellm_openai.py +25 -0
- agno/models/llama_cpp/__init__.py +5 -0
- agno/models/llama_cpp/llama_cpp.py +22 -0
- agno/models/lmstudio/__init__.py +5 -0
- agno/models/lmstudio/lmstudio.py +25 -0
- agno/models/message.py +434 -0
- agno/models/meta/__init__.py +12 -0
- agno/models/meta/llama.py +475 -0
- agno/models/meta/llama_openai.py +78 -0
- agno/models/metrics.py +120 -0
- agno/models/mistral/__init__.py +5 -0
- agno/models/mistral/mistral.py +432 -0
- agno/models/nebius/__init__.py +3 -0
- agno/models/nebius/nebius.py +54 -0
- agno/models/nexus/__init__.py +3 -0
- agno/models/nexus/nexus.py +22 -0
- agno/models/nvidia/__init__.py +5 -0
- agno/models/nvidia/nvidia.py +28 -0
- agno/models/ollama/__init__.py +5 -0
- agno/models/ollama/chat.py +441 -0
- agno/models/openai/__init__.py +9 -0
- agno/models/openai/chat.py +883 -0
- agno/models/openai/like.py +27 -0
- agno/models/openai/responses.py +1050 -0
- agno/models/openrouter/__init__.py +5 -0
- agno/models/openrouter/openrouter.py +66 -0
- agno/models/perplexity/__init__.py +5 -0
- agno/models/perplexity/perplexity.py +187 -0
- agno/models/portkey/__init__.py +3 -0
- agno/models/portkey/portkey.py +81 -0
- agno/models/requesty/__init__.py +5 -0
- agno/models/requesty/requesty.py +52 -0
- agno/models/response.py +199 -0
- agno/models/sambanova/__init__.py +5 -0
- agno/models/sambanova/sambanova.py +28 -0
- agno/models/siliconflow/__init__.py +5 -0
- agno/models/siliconflow/siliconflow.py +25 -0
- agno/models/together/__init__.py +5 -0
- agno/models/together/together.py +25 -0
- agno/models/utils.py +266 -0
- agno/models/vercel/__init__.py +3 -0
- agno/models/vercel/v0.py +26 -0
- agno/models/vertexai/__init__.py +0 -0
- agno/models/vertexai/claude.py +70 -0
- agno/models/vllm/__init__.py +3 -0
- agno/models/vllm/vllm.py +78 -0
- agno/models/xai/__init__.py +3 -0
- agno/models/xai/xai.py +113 -0
- agno/os/__init__.py +3 -0
- agno/os/app.py +876 -0
- agno/os/auth.py +57 -0
- agno/os/config.py +104 -0
- agno/os/interfaces/__init__.py +1 -0
- agno/os/interfaces/a2a/__init__.py +3 -0
- agno/os/interfaces/a2a/a2a.py +42 -0
- agno/os/interfaces/a2a/router.py +250 -0
- agno/os/interfaces/a2a/utils.py +924 -0
- agno/os/interfaces/agui/__init__.py +3 -0
- agno/os/interfaces/agui/agui.py +47 -0
- agno/os/interfaces/agui/router.py +144 -0
- agno/os/interfaces/agui/utils.py +534 -0
- agno/os/interfaces/base.py +25 -0
- agno/os/interfaces/slack/__init__.py +3 -0
- agno/os/interfaces/slack/router.py +148 -0
- agno/os/interfaces/slack/security.py +30 -0
- agno/os/interfaces/slack/slack.py +47 -0
- agno/os/interfaces/whatsapp/__init__.py +3 -0
- agno/os/interfaces/whatsapp/router.py +211 -0
- agno/os/interfaces/whatsapp/security.py +53 -0
- agno/os/interfaces/whatsapp/whatsapp.py +36 -0
- agno/os/mcp.py +292 -0
- agno/os/middleware/__init__.py +7 -0
- agno/os/middleware/jwt.py +233 -0
- agno/os/router.py +1763 -0
- agno/os/routers/__init__.py +3 -0
- agno/os/routers/evals/__init__.py +3 -0
- agno/os/routers/evals/evals.py +430 -0
- agno/os/routers/evals/schemas.py +142 -0
- agno/os/routers/evals/utils.py +162 -0
- agno/os/routers/health.py +31 -0
- agno/os/routers/home.py +52 -0
- agno/os/routers/knowledge/__init__.py +3 -0
- agno/os/routers/knowledge/knowledge.py +997 -0
- agno/os/routers/knowledge/schemas.py +178 -0
- agno/os/routers/memory/__init__.py +3 -0
- agno/os/routers/memory/memory.py +515 -0
- agno/os/routers/memory/schemas.py +62 -0
- agno/os/routers/metrics/__init__.py +3 -0
- agno/os/routers/metrics/metrics.py +190 -0
- agno/os/routers/metrics/schemas.py +47 -0
- agno/os/routers/session/__init__.py +3 -0
- agno/os/routers/session/session.py +997 -0
- agno/os/schema.py +1055 -0
- agno/os/settings.py +43 -0
- agno/os/utils.py +630 -0
- agno/py.typed +0 -0
- agno/reasoning/__init__.py +0 -0
- agno/reasoning/anthropic.py +80 -0
- agno/reasoning/azure_ai_foundry.py +67 -0
- agno/reasoning/deepseek.py +63 -0
- agno/reasoning/default.py +97 -0
- agno/reasoning/gemini.py +73 -0
- agno/reasoning/groq.py +71 -0
- agno/reasoning/helpers.py +63 -0
- agno/reasoning/ollama.py +67 -0
- agno/reasoning/openai.py +86 -0
- agno/reasoning/step.py +31 -0
- agno/reasoning/vertexai.py +76 -0
- agno/run/__init__.py +6 -0
- agno/run/agent.py +787 -0
- agno/run/base.py +229 -0
- agno/run/cancel.py +81 -0
- agno/run/messages.py +32 -0
- agno/run/team.py +753 -0
- agno/run/workflow.py +708 -0
- agno/session/__init__.py +10 -0
- agno/session/agent.py +295 -0
- agno/session/summary.py +265 -0
- agno/session/team.py +392 -0
- agno/session/workflow.py +205 -0
- agno/team/__init__.py +37 -0
- agno/team/team.py +8793 -0
- agno/tools/__init__.py +10 -0
- agno/tools/agentql.py +120 -0
- agno/tools/airflow.py +69 -0
- agno/tools/api.py +122 -0
- agno/tools/apify.py +314 -0
- agno/tools/arxiv.py +127 -0
- agno/tools/aws_lambda.py +53 -0
- agno/tools/aws_ses.py +66 -0
- agno/tools/baidusearch.py +89 -0
- agno/tools/bitbucket.py +292 -0
- agno/tools/brandfetch.py +213 -0
- agno/tools/bravesearch.py +106 -0
- agno/tools/brightdata.py +367 -0
- agno/tools/browserbase.py +209 -0
- agno/tools/calcom.py +255 -0
- agno/tools/calculator.py +151 -0
- agno/tools/cartesia.py +187 -0
- agno/tools/clickup.py +244 -0
- agno/tools/confluence.py +240 -0
- agno/tools/crawl4ai.py +158 -0
- agno/tools/csv_toolkit.py +185 -0
- agno/tools/dalle.py +110 -0
- agno/tools/daytona.py +475 -0
- agno/tools/decorator.py +262 -0
- agno/tools/desi_vocal.py +108 -0
- agno/tools/discord.py +161 -0
- agno/tools/docker.py +716 -0
- agno/tools/duckdb.py +379 -0
- agno/tools/duckduckgo.py +91 -0
- agno/tools/e2b.py +703 -0
- agno/tools/eleven_labs.py +196 -0
- agno/tools/email.py +67 -0
- agno/tools/evm.py +129 -0
- agno/tools/exa.py +396 -0
- agno/tools/fal.py +127 -0
- agno/tools/file.py +240 -0
- agno/tools/file_generation.py +350 -0
- agno/tools/financial_datasets.py +288 -0
- agno/tools/firecrawl.py +143 -0
- agno/tools/function.py +1187 -0
- agno/tools/giphy.py +93 -0
- agno/tools/github.py +1760 -0
- agno/tools/gmail.py +922 -0
- agno/tools/google_bigquery.py +117 -0
- agno/tools/google_drive.py +270 -0
- agno/tools/google_maps.py +253 -0
- agno/tools/googlecalendar.py +674 -0
- agno/tools/googlesearch.py +98 -0
- agno/tools/googlesheets.py +377 -0
- agno/tools/hackernews.py +77 -0
- agno/tools/jina.py +101 -0
- agno/tools/jira.py +170 -0
- agno/tools/knowledge.py +218 -0
- agno/tools/linear.py +426 -0
- agno/tools/linkup.py +58 -0
- agno/tools/local_file_system.py +90 -0
- agno/tools/lumalab.py +183 -0
- agno/tools/mcp/__init__.py +10 -0
- agno/tools/mcp/mcp.py +331 -0
- agno/tools/mcp/multi_mcp.py +347 -0
- agno/tools/mcp/params.py +24 -0
- agno/tools/mcp_toolbox.py +284 -0
- agno/tools/mem0.py +193 -0
- agno/tools/memori.py +339 -0
- agno/tools/memory.py +419 -0
- agno/tools/mlx_transcribe.py +139 -0
- agno/tools/models/__init__.py +0 -0
- agno/tools/models/azure_openai.py +190 -0
- agno/tools/models/gemini.py +203 -0
- agno/tools/models/groq.py +158 -0
- agno/tools/models/morph.py +186 -0
- agno/tools/models/nebius.py +124 -0
- agno/tools/models_labs.py +195 -0
- agno/tools/moviepy_video.py +349 -0
- agno/tools/neo4j.py +134 -0
- agno/tools/newspaper.py +46 -0
- agno/tools/newspaper4k.py +93 -0
- agno/tools/notion.py +204 -0
- agno/tools/openai.py +202 -0
- agno/tools/openbb.py +160 -0
- agno/tools/opencv.py +321 -0
- agno/tools/openweather.py +233 -0
- agno/tools/oxylabs.py +385 -0
- agno/tools/pandas.py +102 -0
- agno/tools/parallel.py +314 -0
- agno/tools/postgres.py +257 -0
- agno/tools/pubmed.py +188 -0
- agno/tools/python.py +205 -0
- agno/tools/reasoning.py +283 -0
- agno/tools/reddit.py +467 -0
- agno/tools/replicate.py +117 -0
- agno/tools/resend.py +62 -0
- agno/tools/scrapegraph.py +222 -0
- agno/tools/searxng.py +152 -0
- agno/tools/serpapi.py +116 -0
- agno/tools/serper.py +255 -0
- agno/tools/shell.py +53 -0
- agno/tools/slack.py +136 -0
- agno/tools/sleep.py +20 -0
- agno/tools/spider.py +116 -0
- agno/tools/sql.py +154 -0
- agno/tools/streamlit/__init__.py +0 -0
- agno/tools/streamlit/components.py +113 -0
- agno/tools/tavily.py +254 -0
- agno/tools/telegram.py +48 -0
- agno/tools/todoist.py +218 -0
- agno/tools/tool_registry.py +1 -0
- agno/tools/toolkit.py +146 -0
- agno/tools/trafilatura.py +388 -0
- agno/tools/trello.py +274 -0
- agno/tools/twilio.py +186 -0
- agno/tools/user_control_flow.py +78 -0
- agno/tools/valyu.py +228 -0
- agno/tools/visualization.py +467 -0
- agno/tools/webbrowser.py +28 -0
- agno/tools/webex.py +76 -0
- agno/tools/website.py +54 -0
- agno/tools/webtools.py +45 -0
- agno/tools/whatsapp.py +286 -0
- agno/tools/wikipedia.py +63 -0
- agno/tools/workflow.py +278 -0
- agno/tools/x.py +335 -0
- agno/tools/yfinance.py +257 -0
- agno/tools/youtube.py +184 -0
- agno/tools/zendesk.py +82 -0
- agno/tools/zep.py +454 -0
- agno/tools/zoom.py +382 -0
- agno/utils/__init__.py +0 -0
- agno/utils/agent.py +820 -0
- agno/utils/audio.py +49 -0
- agno/utils/certs.py +27 -0
- agno/utils/code_execution.py +11 -0
- agno/utils/common.py +132 -0
- agno/utils/dttm.py +13 -0
- agno/utils/enum.py +22 -0
- agno/utils/env.py +11 -0
- agno/utils/events.py +696 -0
- agno/utils/format_str.py +16 -0
- agno/utils/functions.py +166 -0
- agno/utils/gemini.py +426 -0
- agno/utils/hooks.py +57 -0
- agno/utils/http.py +74 -0
- agno/utils/json_schema.py +234 -0
- agno/utils/knowledge.py +36 -0
- agno/utils/location.py +19 -0
- agno/utils/log.py +255 -0
- agno/utils/mcp.py +214 -0
- agno/utils/media.py +352 -0
- agno/utils/merge_dict.py +41 -0
- agno/utils/message.py +118 -0
- agno/utils/models/__init__.py +0 -0
- agno/utils/models/ai_foundry.py +43 -0
- agno/utils/models/claude.py +358 -0
- agno/utils/models/cohere.py +87 -0
- agno/utils/models/llama.py +78 -0
- agno/utils/models/mistral.py +98 -0
- agno/utils/models/openai_responses.py +140 -0
- agno/utils/models/schema_utils.py +153 -0
- agno/utils/models/watsonx.py +41 -0
- agno/utils/openai.py +257 -0
- agno/utils/pickle.py +32 -0
- agno/utils/pprint.py +178 -0
- agno/utils/print_response/__init__.py +0 -0
- agno/utils/print_response/agent.py +842 -0
- agno/utils/print_response/team.py +1724 -0
- agno/utils/print_response/workflow.py +1668 -0
- agno/utils/prompts.py +111 -0
- agno/utils/reasoning.py +108 -0
- agno/utils/response.py +163 -0
- agno/utils/response_iterator.py +17 -0
- agno/utils/safe_formatter.py +24 -0
- agno/utils/serialize.py +32 -0
- agno/utils/shell.py +22 -0
- agno/utils/streamlit.py +487 -0
- agno/utils/string.py +231 -0
- agno/utils/team.py +139 -0
- agno/utils/timer.py +41 -0
- agno/utils/tools.py +102 -0
- agno/utils/web.py +23 -0
- agno/utils/whatsapp.py +305 -0
- agno/utils/yaml_io.py +25 -0
- agno/vectordb/__init__.py +3 -0
- agno/vectordb/base.py +127 -0
- agno/vectordb/cassandra/__init__.py +5 -0
- agno/vectordb/cassandra/cassandra.py +501 -0
- agno/vectordb/cassandra/extra_param_mixin.py +11 -0
- agno/vectordb/cassandra/index.py +13 -0
- agno/vectordb/chroma/__init__.py +5 -0
- agno/vectordb/chroma/chromadb.py +929 -0
- agno/vectordb/clickhouse/__init__.py +9 -0
- agno/vectordb/clickhouse/clickhousedb.py +835 -0
- agno/vectordb/clickhouse/index.py +9 -0
- agno/vectordb/couchbase/__init__.py +3 -0
- agno/vectordb/couchbase/couchbase.py +1442 -0
- agno/vectordb/distance.py +7 -0
- agno/vectordb/lancedb/__init__.py +6 -0
- agno/vectordb/lancedb/lance_db.py +995 -0
- agno/vectordb/langchaindb/__init__.py +5 -0
- agno/vectordb/langchaindb/langchaindb.py +163 -0
- agno/vectordb/lightrag/__init__.py +5 -0
- agno/vectordb/lightrag/lightrag.py +388 -0
- agno/vectordb/llamaindex/__init__.py +3 -0
- agno/vectordb/llamaindex/llamaindexdb.py +166 -0
- agno/vectordb/milvus/__init__.py +4 -0
- agno/vectordb/milvus/milvus.py +1182 -0
- agno/vectordb/mongodb/__init__.py +9 -0
- agno/vectordb/mongodb/mongodb.py +1417 -0
- agno/vectordb/pgvector/__init__.py +12 -0
- agno/vectordb/pgvector/index.py +23 -0
- agno/vectordb/pgvector/pgvector.py +1462 -0
- agno/vectordb/pineconedb/__init__.py +5 -0
- agno/vectordb/pineconedb/pineconedb.py +747 -0
- agno/vectordb/qdrant/__init__.py +5 -0
- agno/vectordb/qdrant/qdrant.py +1134 -0
- agno/vectordb/redis/__init__.py +9 -0
- agno/vectordb/redis/redisdb.py +694 -0
- agno/vectordb/search.py +7 -0
- agno/vectordb/singlestore/__init__.py +10 -0
- agno/vectordb/singlestore/index.py +41 -0
- agno/vectordb/singlestore/singlestore.py +763 -0
- agno/vectordb/surrealdb/__init__.py +3 -0
- agno/vectordb/surrealdb/surrealdb.py +699 -0
- agno/vectordb/upstashdb/__init__.py +5 -0
- agno/vectordb/upstashdb/upstashdb.py +718 -0
- agno/vectordb/weaviate/__init__.py +8 -0
- agno/vectordb/weaviate/index.py +15 -0
- agno/vectordb/weaviate/weaviate.py +1005 -0
- agno/workflow/__init__.py +23 -0
- agno/workflow/agent.py +299 -0
- agno/workflow/condition.py +738 -0
- agno/workflow/loop.py +735 -0
- agno/workflow/parallel.py +824 -0
- agno/workflow/router.py +702 -0
- agno/workflow/step.py +1432 -0
- agno/workflow/steps.py +592 -0
- agno/workflow/types.py +520 -0
- agno/workflow/workflow.py +4321 -0
- agno-2.2.13.dist-info/METADATA +614 -0
- agno-2.2.13.dist-info/RECORD +575 -0
- agno-2.2.13.dist-info/WHEEL +5 -0
- agno-2.2.13.dist-info/licenses/LICENSE +201 -0
- agno-2.2.13.dist-info/top_level.txt +1 -0
agno/memory/manager.py
ADDED
|
@@ -0,0 +1,1327 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from os import getenv
|
|
5
|
+
from textwrap import dedent
|
|
6
|
+
from typing import Any, Callable, Dict, List, Literal, Optional, Type, Union
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, Field
|
|
9
|
+
|
|
10
|
+
from agno.db.base import AsyncBaseDb, BaseDb
|
|
11
|
+
from agno.db.schemas import UserMemory
|
|
12
|
+
from agno.models.base import Model
|
|
13
|
+
from agno.models.message import Message
|
|
14
|
+
from agno.models.utils import get_model
|
|
15
|
+
from agno.tools.function import Function
|
|
16
|
+
from agno.utils.log import (
|
|
17
|
+
log_debug,
|
|
18
|
+
log_error,
|
|
19
|
+
log_warning,
|
|
20
|
+
set_log_level_to_debug,
|
|
21
|
+
set_log_level_to_info,
|
|
22
|
+
)
|
|
23
|
+
from agno.utils.prompts import get_json_output_prompt
|
|
24
|
+
from agno.utils.string import parse_response_model_str
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class MemorySearchResponse(BaseModel):
|
|
28
|
+
"""Model for Memory Search Response."""
|
|
29
|
+
|
|
30
|
+
memory_ids: List[str] = Field(
|
|
31
|
+
...,
|
|
32
|
+
description="The IDs of the memories that are most semantically similar to the query.",
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class MemoryManager:
|
|
38
|
+
"""Memory Manager"""
|
|
39
|
+
|
|
40
|
+
# Model used for memory management
|
|
41
|
+
model: Optional[Model] = None
|
|
42
|
+
|
|
43
|
+
# Provide the system message for the manager as a string. If not provided, the default system message will be used.
|
|
44
|
+
system_message: Optional[str] = None
|
|
45
|
+
# Provide the memory capture instructions for the manager as a string. If not provided, the default memory capture instructions will be used.
|
|
46
|
+
memory_capture_instructions: Optional[str] = None
|
|
47
|
+
# Additional instructions for the manager. These instructions are appended to the default system message.
|
|
48
|
+
additional_instructions: Optional[str] = None
|
|
49
|
+
|
|
50
|
+
# Whether memories were created in the last run
|
|
51
|
+
memories_updated: bool = False
|
|
52
|
+
|
|
53
|
+
# ----- db tools ---------
|
|
54
|
+
# Whether to delete memories
|
|
55
|
+
delete_memories: bool = True
|
|
56
|
+
# Whether to clear memories
|
|
57
|
+
clear_memories: bool = True
|
|
58
|
+
# Whether to update memories
|
|
59
|
+
update_memories: bool = True
|
|
60
|
+
# whether to add memories
|
|
61
|
+
add_memories: bool = True
|
|
62
|
+
|
|
63
|
+
# The database to store memories
|
|
64
|
+
db: Optional[Union[BaseDb, AsyncBaseDb]] = None
|
|
65
|
+
|
|
66
|
+
debug_mode: bool = False
|
|
67
|
+
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
model: Optional[Union[Model, str]] = None,
|
|
71
|
+
system_message: Optional[str] = None,
|
|
72
|
+
memory_capture_instructions: Optional[str] = None,
|
|
73
|
+
additional_instructions: Optional[str] = None,
|
|
74
|
+
db: Optional[Union[BaseDb, AsyncBaseDb]] = None,
|
|
75
|
+
delete_memories: bool = False,
|
|
76
|
+
update_memories: bool = True,
|
|
77
|
+
add_memories: bool = True,
|
|
78
|
+
clear_memories: bool = False,
|
|
79
|
+
debug_mode: bool = False,
|
|
80
|
+
):
|
|
81
|
+
self.model = model # type: ignore[assignment]
|
|
82
|
+
self.system_message = system_message
|
|
83
|
+
self.memory_capture_instructions = memory_capture_instructions
|
|
84
|
+
self.additional_instructions = additional_instructions
|
|
85
|
+
self.db = db
|
|
86
|
+
self.delete_memories = delete_memories
|
|
87
|
+
self.update_memories = update_memories
|
|
88
|
+
self.add_memories = add_memories
|
|
89
|
+
self.clear_memories = clear_memories
|
|
90
|
+
self.debug_mode = debug_mode
|
|
91
|
+
|
|
92
|
+
self._get_models()
|
|
93
|
+
|
|
94
|
+
def _get_models(self) -> None:
|
|
95
|
+
if self.model is not None:
|
|
96
|
+
self.model = get_model(self.model)
|
|
97
|
+
|
|
98
|
+
def get_model(self) -> Model:
|
|
99
|
+
if self.model is None:
|
|
100
|
+
try:
|
|
101
|
+
from agno.models.openai import OpenAIChat
|
|
102
|
+
except ModuleNotFoundError as e:
|
|
103
|
+
log_error(e)
|
|
104
|
+
log_error(
|
|
105
|
+
"Agno uses `openai` as the default model provider. Please provide a `model` or install `openai`."
|
|
106
|
+
)
|
|
107
|
+
exit(1)
|
|
108
|
+
self.model = OpenAIChat(id="gpt-4o")
|
|
109
|
+
return self.model
|
|
110
|
+
|
|
111
|
+
def read_from_db(self, user_id: Optional[str] = None):
|
|
112
|
+
if self.db:
|
|
113
|
+
# If no user_id is provided, read all memories
|
|
114
|
+
if user_id is None:
|
|
115
|
+
all_memories: List[UserMemory] = self.db.get_user_memories() # type: ignore
|
|
116
|
+
else:
|
|
117
|
+
all_memories = self.db.get_user_memories(user_id=user_id) # type: ignore
|
|
118
|
+
|
|
119
|
+
memories: Dict[str, List[UserMemory]] = {}
|
|
120
|
+
for memory in all_memories:
|
|
121
|
+
if memory.user_id is not None and memory.memory_id is not None:
|
|
122
|
+
memories.setdefault(memory.user_id, []).append(memory)
|
|
123
|
+
|
|
124
|
+
return memories
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
async def aread_from_db(self, user_id: Optional[str] = None):
|
|
128
|
+
if self.db:
|
|
129
|
+
if isinstance(self.db, AsyncBaseDb):
|
|
130
|
+
# If no user_id is provided, read all memories
|
|
131
|
+
if user_id is None:
|
|
132
|
+
all_memories: List[UserMemory] = await self.db.get_user_memories() # type: ignore
|
|
133
|
+
else:
|
|
134
|
+
all_memories = await self.db.get_user_memories(user_id=user_id) # type: ignore
|
|
135
|
+
else:
|
|
136
|
+
if user_id is None:
|
|
137
|
+
all_memories = self.db.get_user_memories() # type: ignore
|
|
138
|
+
else:
|
|
139
|
+
all_memories = self.db.get_user_memories(user_id=user_id) # type: ignore
|
|
140
|
+
|
|
141
|
+
memories: Dict[str, List[UserMemory]] = {}
|
|
142
|
+
for memory in all_memories:
|
|
143
|
+
if memory.user_id is not None and memory.memory_id is not None:
|
|
144
|
+
memories.setdefault(memory.user_id, []).append(memory)
|
|
145
|
+
|
|
146
|
+
return memories
|
|
147
|
+
return None
|
|
148
|
+
|
|
149
|
+
def set_log_level(self):
|
|
150
|
+
if self.debug_mode or getenv("AGNO_DEBUG", "false").lower() == "true":
|
|
151
|
+
self.debug_mode = True
|
|
152
|
+
set_log_level_to_debug()
|
|
153
|
+
else:
|
|
154
|
+
set_log_level_to_info()
|
|
155
|
+
|
|
156
|
+
def initialize(self, user_id: Optional[str] = None):
|
|
157
|
+
self.set_log_level()
|
|
158
|
+
|
|
159
|
+
# -*- Public Functions
|
|
160
|
+
def get_user_memories(self, user_id: Optional[str] = None) -> Optional[List[UserMemory]]:
|
|
161
|
+
"""Get the user memories for a given user id"""
|
|
162
|
+
if self.db:
|
|
163
|
+
if user_id is None:
|
|
164
|
+
user_id = "default"
|
|
165
|
+
# Refresh from the Db
|
|
166
|
+
memories = self.read_from_db(user_id=user_id)
|
|
167
|
+
if memories is None:
|
|
168
|
+
return []
|
|
169
|
+
return memories.get(user_id, [])
|
|
170
|
+
else:
|
|
171
|
+
log_warning("Memory Db not provided.")
|
|
172
|
+
return []
|
|
173
|
+
|
|
174
|
+
async def aget_user_memories(self, user_id: Optional[str] = None) -> Optional[List[UserMemory]]:
|
|
175
|
+
"""Get the user memories for a given user id"""
|
|
176
|
+
if self.db:
|
|
177
|
+
if user_id is None:
|
|
178
|
+
user_id = "default"
|
|
179
|
+
# Refresh from the Db
|
|
180
|
+
memories = await self.aread_from_db(user_id=user_id)
|
|
181
|
+
if memories is None:
|
|
182
|
+
return []
|
|
183
|
+
return memories.get(user_id, [])
|
|
184
|
+
else:
|
|
185
|
+
log_warning("Memory Db not provided.")
|
|
186
|
+
return []
|
|
187
|
+
|
|
188
|
+
def get_user_memory(self, memory_id: str, user_id: Optional[str] = None) -> Optional[UserMemory]:
|
|
189
|
+
"""Get the user memory for a given user id"""
|
|
190
|
+
if self.db:
|
|
191
|
+
if user_id is None:
|
|
192
|
+
user_id = "default"
|
|
193
|
+
# Refresh from the DB
|
|
194
|
+
memories = self.read_from_db(user_id=user_id)
|
|
195
|
+
if memories is None:
|
|
196
|
+
return None
|
|
197
|
+
memories_for_user = memories.get(user_id, [])
|
|
198
|
+
for memory in memories_for_user:
|
|
199
|
+
if memory.memory_id == memory_id:
|
|
200
|
+
return memory
|
|
201
|
+
return None
|
|
202
|
+
else:
|
|
203
|
+
log_warning("Memory Db not provided.")
|
|
204
|
+
return None
|
|
205
|
+
|
|
206
|
+
def add_user_memory(
|
|
207
|
+
self,
|
|
208
|
+
memory: UserMemory,
|
|
209
|
+
user_id: Optional[str] = None,
|
|
210
|
+
) -> Optional[str]:
|
|
211
|
+
"""Add a user memory for a given user id
|
|
212
|
+
Args:
|
|
213
|
+
memory (UserMemory): The memory to add
|
|
214
|
+
user_id (Optional[str]): The user id to add the memory to. If not provided, the memory is added to the "default" user.
|
|
215
|
+
Returns:
|
|
216
|
+
str: The id of the memory
|
|
217
|
+
"""
|
|
218
|
+
if self.db:
|
|
219
|
+
if memory.memory_id is None:
|
|
220
|
+
from uuid import uuid4
|
|
221
|
+
|
|
222
|
+
memory_id = memory.memory_id or str(uuid4())
|
|
223
|
+
memory.memory_id = memory_id
|
|
224
|
+
|
|
225
|
+
if user_id is None:
|
|
226
|
+
user_id = "default"
|
|
227
|
+
memory.user_id = user_id
|
|
228
|
+
|
|
229
|
+
if not memory.updated_at:
|
|
230
|
+
memory.updated_at = datetime.now()
|
|
231
|
+
|
|
232
|
+
self._upsert_db_memory(memory=memory)
|
|
233
|
+
return memory.memory_id
|
|
234
|
+
|
|
235
|
+
else:
|
|
236
|
+
log_warning("Memory Db not provided.")
|
|
237
|
+
return None
|
|
238
|
+
|
|
239
|
+
def replace_user_memory(
|
|
240
|
+
self,
|
|
241
|
+
memory_id: str,
|
|
242
|
+
memory: UserMemory,
|
|
243
|
+
user_id: Optional[str] = None,
|
|
244
|
+
) -> Optional[str]:
|
|
245
|
+
"""Replace a user memory for a given user id
|
|
246
|
+
Args:
|
|
247
|
+
memory_id (str): The id of the memory to replace
|
|
248
|
+
memory (UserMemory): The memory to add
|
|
249
|
+
user_id (Optional[str]): The user id to add the memory to. If not provided, the memory is added to the "default" user.
|
|
250
|
+
Returns:
|
|
251
|
+
str: The id of the memory
|
|
252
|
+
"""
|
|
253
|
+
if self.db:
|
|
254
|
+
if user_id is None:
|
|
255
|
+
user_id = "default"
|
|
256
|
+
|
|
257
|
+
if not memory.updated_at:
|
|
258
|
+
memory.updated_at = datetime.now()
|
|
259
|
+
|
|
260
|
+
memory.memory_id = memory_id
|
|
261
|
+
memory.user_id = user_id
|
|
262
|
+
|
|
263
|
+
self._upsert_db_memory(memory=memory)
|
|
264
|
+
|
|
265
|
+
return memory.memory_id
|
|
266
|
+
else:
|
|
267
|
+
log_warning("Memory Db not provided.")
|
|
268
|
+
return None
|
|
269
|
+
|
|
270
|
+
def clear(self) -> None:
|
|
271
|
+
"""Clears the memory."""
|
|
272
|
+
if self.db:
|
|
273
|
+
self.db.clear_memories()
|
|
274
|
+
|
|
275
|
+
def delete_user_memory(
|
|
276
|
+
self,
|
|
277
|
+
memory_id: str,
|
|
278
|
+
user_id: Optional[str] = None,
|
|
279
|
+
) -> None:
|
|
280
|
+
"""Delete a user memory for a given user id
|
|
281
|
+
Args:
|
|
282
|
+
memory_id (str): The id of the memory to delete
|
|
283
|
+
user_id (Optional[str]): The user id to delete the memory from. If not provided, the memory is deleted from the "default" user.
|
|
284
|
+
"""
|
|
285
|
+
if user_id is None:
|
|
286
|
+
user_id = "default"
|
|
287
|
+
|
|
288
|
+
if self.db:
|
|
289
|
+
self._delete_db_memory(memory_id=memory_id, user_id=user_id)
|
|
290
|
+
else:
|
|
291
|
+
log_warning("Memory DB not provided.")
|
|
292
|
+
return None
|
|
293
|
+
|
|
294
|
+
# -*- Agent Functions
|
|
295
|
+
def create_user_memories(
|
|
296
|
+
self,
|
|
297
|
+
message: Optional[str] = None,
|
|
298
|
+
messages: Optional[List[Message]] = None,
|
|
299
|
+
agent_id: Optional[str] = None,
|
|
300
|
+
team_id: Optional[str] = None,
|
|
301
|
+
user_id: Optional[str] = None,
|
|
302
|
+
) -> str:
|
|
303
|
+
"""Creates memories from multiple messages and adds them to the memory db."""
|
|
304
|
+
self.set_log_level()
|
|
305
|
+
|
|
306
|
+
if self.db is None:
|
|
307
|
+
log_warning("MemoryDb not provided.")
|
|
308
|
+
return "Please provide a db to store memories"
|
|
309
|
+
|
|
310
|
+
if isinstance(self.db, AsyncBaseDb):
|
|
311
|
+
raise ValueError(
|
|
312
|
+
"create_user_memories() is not supported with an async DB. Please use acreate_user_memories() instead."
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
if not messages and not message:
|
|
316
|
+
raise ValueError("You must provide either a message or a list of messages")
|
|
317
|
+
|
|
318
|
+
if message:
|
|
319
|
+
messages = [Message(role="user", content=message)]
|
|
320
|
+
|
|
321
|
+
if not messages or not isinstance(messages, list):
|
|
322
|
+
raise ValueError("Invalid messages list")
|
|
323
|
+
|
|
324
|
+
if user_id is None:
|
|
325
|
+
user_id = "default"
|
|
326
|
+
|
|
327
|
+
memories = self.read_from_db(user_id=user_id)
|
|
328
|
+
if memories is None:
|
|
329
|
+
memories = {}
|
|
330
|
+
|
|
331
|
+
existing_memories = memories.get(user_id, []) # type: ignore
|
|
332
|
+
existing_memories = [{"memory_id": memory.memory_id, "memory": memory.memory} for memory in existing_memories]
|
|
333
|
+
response = self.create_or_update_memories( # type: ignore
|
|
334
|
+
messages=messages,
|
|
335
|
+
existing_memories=existing_memories,
|
|
336
|
+
user_id=user_id,
|
|
337
|
+
agent_id=agent_id,
|
|
338
|
+
team_id=team_id,
|
|
339
|
+
db=self.db,
|
|
340
|
+
update_memories=self.update_memories,
|
|
341
|
+
add_memories=self.add_memories,
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
# We refresh from the DB
|
|
345
|
+
self.read_from_db(user_id=user_id)
|
|
346
|
+
return response
|
|
347
|
+
|
|
348
|
+
async def acreate_user_memories(
|
|
349
|
+
self,
|
|
350
|
+
message: Optional[str] = None,
|
|
351
|
+
messages: Optional[List[Message]] = None,
|
|
352
|
+
agent_id: Optional[str] = None,
|
|
353
|
+
team_id: Optional[str] = None,
|
|
354
|
+
user_id: Optional[str] = None,
|
|
355
|
+
) -> str:
|
|
356
|
+
"""Creates memories from multiple messages and adds them to the memory db."""
|
|
357
|
+
self.set_log_level()
|
|
358
|
+
|
|
359
|
+
if self.db is None:
|
|
360
|
+
log_warning("MemoryDb not provided.")
|
|
361
|
+
return "Please provide a db to store memories"
|
|
362
|
+
|
|
363
|
+
if not messages and not message:
|
|
364
|
+
raise ValueError("You must provide either a message or a list of messages")
|
|
365
|
+
|
|
366
|
+
if message:
|
|
367
|
+
messages = [Message(role="user", content=message)]
|
|
368
|
+
|
|
369
|
+
if not messages or not isinstance(messages, list):
|
|
370
|
+
raise ValueError("Invalid messages list")
|
|
371
|
+
|
|
372
|
+
if user_id is None:
|
|
373
|
+
user_id = "default"
|
|
374
|
+
|
|
375
|
+
if isinstance(self.db, AsyncBaseDb):
|
|
376
|
+
memories = await self.aread_from_db(user_id=user_id)
|
|
377
|
+
else:
|
|
378
|
+
memories = self.read_from_db(user_id=user_id)
|
|
379
|
+
if memories is None:
|
|
380
|
+
memories = {}
|
|
381
|
+
|
|
382
|
+
existing_memories = memories.get(user_id, []) # type: ignore
|
|
383
|
+
existing_memories = [{"memory_id": memory.memory_id, "memory": memory.memory} for memory in existing_memories]
|
|
384
|
+
|
|
385
|
+
response = await self.acreate_or_update_memories( # type: ignore
|
|
386
|
+
messages=messages,
|
|
387
|
+
existing_memories=existing_memories,
|
|
388
|
+
user_id=user_id,
|
|
389
|
+
agent_id=agent_id,
|
|
390
|
+
team_id=team_id,
|
|
391
|
+
db=self.db,
|
|
392
|
+
update_memories=self.update_memories,
|
|
393
|
+
add_memories=self.add_memories,
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
# We refresh from the DB
|
|
397
|
+
if isinstance(self.db, AsyncBaseDb):
|
|
398
|
+
memories = await self.aread_from_db(user_id=user_id)
|
|
399
|
+
else:
|
|
400
|
+
memories = self.read_from_db(user_id=user_id)
|
|
401
|
+
|
|
402
|
+
return response
|
|
403
|
+
|
|
404
|
+
def update_memory_task(self, task: str, user_id: Optional[str] = None) -> str:
|
|
405
|
+
"""Updates the memory with a task"""
|
|
406
|
+
|
|
407
|
+
if not self.db:
|
|
408
|
+
log_warning("MemoryDb not provided.")
|
|
409
|
+
return "Please provide a db to store memories"
|
|
410
|
+
|
|
411
|
+
if not isinstance(self.db, BaseDb):
|
|
412
|
+
raise ValueError(
|
|
413
|
+
"update_memory_task() is not supported with an async DB. Please use aupdate_memory_task() instead."
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
if user_id is None:
|
|
417
|
+
user_id = "default"
|
|
418
|
+
|
|
419
|
+
memories = self.read_from_db(user_id=user_id)
|
|
420
|
+
if memories is None:
|
|
421
|
+
memories = {}
|
|
422
|
+
|
|
423
|
+
existing_memories = memories.get(user_id, []) # type: ignore
|
|
424
|
+
existing_memories = [{"memory_id": memory.memory_id, "memory": memory.memory} for memory in existing_memories]
|
|
425
|
+
# The memory manager updates the DB directly
|
|
426
|
+
response = self.run_memory_task( # type: ignore
|
|
427
|
+
task=task,
|
|
428
|
+
existing_memories=existing_memories,
|
|
429
|
+
user_id=user_id,
|
|
430
|
+
db=self.db,
|
|
431
|
+
delete_memories=self.delete_memories,
|
|
432
|
+
update_memories=self.update_memories,
|
|
433
|
+
add_memories=self.add_memories,
|
|
434
|
+
clear_memories=self.clear_memories,
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
# We refresh from the DB
|
|
438
|
+
self.read_from_db(user_id=user_id)
|
|
439
|
+
|
|
440
|
+
return response
|
|
441
|
+
|
|
442
|
+
async def aupdate_memory_task(self, task: str, user_id: Optional[str] = None) -> str:
|
|
443
|
+
"""Updates the memory with a task"""
|
|
444
|
+
self.set_log_level()
|
|
445
|
+
|
|
446
|
+
if not self.db:
|
|
447
|
+
log_warning("MemoryDb not provided.")
|
|
448
|
+
return "Please provide a db to store memories"
|
|
449
|
+
|
|
450
|
+
if user_id is None:
|
|
451
|
+
user_id = "default"
|
|
452
|
+
|
|
453
|
+
if isinstance(self.db, AsyncBaseDb):
|
|
454
|
+
memories = await self.aread_from_db(user_id=user_id)
|
|
455
|
+
else:
|
|
456
|
+
memories = self.read_from_db(user_id=user_id)
|
|
457
|
+
|
|
458
|
+
if memories is None:
|
|
459
|
+
memories = {}
|
|
460
|
+
|
|
461
|
+
existing_memories = memories.get(user_id, []) # type: ignore
|
|
462
|
+
existing_memories = [{"memory_id": memory.memory_id, "memory": memory.memory} for memory in existing_memories]
|
|
463
|
+
# The memory manager updates the DB directly
|
|
464
|
+
response = await self.arun_memory_task( # type: ignore
|
|
465
|
+
task=task,
|
|
466
|
+
existing_memories=existing_memories,
|
|
467
|
+
user_id=user_id,
|
|
468
|
+
db=self.db,
|
|
469
|
+
delete_memories=self.delete_memories,
|
|
470
|
+
update_memories=self.update_memories,
|
|
471
|
+
add_memories=self.add_memories,
|
|
472
|
+
clear_memories=self.clear_memories,
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
# We refresh from the DB
|
|
476
|
+
if isinstance(self.db, AsyncBaseDb):
|
|
477
|
+
await self.aread_from_db(user_id=user_id)
|
|
478
|
+
else:
|
|
479
|
+
self.read_from_db(user_id=user_id)
|
|
480
|
+
|
|
481
|
+
return response
|
|
482
|
+
|
|
483
|
+
# -*- Memory Db Functions
|
|
484
|
+
def _upsert_db_memory(self, memory: UserMemory) -> str:
|
|
485
|
+
"""Use this function to add a memory to the database."""
|
|
486
|
+
try:
|
|
487
|
+
if not self.db:
|
|
488
|
+
raise ValueError("Memory db not initialized")
|
|
489
|
+
self.db.upsert_user_memory(memory=memory)
|
|
490
|
+
return "Memory added successfully"
|
|
491
|
+
except Exception as e:
|
|
492
|
+
log_warning(f"Error storing memory in db: {e}")
|
|
493
|
+
return f"Error adding memory: {e}"
|
|
494
|
+
|
|
495
|
+
def _delete_db_memory(self, memory_id: str, user_id: Optional[str] = None) -> str:
|
|
496
|
+
"""Use this function to delete a memory from the database."""
|
|
497
|
+
try:
|
|
498
|
+
if not self.db:
|
|
499
|
+
raise ValueError("Memory db not initialized")
|
|
500
|
+
|
|
501
|
+
if user_id is None:
|
|
502
|
+
user_id = "default"
|
|
503
|
+
|
|
504
|
+
self.db.delete_user_memory(memory_id=memory_id, user_id=user_id)
|
|
505
|
+
return "Memory deleted successfully"
|
|
506
|
+
except Exception as e:
|
|
507
|
+
log_warning(f"Error deleting memory in db: {e}")
|
|
508
|
+
return f"Error deleting memory: {e}"
|
|
509
|
+
|
|
510
|
+
# -*- Utility Functions
|
|
511
|
+
def search_user_memories(
|
|
512
|
+
self,
|
|
513
|
+
query: Optional[str] = None,
|
|
514
|
+
limit: Optional[int] = None,
|
|
515
|
+
retrieval_method: Optional[Literal["last_n", "first_n", "agentic"]] = None,
|
|
516
|
+
user_id: Optional[str] = None,
|
|
517
|
+
) -> List[UserMemory]:
|
|
518
|
+
"""Search through user memories using the specified retrieval method.
|
|
519
|
+
|
|
520
|
+
Args:
|
|
521
|
+
query: The search query for agentic search. Required if retrieval_method is "agentic".
|
|
522
|
+
limit: Maximum number of memories to return. Defaults to self.retrieval_limit if not specified. Optional.
|
|
523
|
+
retrieval_method: The method to use for retrieving memories. Defaults to self.retrieval if not specified.
|
|
524
|
+
- "last_n": Return the most recent memories
|
|
525
|
+
- "first_n": Return the oldest memories
|
|
526
|
+
- "agentic": Return memories most similar to the query, but using an agentic approach
|
|
527
|
+
user_id: The user to search for. Optional.
|
|
528
|
+
|
|
529
|
+
Returns:
|
|
530
|
+
A list of UserMemory objects matching the search criteria.
|
|
531
|
+
"""
|
|
532
|
+
|
|
533
|
+
if user_id is None:
|
|
534
|
+
user_id = "default"
|
|
535
|
+
|
|
536
|
+
self.set_log_level()
|
|
537
|
+
|
|
538
|
+
memories = self.read_from_db(user_id=user_id)
|
|
539
|
+
if memories is None:
|
|
540
|
+
memories = {}
|
|
541
|
+
|
|
542
|
+
if not memories:
|
|
543
|
+
return []
|
|
544
|
+
|
|
545
|
+
# Use default retrieval method if not specified
|
|
546
|
+
retrieval_method = retrieval_method
|
|
547
|
+
# Use default limit if not specified
|
|
548
|
+
limit = limit
|
|
549
|
+
|
|
550
|
+
# Handle different retrieval methods
|
|
551
|
+
if retrieval_method == "agentic":
|
|
552
|
+
if not query:
|
|
553
|
+
raise ValueError("Query is required for agentic search")
|
|
554
|
+
|
|
555
|
+
return self._search_user_memories_agentic(user_id=user_id, query=query, limit=limit)
|
|
556
|
+
|
|
557
|
+
elif retrieval_method == "first_n":
|
|
558
|
+
return self._get_first_n_memories(user_id=user_id, limit=limit)
|
|
559
|
+
|
|
560
|
+
else: # Default to last_n
|
|
561
|
+
return self._get_last_n_memories(user_id=user_id, limit=limit)
|
|
562
|
+
|
|
563
|
+
def _get_response_format(self) -> Union[Dict[str, Any], Type[BaseModel]]:
|
|
564
|
+
model = self.get_model()
|
|
565
|
+
if model.supports_native_structured_outputs:
|
|
566
|
+
return MemorySearchResponse
|
|
567
|
+
|
|
568
|
+
elif model.supports_json_schema_outputs:
|
|
569
|
+
return {
|
|
570
|
+
"type": "json_schema",
|
|
571
|
+
"json_schema": {
|
|
572
|
+
"name": MemorySearchResponse.__name__,
|
|
573
|
+
"schema": MemorySearchResponse.model_json_schema(),
|
|
574
|
+
},
|
|
575
|
+
}
|
|
576
|
+
else:
|
|
577
|
+
return {"type": "json_object"}
|
|
578
|
+
|
|
579
|
+
def _search_user_memories_agentic(self, user_id: str, query: str, limit: Optional[int] = None) -> List[UserMemory]:
|
|
580
|
+
"""Search through user memories using agentic search."""
|
|
581
|
+
memories = self.read_from_db(user_id=user_id)
|
|
582
|
+
if memories is None:
|
|
583
|
+
memories = {}
|
|
584
|
+
|
|
585
|
+
if not memories:
|
|
586
|
+
return []
|
|
587
|
+
|
|
588
|
+
model = self.get_model()
|
|
589
|
+
|
|
590
|
+
response_format = self._get_response_format()
|
|
591
|
+
|
|
592
|
+
log_debug("Searching for memories", center=True)
|
|
593
|
+
|
|
594
|
+
# Get all memories as a list
|
|
595
|
+
user_memories: List[UserMemory] = memories[user_id]
|
|
596
|
+
system_message_str = "Your task is to search through user memories and return the IDs of the memories that are related to the query.\n"
|
|
597
|
+
system_message_str += "\n<user_memories>\n"
|
|
598
|
+
for memory in user_memories:
|
|
599
|
+
system_message_str += f"ID: {memory.memory_id}\n"
|
|
600
|
+
system_message_str += f"Memory: {memory.memory}\n"
|
|
601
|
+
if memory.topics:
|
|
602
|
+
system_message_str += f"Topics: {','.join(memory.topics)}\n"
|
|
603
|
+
system_message_str += "\n"
|
|
604
|
+
system_message_str = system_message_str.strip()
|
|
605
|
+
system_message_str += "\n</user_memories>\n\n"
|
|
606
|
+
system_message_str += "REMEMBER: Only return the IDs of the memories that are related to the query."
|
|
607
|
+
|
|
608
|
+
if response_format == {"type": "json_object"}:
|
|
609
|
+
system_message_str += "\n" + get_json_output_prompt(MemorySearchResponse) # type: ignore
|
|
610
|
+
|
|
611
|
+
messages_for_model = [
|
|
612
|
+
Message(role="system", content=system_message_str),
|
|
613
|
+
Message(
|
|
614
|
+
role="user",
|
|
615
|
+
content=f"Return the IDs of the memories related to the following query: {query}",
|
|
616
|
+
),
|
|
617
|
+
]
|
|
618
|
+
|
|
619
|
+
# Generate a response from the Model (includes running function calls)
|
|
620
|
+
response = model.response(messages=messages_for_model, response_format=response_format)
|
|
621
|
+
log_debug("Search for memories complete", center=True)
|
|
622
|
+
|
|
623
|
+
memory_search: Optional[MemorySearchResponse] = None
|
|
624
|
+
# If the model natively supports structured outputs, the parsed value is already in the structured format
|
|
625
|
+
if (
|
|
626
|
+
model.supports_native_structured_outputs
|
|
627
|
+
and response.parsed is not None
|
|
628
|
+
and isinstance(response.parsed, MemorySearchResponse)
|
|
629
|
+
):
|
|
630
|
+
memory_search = response.parsed
|
|
631
|
+
|
|
632
|
+
# Otherwise convert the response to the structured format
|
|
633
|
+
if isinstance(response.content, str):
|
|
634
|
+
try:
|
|
635
|
+
memory_search = parse_response_model_str(response.content, MemorySearchResponse) # type: ignore
|
|
636
|
+
|
|
637
|
+
# Update RunOutput
|
|
638
|
+
if memory_search is None:
|
|
639
|
+
log_warning("Failed to convert memory_search response to MemorySearchResponse")
|
|
640
|
+
return []
|
|
641
|
+
except Exception as e:
|
|
642
|
+
log_warning(f"Failed to convert memory_search response to MemorySearchResponse: {e}")
|
|
643
|
+
return []
|
|
644
|
+
|
|
645
|
+
memories_to_return = []
|
|
646
|
+
if memory_search:
|
|
647
|
+
for memory_id in memory_search.memory_ids:
|
|
648
|
+
for memory in user_memories:
|
|
649
|
+
if memory.memory_id == memory_id:
|
|
650
|
+
memories_to_return.append(memory)
|
|
651
|
+
return memories_to_return[:limit]
|
|
652
|
+
|
|
653
|
+
def _get_last_n_memories(self, user_id: str, limit: Optional[int] = None) -> List[UserMemory]:
|
|
654
|
+
"""Get the most recent user memories.
|
|
655
|
+
|
|
656
|
+
Args:
|
|
657
|
+
limit: Maximum number of memories to return.
|
|
658
|
+
|
|
659
|
+
Returns:
|
|
660
|
+
A list of the most recent UserMemory objects.
|
|
661
|
+
"""
|
|
662
|
+
memories = self.read_from_db(user_id=user_id)
|
|
663
|
+
if memories is None:
|
|
664
|
+
memories = {}
|
|
665
|
+
|
|
666
|
+
memories_list = memories.get(user_id, [])
|
|
667
|
+
|
|
668
|
+
# Sort memories by updated_at timestamp if available
|
|
669
|
+
if memories_list:
|
|
670
|
+
# Sort memories by updated_at timestamp (newest first)
|
|
671
|
+
# If updated_at is None, place at the beginning of the list
|
|
672
|
+
sorted_memories_list = sorted(
|
|
673
|
+
memories_list,
|
|
674
|
+
key=lambda memory: memory.updated_at or datetime.min,
|
|
675
|
+
)
|
|
676
|
+
else:
|
|
677
|
+
sorted_memories_list = []
|
|
678
|
+
|
|
679
|
+
if limit is not None and limit > 0:
|
|
680
|
+
sorted_memories_list = sorted_memories_list[-limit:]
|
|
681
|
+
|
|
682
|
+
return sorted_memories_list
|
|
683
|
+
|
|
684
|
+
def _get_first_n_memories(self, user_id: str, limit: Optional[int] = None) -> List[UserMemory]:
|
|
685
|
+
"""Get the oldest user memories.
|
|
686
|
+
|
|
687
|
+
Args:
|
|
688
|
+
limit: Maximum number of memories to return.
|
|
689
|
+
|
|
690
|
+
Returns:
|
|
691
|
+
A list of the oldest UserMemory objects.
|
|
692
|
+
"""
|
|
693
|
+
memories = self.read_from_db(user_id=user_id)
|
|
694
|
+
if memories is None:
|
|
695
|
+
memories = {}
|
|
696
|
+
|
|
697
|
+
memories_list = memories.get(user_id, [])
|
|
698
|
+
# Sort memories by updated_at timestamp if available
|
|
699
|
+
if memories_list:
|
|
700
|
+
# Sort memories by updated_at timestamp (oldest first)
|
|
701
|
+
# If updated_at is None, place at the end of the list
|
|
702
|
+
sorted_memories_list = sorted(
|
|
703
|
+
memories_list,
|
|
704
|
+
key=lambda memory: memory.updated_at or datetime.max,
|
|
705
|
+
)
|
|
706
|
+
|
|
707
|
+
else:
|
|
708
|
+
sorted_memories_list = []
|
|
709
|
+
|
|
710
|
+
if limit is not None and limit > 0:
|
|
711
|
+
sorted_memories_list = sorted_memories_list[:limit]
|
|
712
|
+
|
|
713
|
+
return sorted_memories_list
|
|
714
|
+
|
|
715
|
+
# --Memory Manager Functions--
|
|
716
|
+
def determine_tools_for_model(self, tools: List[Callable]) -> List[Union[Function, dict]]:
|
|
717
|
+
# Have to reset each time, because of different user IDs
|
|
718
|
+
_function_names = []
|
|
719
|
+
_functions: List[Union[Function, dict]] = []
|
|
720
|
+
|
|
721
|
+
for tool in tools:
|
|
722
|
+
try:
|
|
723
|
+
function_name = tool.__name__
|
|
724
|
+
if function_name in _function_names:
|
|
725
|
+
continue
|
|
726
|
+
_function_names.append(function_name)
|
|
727
|
+
func = Function.from_callable(tool, strict=True) # type: ignore
|
|
728
|
+
func.strict = True
|
|
729
|
+
_functions.append(func)
|
|
730
|
+
log_debug(f"Added function {func.name}")
|
|
731
|
+
except Exception as e:
|
|
732
|
+
log_warning(f"Could not add function {tool}: {e}")
|
|
733
|
+
|
|
734
|
+
return _functions
|
|
735
|
+
|
|
736
|
+
def get_system_message(
|
|
737
|
+
self,
|
|
738
|
+
existing_memories: Optional[List[Dict[str, Any]]] = None,
|
|
739
|
+
enable_delete_memory: bool = True,
|
|
740
|
+
enable_clear_memory: bool = True,
|
|
741
|
+
enable_update_memory: bool = True,
|
|
742
|
+
enable_add_memory: bool = True,
|
|
743
|
+
) -> Message:
|
|
744
|
+
if self.system_message is not None:
|
|
745
|
+
return Message(role="system", content=self.system_message)
|
|
746
|
+
|
|
747
|
+
memory_capture_instructions = self.memory_capture_instructions or dedent(
|
|
748
|
+
"""\
|
|
749
|
+
Memories should capture personal information about the user that is relevant to the current conversation, such as:
|
|
750
|
+
- Personal facts: name, age, occupation, location, interests, and preferences
|
|
751
|
+
- Opinions and preferences: what the user likes, dislikes, enjoys, or finds frustrating
|
|
752
|
+
- Significant life events or experiences shared by the user
|
|
753
|
+
- Important context about the user's current situation, challenges, or goals
|
|
754
|
+
- Any other details that offer meaningful insight into the user's personality, perspective, or needs
|
|
755
|
+
"""
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
# -*- Return a system message for the memory manager
|
|
759
|
+
system_prompt_lines = [
|
|
760
|
+
"You are a Memory Manager that is responsible for managing information and preferences about the user. "
|
|
761
|
+
"You will be provided with a criteria for memories to capture in the <memories_to_capture> section and a list of existing memories in the <existing_memories> section.",
|
|
762
|
+
"",
|
|
763
|
+
"## When to add or update memories",
|
|
764
|
+
"- Your first task is to decide if a memory needs to be added, updated, or deleted based on the user's message OR if no changes are needed.",
|
|
765
|
+
"- If the user's message meets the criteria in the <memories_to_capture> section and that information is not already captured in the <existing_memories> section, you should capture it as a memory.",
|
|
766
|
+
"- If the users messages does not meet the criteria in the <memories_to_capture> section, no memory updates are needed.",
|
|
767
|
+
"- If the existing memories in the <existing_memories> section capture all relevant information, no memory updates are needed.",
|
|
768
|
+
"",
|
|
769
|
+
"## How to add or update memories",
|
|
770
|
+
"- If you decide to add a new memory, create memories that captures key information, as if you were storing it for future reference.",
|
|
771
|
+
"- Memories should be a brief, third-person statements that encapsulate the most important aspect of the user's input, without adding any extraneous information.",
|
|
772
|
+
" - Example: If the user's message is 'I'm going to the gym', a memory could be `John Doe goes to the gym regularly`.",
|
|
773
|
+
" - Example: If the user's message is 'My name is John Doe', a memory could be `User's name is John Doe`.",
|
|
774
|
+
"- Don't make a single memory too long or complex, create multiple memories if needed to capture all the information.",
|
|
775
|
+
"- Don't repeat the same information in multiple memories. Rather update existing memories if needed.",
|
|
776
|
+
"- If a user asks for a memory to be updated or forgotten, remove all reference to the information that should be forgotten. Don't say 'The user used to like ...`",
|
|
777
|
+
"- When updating a memory, append the existing memory with new information rather than completely overwriting it.",
|
|
778
|
+
"- When a user's preferences change, update the relevant memories to reflect the new preferences but also capture what the user's preferences used to be and what has changed.",
|
|
779
|
+
"",
|
|
780
|
+
"## Criteria for creating memories",
|
|
781
|
+
"Use the following criteria to determine if a user's message should be captured as a memory.",
|
|
782
|
+
"",
|
|
783
|
+
"<memories_to_capture>",
|
|
784
|
+
memory_capture_instructions,
|
|
785
|
+
"</memories_to_capture>",
|
|
786
|
+
"",
|
|
787
|
+
"## Updating memories",
|
|
788
|
+
"You will also be provided with a list of existing memories in the <existing_memories> section. You can:",
|
|
789
|
+
" - Decide to make no changes.",
|
|
790
|
+
]
|
|
791
|
+
if enable_add_memory:
|
|
792
|
+
system_prompt_lines.append(" - Decide to add a new memory, using the `add_memory` tool.")
|
|
793
|
+
if enable_update_memory:
|
|
794
|
+
system_prompt_lines.append(" - Decide to update an existing memory, using the `update_memory` tool.")
|
|
795
|
+
if enable_delete_memory:
|
|
796
|
+
system_prompt_lines.append(" - Decide to delete an existing memory, using the `delete_memory` tool.")
|
|
797
|
+
if enable_clear_memory:
|
|
798
|
+
system_prompt_lines.append(" - Decide to clear all memories, using the `clear_memory` tool.")
|
|
799
|
+
|
|
800
|
+
system_prompt_lines += [
|
|
801
|
+
"You can call multiple tools in a single response if needed. ",
|
|
802
|
+
"Only add or update memories if it is necessary to capture key information provided by the user.",
|
|
803
|
+
]
|
|
804
|
+
|
|
805
|
+
if existing_memories and len(existing_memories) > 0:
|
|
806
|
+
system_prompt_lines.append("\n<existing_memories>")
|
|
807
|
+
for existing_memory in existing_memories:
|
|
808
|
+
system_prompt_lines.append(f"ID: {existing_memory['memory_id']}")
|
|
809
|
+
system_prompt_lines.append(f"Memory: {existing_memory['memory']}")
|
|
810
|
+
system_prompt_lines.append("")
|
|
811
|
+
system_prompt_lines.append("</existing_memories>")
|
|
812
|
+
|
|
813
|
+
if self.additional_instructions:
|
|
814
|
+
system_prompt_lines.append(self.additional_instructions)
|
|
815
|
+
|
|
816
|
+
return Message(role="system", content="\n".join(system_prompt_lines))
|
|
817
|
+
|
|
818
|
+
def create_or_update_memories(
|
|
819
|
+
self,
|
|
820
|
+
messages: List[Message],
|
|
821
|
+
existing_memories: List[Dict[str, Any]],
|
|
822
|
+
user_id: str,
|
|
823
|
+
db: BaseDb,
|
|
824
|
+
agent_id: Optional[str] = None,
|
|
825
|
+
team_id: Optional[str] = None,
|
|
826
|
+
update_memories: bool = True,
|
|
827
|
+
add_memories: bool = True,
|
|
828
|
+
) -> str:
|
|
829
|
+
if self.model is None:
|
|
830
|
+
log_error("No model provided for memory manager")
|
|
831
|
+
return "No model provided for memory manager"
|
|
832
|
+
|
|
833
|
+
log_debug("MemoryManager Start", center=True)
|
|
834
|
+
|
|
835
|
+
if len(messages) == 1:
|
|
836
|
+
input_string = messages[0].get_content_string()
|
|
837
|
+
else:
|
|
838
|
+
input_string = f"{', '.join([m.get_content_string() for m in messages if m.role == 'user' and m.content])}"
|
|
839
|
+
|
|
840
|
+
model_copy = deepcopy(self.model)
|
|
841
|
+
# Update the Model (set defaults, add logit etc.)
|
|
842
|
+
_tools = self.determine_tools_for_model(
|
|
843
|
+
self._get_db_tools(
|
|
844
|
+
user_id,
|
|
845
|
+
db,
|
|
846
|
+
input_string,
|
|
847
|
+
agent_id=agent_id,
|
|
848
|
+
team_id=team_id,
|
|
849
|
+
enable_add_memory=add_memories,
|
|
850
|
+
enable_update_memory=update_memories,
|
|
851
|
+
enable_delete_memory=True,
|
|
852
|
+
enable_clear_memory=False,
|
|
853
|
+
),
|
|
854
|
+
)
|
|
855
|
+
|
|
856
|
+
# Prepare the List of messages to send to the Model
|
|
857
|
+
messages_for_model: List[Message] = [
|
|
858
|
+
self.get_system_message(
|
|
859
|
+
existing_memories=existing_memories,
|
|
860
|
+
enable_update_memory=update_memories,
|
|
861
|
+
enable_add_memory=add_memories,
|
|
862
|
+
enable_delete_memory=True,
|
|
863
|
+
enable_clear_memory=False,
|
|
864
|
+
),
|
|
865
|
+
*messages,
|
|
866
|
+
]
|
|
867
|
+
|
|
868
|
+
# Generate a response from the Model (includes running function calls)
|
|
869
|
+
response = model_copy.response(
|
|
870
|
+
messages=messages_for_model,
|
|
871
|
+
tools=_tools,
|
|
872
|
+
)
|
|
873
|
+
|
|
874
|
+
if response.tool_calls is not None and len(response.tool_calls) > 0:
|
|
875
|
+
self.memories_updated = True
|
|
876
|
+
log_debug("MemoryManager End", center=True)
|
|
877
|
+
|
|
878
|
+
return response.content or "No response from model"
|
|
879
|
+
|
|
880
|
+
async def acreate_or_update_memories(
|
|
881
|
+
self,
|
|
882
|
+
messages: List[Message],
|
|
883
|
+
existing_memories: List[Dict[str, Any]],
|
|
884
|
+
user_id: str,
|
|
885
|
+
db: Union[BaseDb, AsyncBaseDb],
|
|
886
|
+
agent_id: Optional[str] = None,
|
|
887
|
+
team_id: Optional[str] = None,
|
|
888
|
+
update_memories: bool = True,
|
|
889
|
+
add_memories: bool = True,
|
|
890
|
+
) -> str:
|
|
891
|
+
if self.model is None:
|
|
892
|
+
log_error("No model provided for memory manager")
|
|
893
|
+
return "No model provided for memory manager"
|
|
894
|
+
|
|
895
|
+
log_debug("MemoryManager Start", center=True)
|
|
896
|
+
|
|
897
|
+
if len(messages) == 1:
|
|
898
|
+
input_string = messages[0].get_content_string()
|
|
899
|
+
else:
|
|
900
|
+
input_string = f"{', '.join([m.get_content_string() for m in messages if m.role == 'user' and m.content])}"
|
|
901
|
+
|
|
902
|
+
model_copy = deepcopy(self.model)
|
|
903
|
+
# Update the Model (set defaults, add logit etc.)
|
|
904
|
+
if isinstance(db, AsyncBaseDb):
|
|
905
|
+
_tools = self.determine_tools_for_model(
|
|
906
|
+
await self._aget_db_tools(
|
|
907
|
+
user_id,
|
|
908
|
+
db,
|
|
909
|
+
input_string,
|
|
910
|
+
agent_id=agent_id,
|
|
911
|
+
team_id=team_id,
|
|
912
|
+
enable_add_memory=add_memories,
|
|
913
|
+
enable_update_memory=update_memories,
|
|
914
|
+
enable_delete_memory=True,
|
|
915
|
+
enable_clear_memory=False,
|
|
916
|
+
),
|
|
917
|
+
)
|
|
918
|
+
else:
|
|
919
|
+
_tools = self.determine_tools_for_model(
|
|
920
|
+
self._get_db_tools(
|
|
921
|
+
user_id,
|
|
922
|
+
db,
|
|
923
|
+
input_string,
|
|
924
|
+
agent_id=agent_id,
|
|
925
|
+
team_id=team_id,
|
|
926
|
+
enable_add_memory=add_memories,
|
|
927
|
+
enable_update_memory=update_memories,
|
|
928
|
+
enable_delete_memory=True,
|
|
929
|
+
enable_clear_memory=False,
|
|
930
|
+
),
|
|
931
|
+
)
|
|
932
|
+
|
|
933
|
+
# Prepare the List of messages to send to the Model
|
|
934
|
+
messages_for_model: List[Message] = [
|
|
935
|
+
self.get_system_message(
|
|
936
|
+
existing_memories=existing_memories,
|
|
937
|
+
enable_update_memory=update_memories,
|
|
938
|
+
enable_add_memory=add_memories,
|
|
939
|
+
enable_delete_memory=True,
|
|
940
|
+
enable_clear_memory=False,
|
|
941
|
+
),
|
|
942
|
+
*messages,
|
|
943
|
+
]
|
|
944
|
+
|
|
945
|
+
# Generate a response from the Model (includes running function calls)
|
|
946
|
+
response = await model_copy.aresponse(
|
|
947
|
+
messages=messages_for_model,
|
|
948
|
+
tools=_tools,
|
|
949
|
+
)
|
|
950
|
+
|
|
951
|
+
if response.tool_calls is not None and len(response.tool_calls) > 0:
|
|
952
|
+
self.memories_updated = True
|
|
953
|
+
log_debug("MemoryManager End", center=True)
|
|
954
|
+
|
|
955
|
+
return response.content or "No response from model"
|
|
956
|
+
|
|
957
|
+
def run_memory_task(
|
|
958
|
+
self,
|
|
959
|
+
task: str,
|
|
960
|
+
existing_memories: List[Dict[str, Any]],
|
|
961
|
+
user_id: str,
|
|
962
|
+
db: BaseDb,
|
|
963
|
+
delete_memories: bool = True,
|
|
964
|
+
update_memories: bool = True,
|
|
965
|
+
add_memories: bool = True,
|
|
966
|
+
clear_memories: bool = True,
|
|
967
|
+
) -> str:
|
|
968
|
+
if self.model is None:
|
|
969
|
+
log_error("No model provided for memory manager")
|
|
970
|
+
return "No model provided for memory manager"
|
|
971
|
+
|
|
972
|
+
log_debug("MemoryManager Start", center=True)
|
|
973
|
+
|
|
974
|
+
model_copy = deepcopy(self.model)
|
|
975
|
+
# Update the Model (set defaults, add logit etc.)
|
|
976
|
+
_tools = self.determine_tools_for_model(
|
|
977
|
+
self._get_db_tools(
|
|
978
|
+
user_id,
|
|
979
|
+
db,
|
|
980
|
+
task,
|
|
981
|
+
enable_delete_memory=delete_memories,
|
|
982
|
+
enable_clear_memory=clear_memories,
|
|
983
|
+
enable_update_memory=update_memories,
|
|
984
|
+
enable_add_memory=add_memories,
|
|
985
|
+
),
|
|
986
|
+
)
|
|
987
|
+
|
|
988
|
+
# Prepare the List of messages to send to the Model
|
|
989
|
+
messages_for_model: List[Message] = [
|
|
990
|
+
self.get_system_message(
|
|
991
|
+
existing_memories,
|
|
992
|
+
enable_delete_memory=delete_memories,
|
|
993
|
+
enable_clear_memory=clear_memories,
|
|
994
|
+
enable_update_memory=update_memories,
|
|
995
|
+
enable_add_memory=add_memories,
|
|
996
|
+
),
|
|
997
|
+
# For models that require a non-system message
|
|
998
|
+
Message(role="user", content=task),
|
|
999
|
+
]
|
|
1000
|
+
|
|
1001
|
+
# Generate a response from the Model (includes running function calls)
|
|
1002
|
+
response = model_copy.response(
|
|
1003
|
+
messages=messages_for_model,
|
|
1004
|
+
tools=_tools,
|
|
1005
|
+
)
|
|
1006
|
+
|
|
1007
|
+
if response.tool_calls is not None and len(response.tool_calls) > 0:
|
|
1008
|
+
self.memories_updated = True
|
|
1009
|
+
log_debug("MemoryManager End", center=True)
|
|
1010
|
+
|
|
1011
|
+
return response.content or "No response from model"
|
|
1012
|
+
|
|
1013
|
+
async def arun_memory_task(
|
|
1014
|
+
self,
|
|
1015
|
+
task: str,
|
|
1016
|
+
existing_memories: List[Dict[str, Any]],
|
|
1017
|
+
user_id: str,
|
|
1018
|
+
db: Union[BaseDb, AsyncBaseDb],
|
|
1019
|
+
delete_memories: bool = True,
|
|
1020
|
+
clear_memories: bool = True,
|
|
1021
|
+
update_memories: bool = True,
|
|
1022
|
+
add_memories: bool = True,
|
|
1023
|
+
) -> str:
|
|
1024
|
+
if self.model is None:
|
|
1025
|
+
log_error("No model provided for memory manager")
|
|
1026
|
+
return "No model provided for memory manager"
|
|
1027
|
+
|
|
1028
|
+
log_debug("MemoryManager Start", center=True)
|
|
1029
|
+
|
|
1030
|
+
model_copy = deepcopy(self.model)
|
|
1031
|
+
# Update the Model (set defaults, add logit etc.)
|
|
1032
|
+
if isinstance(db, AsyncBaseDb):
|
|
1033
|
+
_tools = self.determine_tools_for_model(
|
|
1034
|
+
await self._aget_db_tools(
|
|
1035
|
+
user_id,
|
|
1036
|
+
db,
|
|
1037
|
+
task,
|
|
1038
|
+
enable_delete_memory=delete_memories,
|
|
1039
|
+
enable_clear_memory=clear_memories,
|
|
1040
|
+
enable_update_memory=update_memories,
|
|
1041
|
+
enable_add_memory=add_memories,
|
|
1042
|
+
),
|
|
1043
|
+
)
|
|
1044
|
+
else:
|
|
1045
|
+
_tools = self.determine_tools_for_model(
|
|
1046
|
+
self._get_db_tools(
|
|
1047
|
+
user_id,
|
|
1048
|
+
db,
|
|
1049
|
+
task,
|
|
1050
|
+
enable_delete_memory=delete_memories,
|
|
1051
|
+
enable_clear_memory=clear_memories,
|
|
1052
|
+
enable_update_memory=update_memories,
|
|
1053
|
+
enable_add_memory=add_memories,
|
|
1054
|
+
),
|
|
1055
|
+
)
|
|
1056
|
+
|
|
1057
|
+
# Prepare the List of messages to send to the Model
|
|
1058
|
+
messages_for_model: List[Message] = [
|
|
1059
|
+
self.get_system_message(
|
|
1060
|
+
existing_memories,
|
|
1061
|
+
enable_delete_memory=delete_memories,
|
|
1062
|
+
enable_clear_memory=clear_memories,
|
|
1063
|
+
enable_update_memory=update_memories,
|
|
1064
|
+
enable_add_memory=add_memories,
|
|
1065
|
+
),
|
|
1066
|
+
# For models that require a non-system message
|
|
1067
|
+
Message(role="user", content=task),
|
|
1068
|
+
]
|
|
1069
|
+
|
|
1070
|
+
# Generate a response from the Model (includes running function calls)
|
|
1071
|
+
response = await model_copy.aresponse(
|
|
1072
|
+
messages=messages_for_model,
|
|
1073
|
+
tools=_tools,
|
|
1074
|
+
)
|
|
1075
|
+
|
|
1076
|
+
if response.tool_calls is not None and len(response.tool_calls) > 0:
|
|
1077
|
+
self.memories_updated = True
|
|
1078
|
+
log_debug("MemoryManager End", center=True)
|
|
1079
|
+
|
|
1080
|
+
return response.content or "No response from model"
|
|
1081
|
+
|
|
1082
|
+
# -*- DB Functions
|
|
1083
|
+
def _get_db_tools(
|
|
1084
|
+
self,
|
|
1085
|
+
user_id: str,
|
|
1086
|
+
db: BaseDb,
|
|
1087
|
+
input_string: str,
|
|
1088
|
+
enable_add_memory: bool = True,
|
|
1089
|
+
enable_update_memory: bool = True,
|
|
1090
|
+
enable_delete_memory: bool = True,
|
|
1091
|
+
enable_clear_memory: bool = True,
|
|
1092
|
+
agent_id: Optional[str] = None,
|
|
1093
|
+
team_id: Optional[str] = None,
|
|
1094
|
+
) -> List[Callable]:
|
|
1095
|
+
def add_memory(memory: str, topics: Optional[List[str]] = None) -> str:
|
|
1096
|
+
"""Use this function to add a memory to the database.
|
|
1097
|
+
Args:
|
|
1098
|
+
memory (str): The memory to be added.
|
|
1099
|
+
topics (Optional[List[str]]): The topics of the memory (e.g. ["name", "hobbies", "location"]).
|
|
1100
|
+
Returns:
|
|
1101
|
+
str: A message indicating if the memory was added successfully or not.
|
|
1102
|
+
"""
|
|
1103
|
+
from uuid import uuid4
|
|
1104
|
+
|
|
1105
|
+
from agno.db.base import UserMemory
|
|
1106
|
+
|
|
1107
|
+
try:
|
|
1108
|
+
memory_id = str(uuid4())
|
|
1109
|
+
db.upsert_user_memory(
|
|
1110
|
+
UserMemory(
|
|
1111
|
+
memory_id=memory_id,
|
|
1112
|
+
user_id=user_id,
|
|
1113
|
+
agent_id=agent_id,
|
|
1114
|
+
team_id=team_id,
|
|
1115
|
+
memory=memory,
|
|
1116
|
+
topics=topics,
|
|
1117
|
+
input=input_string,
|
|
1118
|
+
)
|
|
1119
|
+
)
|
|
1120
|
+
log_debug(f"Memory added: {memory_id}")
|
|
1121
|
+
return "Memory added successfully"
|
|
1122
|
+
except Exception as e:
|
|
1123
|
+
log_warning(f"Error storing memory in db: {e}")
|
|
1124
|
+
return f"Error adding memory: {e}"
|
|
1125
|
+
|
|
1126
|
+
def update_memory(memory_id: str, memory: str, topics: Optional[List[str]] = None) -> str:
|
|
1127
|
+
"""Use this function to update an existing memory in the database.
|
|
1128
|
+
Args:
|
|
1129
|
+
memory_id (str): The id of the memory to be updated.
|
|
1130
|
+
memory (str): The updated memory.
|
|
1131
|
+
topics (Optional[List[str]]): The topics of the memory (e.g. ["name", "hobbies", "location"]).
|
|
1132
|
+
Returns:
|
|
1133
|
+
str: A message indicating if the memory was updated successfully or not.
|
|
1134
|
+
"""
|
|
1135
|
+
from agno.db.base import UserMemory
|
|
1136
|
+
|
|
1137
|
+
if memory == "":
|
|
1138
|
+
return "Can't update memory with empty string. Use the delete memory function if available."
|
|
1139
|
+
|
|
1140
|
+
try:
|
|
1141
|
+
db.upsert_user_memory(
|
|
1142
|
+
UserMemory(
|
|
1143
|
+
memory_id=memory_id,
|
|
1144
|
+
memory=memory,
|
|
1145
|
+
topics=topics,
|
|
1146
|
+
user_id=user_id,
|
|
1147
|
+
input=input_string,
|
|
1148
|
+
)
|
|
1149
|
+
)
|
|
1150
|
+
log_debug("Memory updated")
|
|
1151
|
+
return "Memory updated successfully"
|
|
1152
|
+
except Exception as e:
|
|
1153
|
+
log_warning(f"Error storing memory in db: {e}")
|
|
1154
|
+
return f"Error adding memory: {e}"
|
|
1155
|
+
|
|
1156
|
+
def delete_memory(memory_id: str) -> str:
|
|
1157
|
+
"""Use this function to delete a single memory from the database.
|
|
1158
|
+
Args:
|
|
1159
|
+
memory_id (str): The id of the memory to be deleted.
|
|
1160
|
+
Returns:
|
|
1161
|
+
str: A message indicating if the memory was deleted successfully or not.
|
|
1162
|
+
"""
|
|
1163
|
+
try:
|
|
1164
|
+
db.delete_user_memory(memory_id=memory_id, user_id=user_id)
|
|
1165
|
+
log_debug("Memory deleted")
|
|
1166
|
+
return "Memory deleted successfully"
|
|
1167
|
+
except Exception as e:
|
|
1168
|
+
log_warning(f"Error deleting memory in db: {e}")
|
|
1169
|
+
return f"Error deleting memory: {e}"
|
|
1170
|
+
|
|
1171
|
+
def clear_memory() -> str:
|
|
1172
|
+
"""Use this function to remove all (or clear all) memories from the database.
|
|
1173
|
+
|
|
1174
|
+
Returns:
|
|
1175
|
+
str: A message indicating if the memory was cleared successfully or not.
|
|
1176
|
+
"""
|
|
1177
|
+
db.clear_memories()
|
|
1178
|
+
log_debug("Memory cleared")
|
|
1179
|
+
return "Memory cleared successfully"
|
|
1180
|
+
|
|
1181
|
+
functions: List[Callable] = []
|
|
1182
|
+
if enable_add_memory:
|
|
1183
|
+
functions.append(add_memory)
|
|
1184
|
+
if enable_update_memory:
|
|
1185
|
+
functions.append(update_memory)
|
|
1186
|
+
if enable_delete_memory:
|
|
1187
|
+
functions.append(delete_memory)
|
|
1188
|
+
if enable_clear_memory:
|
|
1189
|
+
functions.append(clear_memory)
|
|
1190
|
+
return functions
|
|
1191
|
+
|
|
1192
|
+
async def _aget_db_tools(
|
|
1193
|
+
self,
|
|
1194
|
+
user_id: str,
|
|
1195
|
+
db: Union[BaseDb, AsyncBaseDb],
|
|
1196
|
+
input_string: str,
|
|
1197
|
+
enable_add_memory: bool = True,
|
|
1198
|
+
enable_update_memory: bool = True,
|
|
1199
|
+
enable_delete_memory: bool = True,
|
|
1200
|
+
enable_clear_memory: bool = True,
|
|
1201
|
+
agent_id: Optional[str] = None,
|
|
1202
|
+
team_id: Optional[str] = None,
|
|
1203
|
+
) -> List[Callable]:
|
|
1204
|
+
async def add_memory(memory: str, topics: Optional[List[str]] = None) -> str:
|
|
1205
|
+
"""Use this function to add a memory to the database.
|
|
1206
|
+
Args:
|
|
1207
|
+
memory (str): The memory to be added.
|
|
1208
|
+
topics (Optional[List[str]]): The topics of the memory (e.g. ["name", "hobbies", "location"]).
|
|
1209
|
+
Returns:
|
|
1210
|
+
str: A message indicating if the memory was added successfully or not.
|
|
1211
|
+
"""
|
|
1212
|
+
from uuid import uuid4
|
|
1213
|
+
|
|
1214
|
+
from agno.db.base import UserMemory
|
|
1215
|
+
|
|
1216
|
+
try:
|
|
1217
|
+
memory_id = str(uuid4())
|
|
1218
|
+
if isinstance(db, AsyncBaseDb):
|
|
1219
|
+
await db.upsert_user_memory(
|
|
1220
|
+
UserMemory(
|
|
1221
|
+
memory_id=memory_id,
|
|
1222
|
+
user_id=user_id,
|
|
1223
|
+
agent_id=agent_id,
|
|
1224
|
+
team_id=team_id,
|
|
1225
|
+
memory=memory,
|
|
1226
|
+
topics=topics,
|
|
1227
|
+
input=input_string,
|
|
1228
|
+
)
|
|
1229
|
+
)
|
|
1230
|
+
else:
|
|
1231
|
+
db.upsert_user_memory(
|
|
1232
|
+
UserMemory(
|
|
1233
|
+
memory_id=memory_id,
|
|
1234
|
+
user_id=user_id,
|
|
1235
|
+
agent_id=agent_id,
|
|
1236
|
+
team_id=team_id,
|
|
1237
|
+
memory=memory,
|
|
1238
|
+
topics=topics,
|
|
1239
|
+
input=input_string,
|
|
1240
|
+
)
|
|
1241
|
+
)
|
|
1242
|
+
log_debug(f"Memory added: {memory_id}")
|
|
1243
|
+
return "Memory added successfully"
|
|
1244
|
+
except Exception as e:
|
|
1245
|
+
log_warning(f"Error storing memory in db: {e}")
|
|
1246
|
+
return f"Error adding memory: {e}"
|
|
1247
|
+
|
|
1248
|
+
async def update_memory(memory_id: str, memory: str, topics: Optional[List[str]] = None) -> str:
|
|
1249
|
+
"""Use this function to update an existing memory in the database.
|
|
1250
|
+
Args:
|
|
1251
|
+
memory_id (str): The id of the memory to be updated.
|
|
1252
|
+
memory (str): The updated memory.
|
|
1253
|
+
topics (Optional[List[str]]): The topics of the memory (e.g. ["name", "hobbies", "location"]).
|
|
1254
|
+
Returns:
|
|
1255
|
+
str: A message indicating if the memory was updated successfully or not.
|
|
1256
|
+
"""
|
|
1257
|
+
from agno.db.base import UserMemory
|
|
1258
|
+
|
|
1259
|
+
if memory == "":
|
|
1260
|
+
return "Can't update memory with empty string. Use the delete memory function if available."
|
|
1261
|
+
|
|
1262
|
+
try:
|
|
1263
|
+
if isinstance(db, AsyncBaseDb):
|
|
1264
|
+
await db.upsert_user_memory(
|
|
1265
|
+
UserMemory(
|
|
1266
|
+
memory_id=memory_id,
|
|
1267
|
+
memory=memory,
|
|
1268
|
+
topics=topics,
|
|
1269
|
+
input=input_string,
|
|
1270
|
+
)
|
|
1271
|
+
)
|
|
1272
|
+
else:
|
|
1273
|
+
db.upsert_user_memory(
|
|
1274
|
+
UserMemory(
|
|
1275
|
+
memory_id=memory_id,
|
|
1276
|
+
memory=memory,
|
|
1277
|
+
topics=topics,
|
|
1278
|
+
input=input_string,
|
|
1279
|
+
)
|
|
1280
|
+
)
|
|
1281
|
+
log_debug("Memory updated")
|
|
1282
|
+
return "Memory updated successfully"
|
|
1283
|
+
except Exception as e:
|
|
1284
|
+
log_warning(f"Error storing memory in db: {e}")
|
|
1285
|
+
return f"Error adding memory: {e}"
|
|
1286
|
+
|
|
1287
|
+
async def delete_memory(memory_id: str) -> str:
|
|
1288
|
+
"""Use this function to delete a single memory from the database.
|
|
1289
|
+
Args:
|
|
1290
|
+
memory_id (str): The id of the memory to be deleted.
|
|
1291
|
+
Returns:
|
|
1292
|
+
str: A message indicating if the memory was deleted successfully or not.
|
|
1293
|
+
"""
|
|
1294
|
+
try:
|
|
1295
|
+
if isinstance(db, AsyncBaseDb):
|
|
1296
|
+
await db.delete_user_memory(memory_id=memory_id)
|
|
1297
|
+
else:
|
|
1298
|
+
db.delete_user_memory(memory_id=memory_id)
|
|
1299
|
+
log_debug("Memory deleted")
|
|
1300
|
+
return "Memory deleted successfully"
|
|
1301
|
+
except Exception as e:
|
|
1302
|
+
log_warning(f"Error deleting memory in db: {e}")
|
|
1303
|
+
return f"Error deleting memory: {e}"
|
|
1304
|
+
|
|
1305
|
+
async def clear_memory() -> str:
|
|
1306
|
+
"""Use this function to remove all (or clear all) memories from the database.
|
|
1307
|
+
|
|
1308
|
+
Returns:
|
|
1309
|
+
str: A message indicating if the memory was cleared successfully or not.
|
|
1310
|
+
"""
|
|
1311
|
+
if isinstance(db, AsyncBaseDb):
|
|
1312
|
+
await db.clear_memories()
|
|
1313
|
+
else:
|
|
1314
|
+
db.clear_memories()
|
|
1315
|
+
log_debug("Memory cleared")
|
|
1316
|
+
return "Memory cleared successfully"
|
|
1317
|
+
|
|
1318
|
+
functions: List[Callable] = []
|
|
1319
|
+
if enable_add_memory:
|
|
1320
|
+
functions.append(add_memory)
|
|
1321
|
+
if enable_update_memory:
|
|
1322
|
+
functions.append(update_memory)
|
|
1323
|
+
if enable_delete_memory:
|
|
1324
|
+
functions.append(delete_memory)
|
|
1325
|
+
if enable_clear_memory:
|
|
1326
|
+
functions.append(clear_memory)
|
|
1327
|
+
return functions
|