62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""Интерактивный RAG-агент (Qdrant + Ollama + LangChain)."""
|
|
from __future__ import annotations
|
|
|
|
try:
|
|
from agent_app import build_agent, build_llm
|
|
from knowledge_base import build_knowledge_base
|
|
from rag_tools import add_to_knowledge_base, get_knowledge_base, search_knowledge_base
|
|
except ModuleNotFoundError:
|
|
from .agent_app import build_agent, build_llm
|
|
from .knowledge_base import build_knowledge_base
|
|
from .rag_tools import add_to_knowledge_base, get_knowledge_base, search_knowledge_base
|
|
|
|
__all__ = [
|
|
"build_agent",
|
|
"build_llm",
|
|
"build_knowledge_base",
|
|
"get_knowledge_base",
|
|
"search_knowledge_base",
|
|
"add_to_knowledge_base",
|
|
"run_interactive_cli",
|
|
]
|
|
|
|
|
|
def run_interactive_cli() -> None:
|
|
agent = build_agent()
|
|
print("RAG-агент (Qdrant + Ollama). Команды: /add, /search, /quit")
|
|
print(" /add <заголовок> | <текст>")
|
|
print(" /search <запрос>")
|
|
print(" или задайте вопрос агенту\n")
|
|
|
|
while True:
|
|
user_input = input("Вы: ").strip()
|
|
if not user_input:
|
|
continue
|
|
if user_input.lower() in ("/quit", "exit", "quit", "q"):
|
|
print("До встречи!")
|
|
break
|
|
|
|
if user_input.startswith("/search "):
|
|
query = user_input[len("/search ") :].strip()
|
|
print(search_knowledge_base.invoke({"query": query, "max_results": 5}))
|
|
continue
|
|
|
|
if user_input.startswith("/add "):
|
|
payload = user_input[len("/add ") :].strip()
|
|
if "|" not in payload:
|
|
print("Формат: /add <заголовок> | <текст>")
|
|
continue
|
|
title, content = (part.strip() for part in payload.split("|", 1))
|
|
print(add_to_knowledge_base.invoke({"content": content, "title": title}))
|
|
continue
|
|
|
|
result = agent.invoke(
|
|
{"messages": [{"role": "human", "content": user_input}]}
|
|
)
|
|
answer = result["messages"][-1]
|
|
print(f"\nАгент: {getattr(answer, 'content', answer)}\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_interactive_cli()
|