Compare commits

..

9 Commits

Author SHA1 Message Date
214daadc59 ♻️ refactor(代码): 优化主函数中的参数配置
- 启用历史消息记录以增强聊天功能
- 修改响应打印的调用以改善代码可读性
2025-03-31 03:55:38 +08:00
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
8e871647f6 ♻️ refactor(代码): 重构数据库优化器逻辑
- 移除不必要的DatabaseOptimizer类及其配置
- 添加WebsiteKnowledgeBase以支持网页知识库
- 优化MCPTools的使用,合并多个工具的初始化
2025-03-31 02:20:24 +08:00
0455760852 feat(database_optimizer): 支持异步数据库分析
- 新增异步主函数以提升性能
- 重构知识库加载及智能体创建逻辑
- 添加客户端会话以连接MCP服务器
2025-03-31 01:55:21 +08:00
f280258527 feat(database_optimizer): 添加数据库优化工程师智能体功能
- 使用 DeepSeek 模型进行智能分析
- 集成 PostgreSQL MCP 服务器工具
- 加载数据库知识库
- 提供数据库优化建议
2025-03-31 01:00:28 +08:00
356041051c feat(main): 增加PDF知识库和MCP工具集成
- 创建PDF知识库以从本地文件加载数据并支持向量搜索
- 集成MCP工具以与PostgreSQL数据库进行交互
- 通过组合知识库实现多源知识集成
2025-03-31 00:56:36 +08:00
e9865b3e5b feat(project): add initial project setup and dependencies
- 新增.gitignore以排除环境文件和IDE配置
- 更新pyproject.toml以添加新依赖:ipywidgets, openai, python-dotenv
- 新建Python脚本以加载环境变量并配置智能体

💄 style(notebook): improve notebook code structure and comments

- 修改notebook中代码块的执行计数为null
- 添加代码注释以提升可读性和理解性
- 引入load_dotenv以支持环境变量加载
2025-03-30 23:50:21 +08:00
d45eecda50 feat(pyproject): add new dependencies for project
- 添加`agno`、`anthropic`和`notebook`作为项目依赖
- 确保项目能够使用这些库以实现新的功能

 feat(notebook.ipynb): enhance notebook with new code cell

- 在Jupyter笔记本中添加新的代码单元以执行打印操作
- 更新内核配置以使用`.venv`环境
2025-03-30 23:16:31 +08:00
6 changed files with 2364 additions and 2 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.env
.idea
.venv
tmp

View File

@@ -4,4 +4,19 @@ version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
dependencies = [
"agno>=1.2.6",
"anthropic>=0.49.0",
"ipywidgets>=8.1.5",
"lancedb>=0.21.2",
"mcp>=1.6.0",
"notebook>=7.3.3",
"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",
"tantivy>=0.22.2",
]

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)

2110
uv.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -16,11 +16,68 @@
"- [ ] 精通关系型数据库系统的理论知识以及`postgresql`的实现细节(知识库)\n",
"- [ ] 能够使用`postgresql mcp server`来分析现有数据库设计(行为)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-30T15:44:44.236141Z",
"start_time": "2025-03-30T15:44:44.223728Z"
}
},
"outputs": [],
"source": [
"# 然后在notebook中加载\n",
"from dotenv import load_dotenv\n",
"\n",
"# 加载当前目录下的.env文件\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2025-03-30T15:44:58.492402Z",
"start_time": "2025-03-30T15:44:45.695125Z"
}
},
"outputs": [],
"source": [
"from agno.agent import Agent, RunResponse # noqa\n",
"from agno.models.deepseek import DeepSeek\n",
"\n",
"agent = Agent(model=DeepSeek(id=\"deepseek-chat\"), markdown=True)\n",
"\n",
"# Get the response in a variable\n",
"# run: RunResponse = agent.run(\"Share a 2 sentence horror story\")\n",
"# print(run.content)\n",
"\n",
"# Print the response in the terminal\n",
"agent.print_response(\"傻狗\")\n",
"# 用jupyter的话输出有问题妈的,转到py文件"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,

128
数据库优化工程师.py Normal file
View File

@@ -0,0 +1,128 @@
#!/usr/bin/env python3
"""
PostgreSQL 数据库优化工程师智能体
功能:
- 使用 DeepSeek 模型进行智能分析
- 集成 PostgreSQL MCP 服务器工具
- 加载数据库知识库
- 提供数据库优化建议
"""
import asyncio
import os
from typing import Optional
from dataclasses import dataclass
from dotenv import load_dotenv
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
from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.combined import CombinedKnowledgeBase
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()
"""设置知识库"""
local_pdf_kb = PDFKnowledgeBase(
path="D:\\Sources\\DONGJAK-TOOLS\\pdfs\\Database Fundamentals.pdf",
vector_db=LanceDb(
table_name="database_fundamentals",
uri="tmp/lancedb",
search_type=SearchType.vector,
embedder=OpenAIEmbedder(id="text-embedding-3-small"),
),
)
# Create Website knowledge base
website_kb = WebsiteKnowledgeBase(
urls=["https://www.lucidchart.com/blog/database-design-best-practices"],
max_links=10,
vector_db=LanceDb(
table_name="website_documents",
uri="tmp/lancedb",
search_type=SearchType.vector,
embedder=OpenAIEmbedder(id="text-embedding-3-small"),
),
)
knowledge_base = CombinedKnowledgeBase(
sources=[local_pdf_kb, website_kb],
vector_db=LanceDb(
table_name="combined_documents",
uri="tmp/lancedb",
search_type=SearchType.vector,
embedder=OpenAIEmbedder(id="text-embedding-3-small"),
),
)
knowledge_base.load()
postgres_server_params = StdioServerParameters(
command="cmd",
args=[
"/c",
"npx",
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://postgres:postgres@192.168.1.7:5432/aq",
],
env={},
)
searxng_server_params = StdioServerParameters(
command="cmd",
args=[
"/c",
"npx",
"-y",
"https://github.com/ihor-sokoliuk/mcp-searxng.git",
],
env={
"SEARXNG_URL": "https://searchxng.ailoveworld.cn",
},
)
# Create a client session to connect to the MCP server
async with (
MCPTools(server_params=postgres_server_params) as postgres_tools,
MCPTools(server_params=searxng_server_params) as searxng_tools,
):
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("我现在想要记录用户对智能体和课程的使用权限及使用情况,需要如何设计表结构", stream=True)
await agent.aprint_response("调用get_chat_history", 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)
if __name__ == "__main__":
asyncio.run(main())