Compare commits

...

2 Commits

Author SHA1 Message Date
46efa8d9ed feat(pyproject): add psycopg dependency for PostgreSQL support
- 在依赖项中添加psycopg[binary]以支持PostgreSQL
- 提高项目与PostgreSQL的兼容性和灵活性

 feat(test2): create pdf_agent CLI application

- 添加pdf_agent函数以实现PDF代理功能
- 允许用户通过命令行界面与代理交互
2025-03-31 03:38:50 +08:00
6d25886286 feat(agent): 增加智能体存储和记忆功能
- 引入SqliteAgentStorage和SqliteMemoryDb来实现智能体的持久化存储
- 优化智能体的记忆管理,以支持用户记忆和会话摘要功能
2025-03-31 03:06:38 +08:00
4 changed files with 120 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ dependencies = [
"numpy>=2.2.4",
"openai>=1.69.0",
"pandas>=2.2.3",
"psycopg[binary]>=3.2.6",
"pypdf>=5.4.0",
"python-dotenv>=1.1.0",
"sqlalchemy>=2.0.40",

48
test2.py Normal file
View File

@@ -0,0 +1,48 @@
import typer
from typing import Optional, List
from agno.agent import Agent
from agno.storage.sqlite import SqliteStorage
from agno.storage.postgres import PostgresStorage
storage =SqliteStorage(table_name="agent_sessions", db_file="tmp/agent_storage.db")
# db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
# storage = PostgresStorage(table_name="pdf_agent", db_url=db_url)
def pdf_agent(new: bool = False, user: str = "user"):
session_id: Optional[str] = None
if not new:
existing_sessions: List[str] = storage.get_all_session_ids(user)
if len(existing_sessions) > 0:
session_id = existing_sessions[0]
agent = Agent(
session_id=session_id,
user_id=user,
#knowledge=knowledge_base,
storage=storage,
# Show tool calls in the response
show_tool_calls=True,
# Enable the agent to read the chat history
read_chat_history=True,
# We can also automatically add the chat history to the messages sent to the model
# But giving the model the chat history is not always useful, so we give it a tool instead
# to only use when needed.
# add_history_to_messages=True,
# Number of historical responses to add to the messages.
# num_history_responses=3,
)
if session_id is None:
session_id = agent.session_id
print(f"Started Session: {session_id}\n")
else:
print(f"Continuing Session: {session_id}\n")
# Runs the agent as a cli app
agent.cli_app(markdown=True)
if __name__ == "__main__":
# Load the knowledge base: Comment after first run
#knowledge_base.load(upsert=True)
typer.run(pdf_agent)

49
uv.lock generated
View File

@@ -39,6 +39,7 @@ dependencies = [
{ name = "numpy" },
{ name = "openai" },
{ name = "pandas" },
{ name = "psycopg", extra = ["binary"] },
{ name = "pypdf" },
{ name = "python-dotenv" },
{ name = "sqlalchemy" },
@@ -56,6 +57,7 @@ requires-dist = [
{ name = "numpy", specifier = ">=2.2.4" },
{ name = "openai", specifier = ">=1.69.0" },
{ name = "pandas", specifier = ">=2.2.3" },
{ name = "psycopg", extras = ["binary"], specifier = ">=3.2.6" },
{ name = "pypdf", specifier = ">=5.4.0" },
{ name = "python-dotenv", specifier = ">=1.1.0" },
{ name = "sqlalchemy", specifier = ">=2.0.40" },
@@ -1300,6 +1302,53 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/50/1b/6921afe68c74868b4c9fa424dad3be35b095e16687989ebbb50ce4fceb7c/psutil-7.0.0-cp37-abi3-win_amd64.whl", hash = "sha256:4cf3d4eb1aa9b348dec30105c55cd9b7d4629285735a102beb4441e38db90553", size = 244885 },
]
[[package]]
name = "psycopg"
version = "3.2.6"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "typing-extensions", marker = "python_full_version < '3.13'" },
{ name = "tzdata", marker = "sys_platform == 'win32'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/67/97/eea08f74f1c6dd2a02ee81b4ebfe5b558beb468ebbd11031adbf58d31be0/psycopg-3.2.6.tar.gz", hash = "sha256:16fa094efa2698f260f2af74f3710f781e4a6f226efe9d1fd0c37f384639ed8a", size = 156322 }
wheels = [
{ url = "https://files.pythonhosted.org/packages/d7/7d/0ba52deff71f65df8ec8038adad86ba09368c945424a9bd8145d679a2c6a/psycopg-3.2.6-py3-none-any.whl", hash = "sha256:f3ff5488525890abb0566c429146add66b329e20d6d4835662b920cbbf90ac58", size = 199077 },
]
[package.optional-dependencies]
binary = [
{ name = "psycopg-binary", marker = "implementation_name != 'pypy'" },
]
[[package]]
name = "psycopg-binary"
version = "3.2.6"
source = { registry = "https://pypi.org/simple" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/a3/c7/220b1273f0befb2cd9fe83d379b3484ae029a88798a90bc0d36f10bea5df/psycopg_binary-3.2.6-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:f27a46ff0497e882e8c0286e8833c785b4d1a80f23e1bf606f4c90e5f9f3ce75", size = 3857986 },
{ url = "https://files.pythonhosted.org/packages/8a/d8/30176532826cf87c608a6f79dd668bf9aff0cdf8eb80209eddf4c5aa7229/psycopg_binary-3.2.6-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:b30ee4821ded7de48b8048b14952512588e7c5477b0a5965221e1798afba61a1", size = 3940060 },
{ url = "https://files.pythonhosted.org/packages/54/7c/fa7cd1f057f33f7ae483d6bc5a03ec6eff111f8aa5c678d9aaef92705247/psycopg_binary-3.2.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e57edf3b1f5427f39660225b01f8e7b97f5cfab132092f014bf1638bc85d81d2", size = 4499082 },
{ url = "https://files.pythonhosted.org/packages/b8/81/1606966f6146187c273993ea6f88f2151b26741df8f4e01349a625983be9/psycopg_binary-3.2.6-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6c5172ce3e4ae7a4fd450070210f801e2ce6bc0f11d1208d29268deb0cda34de", size = 4307509 },
{ url = "https://files.pythonhosted.org/packages/69/ad/01c87aab17a4b89128b8036800d11ab296c7c2c623940cc7e6f2668f375a/psycopg_binary-3.2.6-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:bcfab3804c43571a6615e559cdc4c4115785d258a4dd71a721be033f5f5f378d", size = 4547813 },
{ url = "https://files.pythonhosted.org/packages/65/30/f93a193846ee738ffe5d2a4837e7ddeb7279707af81d088cee96cae853a0/psycopg_binary-3.2.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8fa1c920cce16f1205f37b20c685c58b9656b170b8b4c93629100d342d0d118e", size = 4259847 },
{ url = "https://files.pythonhosted.org/packages/8e/73/65c4ae71be86675a62154407c92af4b917146f9ff3baaf0e4166c0734aeb/psycopg_binary-3.2.6-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:2e118d818101c1608c6b5ba52a6c977614d8f05aa89467501172ba4d10588e11", size = 3846550 },
{ url = "https://files.pythonhosted.org/packages/53/cc/a24626cac3f208c776bb22e15e9a5e483aa81145221e6427e50381f40811/psycopg_binary-3.2.6-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:763319a8bfeca77d31512da71f5a33459b9568a7621c481c3828c62f9c38f351", size = 3320269 },
{ url = "https://files.pythonhosted.org/packages/55/e6/68c76fb9d6c53d5e4170a0c9216c7aa6c2903808f626d84d002b47a16931/psycopg_binary-3.2.6-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:2fbc05819560389dbece046966bc88e0f2ea77673497e274c4293b8b4c1d0703", size = 3399365 },
{ url = "https://files.pythonhosted.org/packages/b4/2c/55b140f5a2c582dae42ef38502c45ef69c938274242a40bd04c143081029/psycopg_binary-3.2.6-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:5a57f99bb953b4bd6f32d0a9844664e7f6ca5ead9ba40e96635be3cd30794813", size = 3438908 },
{ url = "https://files.pythonhosted.org/packages/ae/f6/589c95cceccee2ab408b6b2e16f1ed6db4536fb24f2f5c9ce568cf43270c/psycopg_binary-3.2.6-cp312-cp312-win_amd64.whl", hash = "sha256:5de6809e19a465dcb9c269675bded46a135f2d600cd99f0735afbb21ddad2af4", size = 2782886 },
{ url = "https://files.pythonhosted.org/packages/bf/32/3d06c478fd3070ac25a49c2e8ca46b6d76b0048fa9fa255b99ee32f32312/psycopg_binary-3.2.6-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:54af3fbf871baa2eb19df96fd7dc0cbd88e628a692063c3d1ab5cdd00aa04322", size = 3852672 },
{ url = "https://files.pythonhosted.org/packages/34/97/e581030e279500ede3096adb510f0e6071874b97cfc047a9a87b7d71fc77/psycopg_binary-3.2.6-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:ad5da1e4636776c21eaeacdec42f25fa4612631a12f25cd9ab34ddf2c346ffb9", size = 3936562 },
{ url = "https://files.pythonhosted.org/packages/74/b6/6a8df4cb23c3d327403a83406c06c9140f311cb56c4e4d720ee7abf6fddc/psycopg_binary-3.2.6-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f7956b9ea56f79cd86eddcfbfc65ae2af1e4fe7932fa400755005d903c709370", size = 4499167 },
{ url = "https://files.pythonhosted.org/packages/e4/5b/950eafef61e5e0b8ddb5afc5b6b279756411aa4bf70a346a6f091ad679bb/psycopg_binary-3.2.6-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1e2efb763188008cf2914820dcb9fb23c10fe2be0d2c97ef0fac7cec28e281d8", size = 4311651 },
{ url = "https://files.pythonhosted.org/packages/72/b9/b366c49afc854c26b3053d4d35376046eea9aebdc48ded18ea249ea1f80c/psycopg_binary-3.2.6-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4b3aab3451679f1e7932270e950259ed48c3b79390022d3f660491c0e65e4838", size = 4547852 },
{ url = "https://files.pythonhosted.org/packages/ab/d4/0e047360e2ea387dc7171ca017ffcee5214a0762f74b9dd982035f2e52fb/psycopg_binary-3.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:849a370ac4e125f55f2ad37f928e588291a67ccf91fa33d0b1e042bb3ee1f986", size = 4261725 },
{ url = "https://files.pythonhosted.org/packages/e3/ea/a1b969804250183900959ebe845d86be7fed2cbd9be58f64cd0fc24b2892/psycopg_binary-3.2.6-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:566d4ace928419d91f1eb3227fc9ef7b41cf0ad22e93dd2c3368d693cf144408", size = 3850073 },
{ url = "https://files.pythonhosted.org/packages/e5/71/ec2907342f0675092b76aea74365b56f38d960c4c635984dcfe25d8178c8/psycopg_binary-3.2.6-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:f1981f13b10de2f11cfa2f99a8738b35b3f0a0f3075861446894a8d3042430c0", size = 3320323 },
{ url = "https://files.pythonhosted.org/packages/d7/d7/0d2cb4b42f231e2efe8ea1799ce917973d47486212a2c4d33cd331e7ac28/psycopg_binary-3.2.6-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:36f598300b55b3c983ae8df06473ad27333d2fd9f3e2cfdb913b3a5aaa3a8bcf", size = 3402335 },
{ url = "https://files.pythonhosted.org/packages/66/92/7050c372f78e53eba14695cec6c3a91b2d9ca56feaf0bfe95fe90facf730/psycopg_binary-3.2.6-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:0f4699fa5fe1fffb0d6b2d14b31fd8c29b7ea7375f89d5989f002aaf21728b21", size = 3440442 },
{ url = "https://files.pythonhosted.org/packages/5f/4c/bebcaf754189283b2f3d457822a3d9b233d08ff50973d8f1e8d51f4d35ed/psycopg_binary-3.2.6-cp313-cp313-win_amd64.whl", hash = "sha256:afe697b8b0071f497c5d4c0f41df9e038391534f5614f7fb3a8c1ca32d66e860", size = 2783465 },
]
[[package]]
name = "ptyprocess"
version = "0.7.0"

View File

@@ -15,7 +15,7 @@ from typing import Optional
from dataclasses import dataclass
from dotenv import load_dotenv
from agno.agent import Agent
from agno.agent import Agent, AgentMemory
from agno.models.deepseek import DeepSeek
from agno.knowledge.pdf import PDFKnowledgeBase
from agno.vectordb.lancedb import LanceDb, SearchType
@@ -25,8 +25,9 @@ from agno.tools.mcp import MCPTools
from mcp.client.stdio import stdio_client
from mcp import ClientSession, StdioServerParameters
from agno.knowledge.website import WebsiteKnowledgeBase
from agno.storage.agent.sqlite import SqliteAgentStorage
from agno.memory.db.sqlite import SqliteMemoryDb
async def main():
load_dotenv()
@@ -94,13 +95,31 @@ async def main():
):
agent = Agent(
model=DeepSeek(id="deepseek-chat"),
storage=SqliteAgentStorage(
table_name="agent_sessions", db_file="tmp/agent_storage.db"
),
memory=AgentMemory(
db=SqliteMemoryDb(
table_name="agent_memory", db_file="tmp/agent_storage.db"
),
create_user_memories=True,
create_session_summary=True,
),
# Set add_history_to_messages=true to add the previous chat history to the messages sent to the Model.
#add_history_to_messages=True,
# Number of historical responses to add to the messages.
#num_history_responses=3,
markdown=True,
knowledge=knowledge_base,
search_knowledge=True,
show_tool_calls=True,
tools=[postgres_tools, searxng_tools],
session_id="1",
# Enable the agent to read the chat history
read_chat_history=True,
)
await agent.aprint_response("帮我分析一下aq.public数据库,并给出优化建议", stream=True)
await agent.aprint_response("我现在想要记录用户对智能体和课程的使用权限及使用情况,需要如何设计表结构", stream=True)
# await agent.aprint_response("帮我分析一下aq.public数据库,并给出优化建议", stream=True)
# await agent.aprint_response("阅读下 https://www.lucidchart.com/blog/database-design-best-practices 这篇文章", stream=True)