From 69f3493f7af98866b0ee8be8d45f35c92c18f340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=205f1b81b8-4f5d-11e8-9c2d-fa7ae01?= =?UTF-8?q?bbebc?= Date: Wed, 1 Jul 2026 18:46:24 +0000 Subject: [PATCH] =?UTF-8?q?fix(needs=5Ffixes):=201=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9,=200=20=D0=BE?= =?UTF-8?q?=D1=82=D1=81=D1=82=D0=BE=D1=8F=D0=BD=D0=BE=20=E2=80=94=20main.p?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 155 ++++++++++++++++++++++---------------------------------- 1 file changed, 60 insertions(+), 95 deletions(-) diff --git a/main.py b/main.py index 613ec29..de4dcb6 100644 --- a/main.py +++ b/main.py @@ -1,138 +1,103 @@ import os import asyncio from pathlib import Path - from dotenv import load_dotenv -from langchain_openai import ChatOpenAI, OpenAIEmbeddings + +# LangChain imports +from langchain_ollama import ChatOllama, OllamaEmbeddings from langchain_core.documents import Document from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_qdrant import QdrantVectorStore +from langchain.agents import create_agent from langchain.tools import tool -from deepagents import create_deep_agent -from deepagents.backends import CompositeBackend, LocalShellBackend, FilesystemBackend +from langchain_core.messages import HumanMessage -# Load environment variables (OPENAI_API_KEY) +# Load environment variables (e.g., Ollama host) 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, -) +# LLM and embeddings configuration +llm = ChatOllama(model="llama3") +embeddings = OllamaEmbeddings(model="nomic-embed-text") -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 -# --------------------------------------------------------------------------- +# Qdrant client and vector store initialization from qdrant_client import QdrantClient -qdrant_client = QdrantClient(url="http://localhost:6333") # Qdrant must be running locally +qdrant_client = QdrantClient(host="localhost", port=6333) vector_store = QdrantVectorStore( - embedding_function=embeddings, client=qdrant_client, collection_name="knowledge", + embeddings=embeddings, ) -# --------------------------------------------------------------------------- # Text splitter for chunking documents -# --------------------------------------------------------------------------- -text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) +splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) -# --------------------------------------------------------------------------- -# Tools for the agent -# --------------------------------------------------------------------------- +# Tool: Search knowledge base @tool -async def search_knowledge_base(query: str, max_results: int = 5) -> str: - """Perform a semantic search in the knowledge base.""" +def search_knowledge_base(query: str, max_results: int = 3) -> str: + """Search the knowledge base for relevant information.""" 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]) + return "\n\n---\n\n".join(f"<{doc.metadata.get('title', 'Untitled')}> +{doc.page_content}" for doc in docs) +# Tool: Add document to knowledge base @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." +def add_to_knowledge_base(content: str, title: str = "Document") -> str: + """Add content to the knowledge base.""" + # Split content into chunks + chunks = splitter.split_text(content) + documents = [Document(page_content=chunk, metadata={"title": title, "chunk_index": idx}) + for idx, chunk in enumerate(chunks)] + vector_store.add_documents(documents) + return f"Added {len(chunks)} chunks from '{title}'." -# --------------------------------------------------------------------------- -# Backend configuration for DeepAgents -# --------------------------------------------------------------------------- -backend = CompositeBackend([ - LocalShellBackend(workspace_dir="./workspace"), - FilesystemBackend(), -]) - -# --------------------------------------------------------------------------- -# DeepAgent definition -# --------------------------------------------------------------------------- -agent = create_deep_agent( - model=llm, +# Agent creation +agent = create_agent( + llm=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.", + system_prompt="You are an AI assistant with access to a knowledge base. Use the provided tools to search and add information." ) -# --------------------------------------------------------------------------- -# 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}.") +# Helper to invoke agent asynchronously +async def invoke_agent(user_input: str) -> str: + result = await agent.ainvoke( + {"messages": [HumanMessage(content=user_input)]}, + {"configurable": {"thread_id": "session-1"}}, + ) + # The last message is the agent's reply + return result["messages"][-1].content -# --------------------------------------------------------------------------- -# Interactive CLI client -# --------------------------------------------------------------------------- -async def interactive_loop(): - print("Welcome to the RAG Agent CLI. Commands: /add title content | /search query | /quit") +# CLI loop +async def main(): + print("Welcome to the RAG agent CLI. Commands: /add | /search | /quit") while True: - user_input = input("> ") + user_input = input(">>> ") + if not user_input: + continue 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") + if user_input.lower().startswith("/add "): + path = user_input[5:].strip() + if Path(path).is_file(): + content = Path(path).read_text(encoding="utf-8") + title = Path(path).stem + # Directly invoke tool via agent + response = await invoke_agent(f"Add document: {title}\n{content}") + else: + print("File not found.") 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}" + elif user_input.lower().startswith("/search "): + query = user_input[8:].strip() + response = await invoke_agent(f"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() + print("\n--- Agent Response ---\n") + print(response) + print("\n-----------------------\n") if __name__ == "__main__": asyncio.run(main())