Initial submission of FAQ-бот: add src/agent.py
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
import os
|
||||||
|
from langchain.agents import initialize_agent, Tool
|
||||||
|
from langchain.llms import Ollama
|
||||||
|
from langchain.chains import RetrievalQA
|
||||||
|
from langchain_ollama import OllamaEmbeddings
|
||||||
|
from langchain_chroma import Chroma
|
||||||
|
from src.utils import search_course_docs
|
||||||
|
from src.mcp_tool import fetch_course_meta, start_meta_server
|
||||||
|
|
||||||
|
# Start mock server
|
||||||
|
start_meta_server()
|
||||||
|
|
||||||
|
# Define tools
|
||||||
|
search_tool = Tool(
|
||||||
|
name="search_course_docs",
|
||||||
|
func=lambda q: "\n".join([doc.page_content for doc in search_course_docs(q, k=3)]),
|
||||||
|
description="Search local FAQ documents. Use when question is about course content."
|
||||||
|
)
|
||||||
|
|
||||||
|
meta_tool = Tool(
|
||||||
|
name="fetch_course_meta",
|
||||||
|
func=lambda q: str(fetch_course_meta(q)),
|
||||||
|
description="Get course metadata like schedule or instructor. Use when question is about schedule or meta."
|
||||||
|
)
|
||||||
|
|
||||||
|
# LLM and agent
|
||||||
|
llm = Ollama(model="llama3.1")
|
||||||
|
agent = initialize_agent([search_tool, meta_tool], llm, agent_type="zero-shot-react-description", verbose=True)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("FAQ-бот готов. Введите вопрос (или 'exit'): ")
|
||||||
|
while True:
|
||||||
|
q = input("> ")
|
||||||
|
if q.lower() in {"exit", "quit"}:
|
||||||
|
break
|
||||||
|
response = agent.run(q)
|
||||||
|
print(response)
|
||||||
Reference in New Issue
Block a user