Files
task-6a02e23da6fe2e4ac16acf65/main.py
T
2026-06-27 13:55:36 +00:00

139 lines
5.6 KiB
Python

import os
import asyncio
from pathlib import Path
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_qdrant import QdrantVectorStore
from langchain.tools import tool
from deepagents import create_deep_agent
from deepagents.backends import CompositeBackend, LocalShellBackend, FilesystemBackend
# Load environment variables (OPENAI_API_KEY)
load_dotenv()
# ---------------------------------------------------------------------------
# LLM and embeddings configuration (OpenRouter)
# ---------------------------------------------------------------------------
llm = ChatOpenAI(
model="openai/gpt-oss-20b:free",
base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.0,
)
embeddings = OpenAIEmbeddings(
model="text-embedding-3-small",
base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENAI_API_KEY"),
)
# ---------------------------------------------------------------------------
# Qdrant vector store setup
# ---------------------------------------------------------------------------
from qdrant_client import QdrantClient
qdrant_client = QdrantClient(url="http://localhost:6333") # Qdrant must be running locally
vector_store = QdrantVectorStore(
embedding_function=embeddings,
client=qdrant_client,
collection_name="knowledge",
)
# ---------------------------------------------------------------------------
# Text splitter for chunking documents
# ---------------------------------------------------------------------------
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
# ---------------------------------------------------------------------------
# Tools for the agent
# ---------------------------------------------------------------------------
@tool
async def search_knowledge_base(query: str, max_results: int = 5) -> str:
"""Perform a semantic search in the knowledge base."""
docs = vector_store.similarity_search(query, k=max_results)
if not docs:
return "No relevant documents found."
return "\n\n".join([f"Title: {doc.metadata.get('title', 'N/A')}\n{doc.page_content}" for doc in docs])
@tool
async def add_to_knowledge_base(content: str, title: str = "Unnamed Document") -> str:
"""Add a document to the knowledge base after chunking."""
chunks = text_splitter.split_text(content)
docs = [Document(page_content=chunk, metadata={"title": title}) for chunk in chunks]
vector_store.add_documents(docs)
return f"Document '{title}' added with {len(chunks)} chunks."
# ---------------------------------------------------------------------------
# Backend configuration for DeepAgents
# ---------------------------------------------------------------------------
backend = CompositeBackend([
LocalShellBackend(workspace_dir="./workspace"),
FilesystemBackend(),
])
# ---------------------------------------------------------------------------
# DeepAgent definition
# ---------------------------------------------------------------------------
agent = create_deep_agent(
model=llm,
tools=[search_knowledge_base, add_to_knowledge_base],
backend=backend,
system_prompt="You are a helpful assistant with access to a knowledge base. Use the provided tools to search and add documents. Respond concisely.",
)
# ---------------------------------------------------------------------------
# Helper function to load all .txt files from a directory into the vector store
# ---------------------------------------------------------------------------
async def load_documents_from_directory(directory: str):
dir_path = Path(directory)
for txt_file in dir_path.rglob("*.txt"):
content = txt_file.read_text(encoding="utf-8")
title = txt_file.stem
await add_to_knowledge_base(content, title)
print(f"Loaded {len(list(dir_path.rglob('*.txt')))} documents from {directory}.")
# ---------------------------------------------------------------------------
# Interactive CLI client
# ---------------------------------------------------------------------------
async def interactive_loop():
print("Welcome to the RAG Agent CLI. Commands: /add title content | /search query | /quit")
while True:
user_input = input("> ")
if user_input.lower() == "/quit":
print("Goodbye!")
break
if user_input.startswith("/add "):
try:
_, rest = user_input.split("/add ", 1)
title, content = rest.split(" ", 1)
except ValueError:
print("Usage: /add title content")
continue
human_msg = f"Please add a document titled '{title}' with content: {content}"
elif user_input.startswith("/search "):
query = user_input[len("/search "):]
human_msg = f"Please search for: {query}"
else:
print("Unknown command. Use /add, /search, or /quit.")
continue
result = await agent.ainvoke(
{"messages": [HumanMessage(content=human_msg)]},
{"configurable": {"thread_id": "session-1"}},
)
print(result["messages"][-1].content)
# ---------------------------------------------------------------------------
# Main entry point
# ---------------------------------------------------------------------------
async def main():
# Optionally load documents from a directory on startup
# await load_documents_from_directory("./data")
await interactive_loop()
if __name__ == "__main__":
asyncio.run(main())