fix: main.py
This commit is contained in:
@@ -1,18 +1,21 @@
|
|||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from pathlib import Path
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
||||||
from langchain_chroma import Chroma
|
from langchain_chroma import Chroma
|
||||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
|
from langchain_tavily import TavilySearchResults
|
||||||
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
|
||||||
|
|
||||||
# Load environment variables
|
# --------------------- 1. Загрузка переменных окружения ---------------------
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# ---------- LLM and Embeddings ----------
|
# --------------------- 2. LLM и Embeddings ---------------------
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="openai/gpt-oss-20b:free",
|
||||||
base_url="https://openrouter.ai/api/v1",
|
base_url="https://openrouter.ai/api/v1",
|
||||||
@@ -26,85 +29,78 @@ embeddings = OpenAIEmbeddings(
|
|||||||
api_key=os.getenv("OPENAI_API_KEY"),
|
api_key=os.getenv("OPENAI_API_KEY"),
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------- Vector Store ----------
|
# --------------------- 3. Векторное хранилище Chroma ---------------------
|
||||||
CHROMA_DIR = "./chroma_db"
|
CHROMA_DIR = Path("./chroma_db")
|
||||||
|
CHROMA_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
vector_store = Chroma(
|
vector_store = Chroma(
|
||||||
collection_name="knowledge",
|
collection_name="knowledge",
|
||||||
embedding_function=embeddings,
|
embedding_function=embeddings,
|
||||||
persist_directory=CHROMA_DIR,
|
persist_directory=str(CHROMA_DIR),
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------- Document Loader ----------
|
# --------------------- 4. Загрузка документов ---------------------
|
||||||
|
DOCS_DIR = Path("./documents")
|
||||||
def load_documents(directory: str):
|
if DOCS_DIR.exists():
|
||||||
"""Read .txt/.md files, split into chunks and add to Chroma."""
|
|
||||||
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
||||||
docs = []
|
for file_path in DOCS_DIR.rglob("*.txt"):
|
||||||
for root, _, files in os.walk(directory):
|
text = file_path.read_text(encoding="utf-8")
|
||||||
for file in files:
|
docs = splitter.split_text(text)
|
||||||
if file.lower().endswith(('.txt', '.md')):
|
documents = [Document(page_content=chunk, metadata={"source": str(file_path)}) for chunk in docs]
|
||||||
path = os.path.join(root, file)
|
vector_store.add_documents(documents)
|
||||||
with open(path, 'r', encoding='utf-8') as f:
|
vector_store.persist()
|
||||||
text = f.read()
|
|
||||||
chunks = splitter.split_text(text)
|
|
||||||
docs.extend([Document(page_content=c, metadata={"source": path}) for c in chunks])
|
|
||||||
if docs:
|
|
||||||
vector_store.add_documents(docs)
|
|
||||||
vector_store.persist()
|
|
||||||
|
|
||||||
# Load documents once at startup
|
# --------------------- 5. Инструменты ---------------------
|
||||||
if not os.path.exists(CHROMA_DIR) or not os.listdir(CHROMA_DIR):
|
|
||||||
load_documents("./documents")
|
|
||||||
|
|
||||||
# ---------- Tools ----------
|
|
||||||
@tool
|
@tool
|
||||||
def search_local_kb(query: str, top_k: int = 3) -> str:
|
def search_local_kb(query: str, top_k: int = 3) -> str:
|
||||||
"""Semantic search in the local knowledge base."""
|
"""Semantic search in the local knowledge base."""
|
||||||
docs = vector_store.similarity_search(query, k=top_k)
|
docs = vector_store.similarity_search(query, k=top_k)
|
||||||
if not docs:
|
if not docs:
|
||||||
return "No relevant information found in local knowledge base."
|
return "No relevant local knowledge found."
|
||||||
return "\n---\n".join(f"{d.metadata.get('source', 'unknown')}\n{d.page_content}" for d in docs)
|
return "\n---\n".join(f"{i+1}. {doc.page_content[:200]}..." for i, doc in enumerate(docs))
|
||||||
|
|
||||||
@tool
|
@tool
|
||||||
def web_search(query: str) -> str:
|
def web_search(query: str) -> str:
|
||||||
"""Web search using Tavily."""
|
"""Web search using Tavily."""
|
||||||
from langchain_tavily import TavilySearchResults
|
tavily = TavilySearchResults(api_key=os.getenv("TAVILY_API_KEY"))
|
||||||
tavily = TavilySearchResults(max_results=3)
|
|
||||||
results = tavily.run(query)
|
results = tavily.run(query)
|
||||||
if not results:
|
if not results:
|
||||||
return "No web results found."
|
return "No web results found."
|
||||||
return "\n---\n".join(f"{r['title']}\n{r['content']}" for r in results)
|
return "\n---\n".join(f"{i+1}. {res['title']}\n{res['url']}\n{res['content'][:200]}..." for i, res in enumerate(results))
|
||||||
|
|
||||||
# ---------- Backend ----------
|
# --------------------- 6. Backend ---------------------
|
||||||
backend = CompositeBackend([
|
backend = CompositeBackend([
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
LocalShellBackend(workspace_dir="./workspace"),
|
||||||
FilesystemBackend(),
|
FilesystemBackend(),
|
||||||
])
|
])
|
||||||
|
|
||||||
# ---------- Agent ----------
|
# --------------------- 7. Создание агента ---------------------
|
||||||
agent = create_deep_agent(
|
agent = create_deep_agent(
|
||||||
model=llm,
|
model=llm,
|
||||||
tools=[search_local_kb, web_search],
|
tools=[search_local_kb, web_search],
|
||||||
backend=backend,
|
backend=backend,
|
||||||
system_prompt="You are a helpful assistant. For questions about local documents use the local knowledge base. For up‑to‑date facts use web search. Always state the source (chromadb or tavily) in your answer.",
|
system_prompt=(
|
||||||
|
"You are an assistant that answers user questions.\n"
|
||||||
|
"If the question is about information that should be in the local knowledge base, use the tool `search_local_kb`.\n"
|
||||||
|
"If the question requires up‑to‑date information from the web, use the tool `web_search`.\n"
|
||||||
|
"Always indicate the source of the answer in the format: `Источник: chromadb` or `Источник: tavily`."
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------- Main Loop ----------
|
# --------------------- 8. CLI ---------------------
|
||||||
async def main():
|
async def chat_loop():
|
||||||
print("RAG Agent ready. Type 'exit' to quit.")
|
print("Добро пожаловать в RAG‑агент. Введите 'exit' для выхода.")
|
||||||
while True:
|
while True:
|
||||||
user_input = input("\nЗапрос: ")
|
user_input = input("\nЗапрос: ")
|
||||||
if user_input.lower() in {"exit", "quit"}:
|
if user_input.lower() in {"exit", "quit", "q"}:
|
||||||
print("Goodbye!")
|
print("До свидания!")
|
||||||
break
|
break
|
||||||
# Invoke agent
|
|
||||||
result = await agent.ainvoke(
|
result = await agent.ainvoke(
|
||||||
{"messages": [HumanMessage(content=user_input)]},
|
{"messages": [{"role": "user", "content": user_input}]},
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
{"configurable": {"thread_id": "session-1"}},
|
||||||
)
|
)
|
||||||
# Extract last message content
|
# Последнее сообщение агента
|
||||||
answer = result["messages"][-1].content
|
content = result["messages"][-1]["content"]
|
||||||
print(f"\nОтвет:\n{answer}")
|
print(content)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(chat_loop())
|
||||||
|
|||||||
Reference in New Issue
Block a user