Compare commits
9 Commits
33487a746c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 214daadc59 | |||
| 46efa8d9ed | |||
| 6d25886286 | |||
| 8e871647f6 | |||
| 0455760852 | |||
| f280258527 | |||
| 356041051c | |||
| e9865b3e5b | |||
| d45eecda50 |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
.env
|
||||
.idea
|
||||
.venv
|
||||
tmp
|
||||
@@ -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
48
test2.py
Normal 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)
|
||||
@@ -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
128
数据库优化工程师.py
Normal 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())
|
||||
Reference in New Issue
Block a user