fix: main.py — Агент с RAG-памятью
This commit is contained in:
@@ -1,119 +1,77 @@
|
|||||||
import os
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from langchain_ollama import Ollama, OllamaEmbeddings
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from langchain_core.messages import HumanMessage
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from deepagents import create_deep_agent
|
from deepagents import create_deep_agent
|
||||||
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
|
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
|
||||||
from langchain_qdrant import QdrantVectorStore
|
|
||||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
from utils import llm, vector_store, splitter
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
from langchain_core.messages import HumanMessage
|
|
||||||
|
|
||||||
# Инициализация эмбеддинговой модели Ollama
|
|
||||||
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
|
||||||
|
|
||||||
# Инициализация векторного хранилища Qdrant
|
|
||||||
vector_store = QdrantVectorStore(
|
|
||||||
url="http://localhost:6333",
|
|
||||||
collection_name="knowledge",
|
|
||||||
embedding_function=embeddings
|
|
||||||
)
|
|
||||||
|
|
||||||
# Инструмент: поиск в базе знаний
|
|
||||||
@tool
|
@tool
|
||||||
def search_knowledge_base(query: str, max_results: int = 3) -> str:
|
def search_knowledge_base(query: str, max_results: int = 3) -> str:
|
||||||
"""Search the knowledge base for relevant information."""
|
"""Search the knowledge base for relevant information."""
|
||||||
docs = vector_store.similarity_search(query, k=max_results)
|
docs = vector_store.similarity_search(query, k=max_results)
|
||||||
return "\n".join(d.page_content for d in docs) if docs else "No results."
|
return "\n".join(d.page_content for d in docs) if docs else "No results."
|
||||||
|
|
||||||
# Инструмент: добавление документа в базу знаний
|
|
||||||
@tool
|
@tool
|
||||||
def add_to_knowledge_base(content: str, title: str = "doc") -> str:
|
def add_to_knowledge_base(content: str, title: str = "doc") -> str:
|
||||||
"""Add content to the knowledge base."""
|
"""Add content to the knowledge base."""
|
||||||
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
|
||||||
chunks = splitter.split_text(content)
|
chunks = splitter.split_text(content)
|
||||||
docs = [Document(page_content=chunk, metadata={"title": title}) for chunk in chunks]
|
docs = [Document(page_content=chunk, metadata={"title": title}) for chunk in chunks]
|
||||||
vector_store.add_documents(docs)
|
vector_store.add_documents(docs)
|
||||||
return f"Added: {title} ({len(chunks)} chunks)"
|
return f"Added {len(docs)} chunks for {title}."
|
||||||
|
|
||||||
# Инициализация LLM Ollama
|
|
||||||
llm = Ollama(model="llama3")
|
|
||||||
|
|
||||||
# Backend для deepagents
|
|
||||||
backend = CompositeBackend([
|
backend = CompositeBackend([
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
LocalShellBackend(workspace_dir="./workspace"),
|
||||||
FilesystemBackend(),
|
FilesystemBackend(),
|
||||||
])
|
])
|
||||||
|
|
||||||
# Системный промпт агента
|
|
||||||
system_prompt = (
|
|
||||||
"You are a helpful agent with access to a knowledge base. "
|
|
||||||
"Use the provided tools to search and add information. "
|
|
||||||
"When answering user queries, first search the knowledge base and then provide a concise response."
|
|
||||||
)
|
|
||||||
|
|
||||||
# Создание агента
|
|
||||||
agent = create_deep_agent(
|
agent = create_deep_agent(
|
||||||
model=llm,
|
model=llm,
|
||||||
tools=[search_knowledge_base, add_to_knowledge_base],
|
tools=[search_knowledge_base, add_to_knowledge_base],
|
||||||
backend=backend,
|
backend=backend,
|
||||||
system_prompt=system_prompt,
|
system_prompt="You are a helpful assistant with access to a knowledge base. Use the provided tools to search and add information.",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Загрузка документов из директории в векторное хранилище
|
async def main():
|
||||||
def load_documents_from_dir(dir_path: str):
|
print("RAG Agent CLI. Commands: /add, /search, /quit")
|
||||||
for root, dirs, files in os.walk(dir_path):
|
|
||||||
for file in files:
|
|
||||||
if file.lower().endswith(".txt"):
|
|
||||||
path = os.path.join(root, file)
|
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
|
||||||
content = f.read()
|
|
||||||
title = os.path.splitext(file)[0]
|
|
||||||
result = add_to_knowledge_base(content, title)
|
|
||||||
print(result)
|
|
||||||
|
|
||||||
# Интерактивный клиент
|
|
||||||
async def interactive_cli():
|
|
||||||
print("Welcome to the RAG agent. Commands: /add <file_path>, /search <query>, /quit")
|
|
||||||
while True:
|
while True:
|
||||||
user_input = input(">> ")
|
user_input = input(">> ").strip()
|
||||||
if not user_input.strip():
|
if not user_input:
|
||||||
continue
|
continue
|
||||||
if user_input.startswith("/add"):
|
if user_input.lower() == "/quit":
|
||||||
parts = user_input.split(maxsplit=1)
|
print("Goodbye.")
|
||||||
if len(parts) < 2:
|
break
|
||||||
print("Usage: /add <file_path>")
|
if user_input.lower().startswith("/add"):
|
||||||
continue
|
title = input("Title: ").strip()
|
||||||
file_path = parts[1]
|
print("Enter content (end with a single line containing only END):")
|
||||||
if not os.path.isfile(file_path):
|
lines = []
|
||||||
print(f"File not found: {file_path}")
|
while True:
|
||||||
continue
|
line = input()
|
||||||
with open(file_path, "r", encoding="utf-8") as f:
|
if line.strip() == "END":
|
||||||
content = f.read()
|
break
|
||||||
title = os.path.splitext(os.path.basename(file_path))[0]
|
lines.append(line)
|
||||||
|
content = "\n".join(lines)
|
||||||
result = add_to_knowledge_base(content, title)
|
result = add_to_knowledge_base(content, title)
|
||||||
print(result)
|
print(result)
|
||||||
elif user_input.startswith("/search"):
|
continue
|
||||||
parts = user_input.split(maxsplit=1)
|
if user_input.lower().startswith("/search"):
|
||||||
if len(parts) < 2:
|
query = input("Query: ").strip()
|
||||||
print("Usage: /search <query>")
|
max_results_str = input("Max results (default 3): ").strip()
|
||||||
continue
|
max_results = int(max_results_str) if max_results_str.isdigit() else 3
|
||||||
query = parts[1]
|
result = search_knowledge_base(query, max_results)
|
||||||
response = await agent.ainvoke(
|
print("Search results:")
|
||||||
{"messages": [HumanMessage(content=query)]},
|
print(result)
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
continue
|
||||||
)
|
# Regular message to agent
|
||||||
print(response["messages"][-1].content)
|
response = await agent.ainvoke(
|
||||||
elif user_input.startswith("/quit"):
|
{"messages": [HumanMessage(content=user_input)]},
|
||||||
print("Goodbye!")
|
{"configurable": {"thread_id": "session-1"}},
|
||||||
break
|
)
|
||||||
else:
|
print(response["messages"][-1].content)
|
||||||
print("Unknown command. Use /add, /search, /quit")
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
# При необходимости загрузить начальные документы
|
|
||||||
# load_documents_from_dir("./docs")
|
|
||||||
await interactive_cli()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
Reference in New Issue
Block a user