Update main.py
This commit is contained in:
@@ -1,107 +1,122 @@
|
|||||||
"""
|
"""Interactive LangChain agent with local RAG memory on Qdrant and Ollama."""
|
||||||
Agent with RAG memory using Qdrant and Ollama.
|
|
||||||
|
|
||||||
This is a minimal example that demonstrates:
|
|
||||||
1. Connecting to a local Qdrant instance.
|
|
||||||
2. Creating two tools: `search_knowledge_base` and `add_to_knowledge_base`.
|
|
||||||
3. Using LangChain's RecursiveCharacterTextSplitter to chunk documents.
|
|
||||||
4. Building an agent with the tools via `create_agent`.
|
|
||||||
5. A simple REPL that accepts `/add`, `/search` and `/quit` commands.
|
|
||||||
|
|
||||||
To run:
|
|
||||||
pip install -r requirements.txt
|
|
||||||
python main.py
|
|
||||||
|
|
||||||
Make sure a Qdrant instance is running locally (default port 6333) and Ollama is available at http://localhost:11434.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from functools import lru_cache
|
||||||
from typing import List, Dict
|
from typing import Any
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from langchain_community.document_loaders import DirectoryLoader
|
from langchain.agents import create_agent
|
||||||
|
from langchain_core.documents import Document
|
||||||
|
from langchain_core.tools import tool
|
||||||
|
from langchain_ollama import ChatOllama, OllamaEmbeddings
|
||||||
|
from langchain_qdrant import QdrantVectorStore
|
||||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||||
from langchain.embeddings.ollama import OllamaEmbeddings
|
from qdrant_client import QdrantClient
|
||||||
from langchain.vectorstores.qdrant import QdrantVectorStore
|
from qdrant_client.models import Distance, VectorParams
|
||||||
from langchain.agents import create_agent, AgentExecutor, Tool
|
|
||||||
from langchain.schema import Document
|
|
||||||
|
|
||||||
# Configuration
|
|
||||||
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6333")
|
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6333")
|
||||||
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama2")
|
QDRANT_COLLECTION = os.getenv("QDRANT_COLLECTION", "rag_memory")
|
||||||
COLLECTION_NAME = "rag_collection"
|
OLLAMA_CHAT_MODEL = os.getenv("OLLAMA_CHAT_MODEL", "llama3")
|
||||||
|
OLLAMA_EMBED_MODEL = os.getenv("OLLAMA_EMBED_MODEL", "nomic-embed-text")
|
||||||
|
EMBEDDING_SIZE = int(os.getenv("OLLAMA_EMBEDDING_SIZE", "768"))
|
||||||
|
|
||||||
# Initialize embeddings and vector store
|
|
||||||
embeddings = OllamaEmbeddings(model=OLLAMA_MODEL)
|
@lru_cache(maxsize=1)
|
||||||
vector_store = QdrantVectorStore(
|
def get_embeddings() -> OllamaEmbeddings:
|
||||||
url=QDRANT_URL,
|
return OllamaEmbeddings(model=OLLAMA_EMBED_MODEL)
|
||||||
collection_name=COLLECTION_NAME,
|
|
||||||
embedding_function=embeddings,
|
|
||||||
|
@lru_cache(maxsize=1)
|
||||||
|
def get_vector_store() -> QdrantVectorStore:
|
||||||
|
client = QdrantClient(url=QDRANT_URL)
|
||||||
|
collections = {item.name for item in client.get_collections().collections}
|
||||||
|
if QDRANT_COLLECTION not in collections:
|
||||||
|
client.create_collection(
|
||||||
|
collection_name=QDRANT_COLLECTION,
|
||||||
|
vectors_config=VectorParams(size=EMBEDDING_SIZE, distance=Distance.COSINE),
|
||||||
|
)
|
||||||
|
return QdrantVectorStore(
|
||||||
|
client=client,
|
||||||
|
collection_name=QDRANT_COLLECTION,
|
||||||
|
embedding=get_embeddings(),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Tool: search knowledge base
|
|
||||||
|
|
||||||
def search_knowledge_base(query: str) -> str:
|
def chunk_document(content: str, title: str) -> list[Document]:
|
||||||
"""Return the top 3 relevant snippets for a query."""
|
splitter = RecursiveCharacterTextSplitter(chunk_size=700, chunk_overlap=100)
|
||||||
docs = vector_store.similarity_search_with_score(query, k=3)
|
return splitter.create_documents([content], metadatas=[{"title": title}])
|
||||||
if not docs:
|
|
||||||
|
|
||||||
|
@tool
|
||||||
|
def search_knowledge_base(query: str, max_results: int = 3) -> str:
|
||||||
|
"""Search relevant chunks in the local Qdrant knowledge base."""
|
||||||
|
results = get_vector_store().similarity_search_with_score(query, k=max_results)
|
||||||
|
if not results:
|
||||||
return "No relevant documents found."
|
return "No relevant documents found."
|
||||||
results = [f"{idx+1}. {doc[0].page_content[:200]}… (score: {doc[1]:.4f})" for idx, doc in enumerate(docs)]
|
|
||||||
return "\n".join(results)
|
|
||||||
|
|
||||||
# Tool: add to knowledge base
|
lines: list[str] = []
|
||||||
|
for index, (document, score) in enumerate(results, start=1):
|
||||||
|
title = document.metadata.get("title", "untitled")
|
||||||
|
snippet = document.page_content.replace("\n", " ")[:300]
|
||||||
|
lines.append(f"{index}. {title} (score={score:.4f}): {snippet}")
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
def add_to_knowledge_base(file_path: str) -> str:
|
|
||||||
"""Load a text file, chunk it and add to Qdrant."""
|
|
||||||
loader = DirectoryLoader(Path(file_path).parent.as_posix(), glob=Path(file_path).name)
|
|
||||||
docs = loader.load()
|
|
||||||
if not docs:
|
|
||||||
return f"No documents found in {file_path}."
|
|
||||||
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
|
||||||
chunks: List[Document] = []
|
|
||||||
for doc in docs:
|
|
||||||
chunks.extend(splitter.split_documents([doc]))
|
|
||||||
vector_store.add_documents(chunks)
|
|
||||||
return f"Added {len(chunks)} chunks from {file_path} to the knowledge base."
|
|
||||||
|
|
||||||
# Define tools list
|
@tool
|
||||||
TOOLS: List[Tool] = [
|
def add_to_knowledge_base(content: str, title: str) -> str:
|
||||||
Tool(
|
"""Split content into chunks and store it in the local Qdrant knowledge base."""
|
||||||
name="search_knowledge_base",
|
documents = chunk_document(content, title)
|
||||||
func=search_knowledge_base,
|
ids = [str(uuid4()) for _ in documents]
|
||||||
description="Search the local Qdrant knowledge base for relevant information.",
|
get_vector_store().add_documents(documents, ids=ids)
|
||||||
|
return f"Added {len(documents)} chunk(s) from '{title}' to the knowledge base."
|
||||||
|
|
||||||
|
|
||||||
|
def build_agent() -> Any:
|
||||||
|
llm = ChatOllama(model=OLLAMA_CHAT_MODEL)
|
||||||
|
return create_agent(
|
||||||
|
model=llm,
|
||||||
|
tools=[search_knowledge_base, add_to_knowledge_base],
|
||||||
|
system_prompt=(
|
||||||
|
"You are a RAG assistant. Use search_knowledge_base before answering "
|
||||||
|
"questions that may depend on stored knowledge, and use "
|
||||||
|
"add_to_knowledge_base when the user asks to remember information."
|
||||||
),
|
),
|
||||||
Tool(
|
)
|
||||||
name="add_to_knowledge_base",
|
|
||||||
func=add_to_knowledge_base,
|
|
||||||
description="Add a text file to the knowledge base. Provide full path.",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
||||||
# Build agent
|
|
||||||
agent = create_agent(TOOLS, llm=embeddings) # embeddings can act as LLM via Ollama
|
|
||||||
executor = AgentExecutor(agent=agent, tools=TOOLS, verbose=True)
|
|
||||||
|
|
||||||
# Simple REPL
|
def run_cli() -> None:
|
||||||
if __name__ == "__main__":
|
print("RAG agent. Commands: /add <title> | <content>, /search <query>, /quit")
|
||||||
print("Welcome to the RAG agent. Commands: /add <file>, /search <query>, /quit")
|
agent = None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
inp = input(">>> ")
|
user_input = input(">>> ").strip()
|
||||||
except EOFError:
|
except EOFError:
|
||||||
break
|
break
|
||||||
if not inp:
|
|
||||||
|
if not user_input:
|
||||||
continue
|
continue
|
||||||
if inp.startswith("/quit"):
|
if user_input == "/quit":
|
||||||
print("Goodbye!")
|
|
||||||
break
|
break
|
||||||
elif inp.startswith("/add "):
|
if user_input.startswith("/add "):
|
||||||
path = inp.split(maxsplit=1)[1]
|
payload = user_input[5:]
|
||||||
print(add_to_knowledge_base(path))
|
if "|" in payload:
|
||||||
elif inp.startswith("/search "):
|
title, content = [part.strip() for part in payload.split("|", 1)]
|
||||||
query = inp.split(maxsplit=1)[1]
|
|
||||||
print(search_knowledge_base(query))
|
|
||||||
else:
|
else:
|
||||||
# Treat as normal agent prompt
|
title, content = "note", payload.strip()
|
||||||
result = executor.invoke({"input": inp})
|
print(add_to_knowledge_base.invoke({"content": content, "title": title}))
|
||||||
print(result.get("output", ""))
|
continue
|
||||||
|
if user_input.startswith("/search "):
|
||||||
|
query = user_input[8:].strip()
|
||||||
|
print(search_knowledge_base.invoke({"query": query, "max_results": 3}))
|
||||||
|
continue
|
||||||
|
|
||||||
|
if agent is None:
|
||||||
|
agent = build_agent()
|
||||||
|
response = agent.invoke({"messages": [{"role": "user", "content": user_input}]})
|
||||||
|
messages = response.get("messages", [])
|
||||||
|
print(messages[-1].content if messages else response)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_cli()
|
||||||
|
|||||||
Reference in New Issue
Block a user