108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
from langchain_openai import ChatOpenAI
|
|
from pydantic import SecretStr
|
|
from langchain.tools import tool
|
|
from langchain_qdrant import QdrantVectorStore
|
|
from qdrant_client import QdrantClient
|
|
from qdrant_client.http.models import Distance, VectorParams
|
|
from langchain_ollama import OllamaEmbeddings
|
|
from langchain_core.documents import Document
|
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
from langchain.agents import create_agent
|
|
|
|
# ---------- LLM ----------
|
|
llm = ChatOpenAI(
|
|
model="openai/gpt-oss-20b",
|
|
base_url='https://platform.brojs.ru/jrnl-bh/api/inference/v1',
|
|
api_key=SecretStr("jrnl_30283ab953615cbb6846ff9940a1eedce0b76d7b2f59a2394f29e74643e6a90d"),
|
|
temperature=0.7,
|
|
)
|
|
|
|
# ---------- Embeddings ----------
|
|
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
|
|
|
# ---------- Qdrant client ----------
|
|
client = QdrantClient(":memory:")
|
|
client.create_collection(
|
|
collection_name="knowledge_base",
|
|
vectors_config=VectorParams(size=embeddings.embedding_size, distance=Distance.COSINE),
|
|
)
|
|
vector_store = QdrantVectorStore(
|
|
client=client,
|
|
collection_name="knowledge_base",
|
|
embedding=embeddings,
|
|
)
|
|
|
|
# ---------- Text splitter ----------
|
|
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
|
|
|
# ---------- Tools ----------
|
|
@tool
|
|
def search_knowledge_base(query: str, max_results: int = 5) -> str:
|
|
"""Search the knowledge base for relevant documents."""
|
|
docs_with_score = vector_store.similarity_search_with_score(query, k=max_results)
|
|
if not docs_with_score:
|
|
return "No results found."
|
|
result_lines = [
|
|
f"{i+1}. {doc.page_content[:200]}..." for i, (doc, _) in enumerate(docs_with_score)
|
|
]
|
|
return "\n".join(result_lines)
|
|
|
|
@tool
|
|
def add_to_knowledge_base(content: str, title: str) -> str:
|
|
"""Add a new document to the knowledge base."""
|
|
chunks = splitter.split_text(content)
|
|
documents = [Document(page_content=chunk, metadata={"title": title}) for chunk in chunks]
|
|
vector_store.add_documents(documents)
|
|
return f"Added {len(documents)} chunks from '{title}'."
|
|
|
|
# ---------- Agent ----------
|
|
agent = create_agent(
|
|
model=llm,
|
|
tools=[search_knowledge_base, add_to_knowledge_base],
|
|
system_prompt="You are an assistant that can search and add documents to the knowledge base.",
|
|
)
|
|
|
|
# ---------- CLI ----------
|
|
def main():
|
|
print("RAG Agent CLI. Commands: /add <title> <content>, /search <query>, /quit")
|
|
while True:
|
|
try:
|
|
inp = input("> ").strip()
|
|
except EOFError:
|
|
break
|
|
if not inp:
|
|
continue
|
|
if inp.lower() in ("quit", "exit"):
|
|
print("Bye!")
|
|
break
|
|
|
|
# Add document
|
|
if inp.startswith("/add "):
|
|
parts = inp[5:].split(None, 1)
|
|
if len(parts) != 2:
|
|
print("Usage: /add <title> <content>")
|
|
continue
|
|
title, content = parts
|
|
res = agent.invoke({"messages": [{"role": "human", "content": f"Add document '{title}'"}]})
|
|
for msg in res["messages"]:
|
|
if hasattr(msg, "tool_calls"):
|
|
print(msg.content)
|
|
continue
|
|
|
|
# Search query
|
|
if inp.startswith("/search "):
|
|
query = inp[8:].strip()
|
|
res = agent.invoke({"messages": [{"role": "human", "content": f"Search: {query}"}]})
|
|
for msg in res["messages"]:
|
|
if hasattr(msg, "tool_calls"):
|
|
print(msg.content)
|
|
continue
|
|
|
|
# General chat
|
|
res = agent.invoke({"messages": [{"role": "human", "content": inp}]})
|
|
for msg in res["messages"]:
|
|
if hasattr(msg, "content"):
|
|
print(msg.content)
|
|
|
|
if __name__ == "__main__":
|
|
main() |