92 lines
3.7 KiB
Python
92 lines
3.7 KiB
Python
from langchain_ollama import ChatOllama, OllamaEmbeddings
|
|
from langchain_qdrant import QdrantVectorStore
|
|
from qdrant_client import QdrantClient
|
|
from qdrant_client.http.models import Distance, VectorParams
|
|
from langchain_core.documents import Document
|
|
from langchain.tools import tool
|
|
from langchain.agents import create_agent
|
|
import os
|
|
|
|
# ---------- LLM and embeddings ----------
|
|
llm = ChatOllama(model="llama3", temperature=0.2)
|
|
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
|
|
|
# ---------- Qdrant client & collection ----------
|
|
client = QdrantClient(":memory:")
|
|
collection_name = "knowledge_base"
|
|
client.create_collection(
|
|
collection_name=collection_name,
|
|
vectors_config=VectorParams(size=embeddings.embedding_size, distance=Distance.COSINE),
|
|
)
|
|
vector_store = QdrantVectorStore(client=client, collection_name=collection_name, embedding=embeddings)
|
|
|
|
# ---------- Text splitter ----------
|
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
|
|
|
# ---------- Tools ----------
|
|
@tool
|
|
def search_knowledge_base(query: str, max_results: int = 5) -> str:
|
|
"""Search the knowledge base for relevant documents."""
|
|
results = vector_store.similarity_search_with_score(query, k=max_results)
|
|
if not results:
|
|
return "No relevant documents found."
|
|
out_lines = []
|
|
for doc, score in results:
|
|
title = doc.metadata.get("title", "Untitled")
|
|
content = doc.page_content[:200] + ("..." if len(doc.page_content) > 200 else "")
|
|
out_lines.append(f"Title: {title}\nScore: {score:.4f}\nContent: {content}")
|
|
return "\n\n".join(out_lines)
|
|
|
|
@tool
|
|
def add_to_knowledge_base(content: str, title: str = "Untitled") -> str:
|
|
"""Add a new document to the knowledge base."""
|
|
chunks = splitter.split_text(content)
|
|
docs = [Document(page_content=chunk, metadata={"title": title}) for chunk in chunks]
|
|
vector_store.add_documents(docs)
|
|
return f"Added {len(chunks)} chunks under title '{title}'."
|
|
|
|
# ---------- Agent ----------
|
|
system_prompt = """
|
|
You are an assistant that can search and add documents to a knowledge base.
|
|
Use the tools `search_knowledge_base` and `add_to_knowledge_base` as needed.
|
|
Respond with plain text. Do not mention tool usage explicitly unless required by the user.
|
|
"""
|
|
agent = create_agent(
|
|
model=llm,
|
|
tools=[search_knowledge_base, add_to_knowledge_base],
|
|
system_prompt=system_prompt,
|
|
)
|
|
|
|
# ---------- CLI ----------
|
|
def main():
|
|
print("RAG Agent CLI. Commands: /add <title> | <content>, /search <query>, /quit")
|
|
while True:
|
|
user_input = input("\nYou: ").strip()
|
|
if not user_input:
|
|
continue
|
|
if user_input.lower() in ("exit", "quit", "/quit"):
|
|
print("Goodbye!")
|
|
break
|
|
if user_input.startswith("/add "):
|
|
try:
|
|
_, rest = user_input.split(maxsplit=1)
|
|
title, content = rest.split("|", 1)
|
|
title = title.strip()
|
|
content = content.strip()
|
|
result_msg = add_to_knowledge_base(content=content, title=title)
|
|
print(f"Bot: {result_msg}")
|
|
except ValueError:
|
|
print("Bot: Usage /add <title> | <content>")
|
|
elif user_input.startswith("/search "):
|
|
query = user_input[len("/search "):].strip()
|
|
result_msg = search_knowledge_base(query=query, max_results=5)
|
|
print(f"Bot:\n{result_msg}")
|
|
else:
|
|
# Regular chat
|
|
response = agent.invoke({"messages": [{"role": "human", "content": user_input}]})
|
|
ai_message = response["messages"][-1]
|
|
print(f"Bot: {ai_message.content}")
|
|
|
|
if __name__ == "__main__":
|
|
main() |