Initial implementation of FAQ bot with ChromaDB and MCP-style tool: update src/agent.py
This commit is contained in:
+45
-25
@@ -1,37 +1,57 @@
|
|||||||
import os
|
import os
|
||||||
from langchain.agents import initialize_agent, Tool
|
from typing import Dict, Any
|
||||||
from langchain.llms import Ollama
|
from langchain_ollama import OllamaLLM
|
||||||
from langchain.chains import RetrievalQA
|
from langchain.agents import initialize_agent, Tool, AgentType
|
||||||
from langchain_ollama import OllamaEmbeddings
|
from langchain.memory import ConversationBufferMemory
|
||||||
from langchain_chroma import Chroma
|
from src.utils import search_course_docs, fetch_course_meta
|
||||||
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
|
# Define tools
|
||||||
search_tool = Tool(
|
search_tool = Tool(
|
||||||
name="search_course_docs",
|
name="search_course_docs",
|
||||||
func=lambda q: "\n".join([doc.page_content for doc in search_course_docs(q, k=3)]),
|
func=search_course_docs,
|
||||||
description="Search local FAQ documents. Use when question is about course content."
|
description="Search local FAQ docs in ChromaDB. Use when question about course content. Returns list of relevant documents."
|
||||||
)
|
)
|
||||||
|
|
||||||
meta_tool = Tool(
|
meta_tool = Tool(
|
||||||
name="fetch_course_meta",
|
name="fetch_course_meta",
|
||||||
func=lambda q: str(fetch_course_meta(q)),
|
func=fetch_course_meta,
|
||||||
description="Get course metadata like schedule or instructor. Use when question is about schedule or meta."
|
description="Fetch course metadata (schedule, exams) from MCP-style tool. Use when question about schedule or metadata. Returns list of matching items."
|
||||||
)
|
)
|
||||||
|
|
||||||
# LLM and agent
|
# System prompt to guide routing
|
||||||
llm = Ollama(model="llama3.1")
|
SYSTEM_PROMPT = (
|
||||||
agent = initialize_agent([search_tool, meta_tool], llm, agent_type="zero-shot-react-description", verbose=True)
|
"You are an FAQ bot for the course. Use search_course_docs for content questions and fetch_course_meta for schedule or metadata questions.\n"
|
||||||
|
"When answering, include a field 'source' with value 'chroma' or 'mcp_meta' to indicate which tool was used."
|
||||||
|
)
|
||||||
|
|
||||||
|
# LLM and agent setup
|
||||||
|
llm = OllamaLLM(model="llama3")
|
||||||
|
memory = ConversationBufferMemory(memory_key="chat_history")
|
||||||
|
agent = initialize_agent(
|
||||||
|
tools=[search_tool, meta_tool],
|
||||||
|
llm=llm,
|
||||||
|
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
|
||||||
|
memory=memory,
|
||||||
|
verbose=True,
|
||||||
|
system_prompt=SYSTEM_PROMPT,
|
||||||
|
)
|
||||||
|
|
||||||
|
def ask(question: str) -> Dict[str, Any]:
|
||||||
|
response = agent.run(question)
|
||||||
|
# Parse response to extract source if present
|
||||||
|
source = "unknown"
|
||||||
|
if "source:" in response.lower():
|
||||||
|
parts = response.lower().split("source:")
|
||||||
|
source = parts[1].strip().split()[0]
|
||||||
|
return {"answer": response, "source": source}
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("FAQ-бот готов. Введите вопрос (или 'exit'): ")
|
# Simple CLI with 3 preset questions
|
||||||
while True:
|
questions = [
|
||||||
q = input("> ")
|
"Как подключить ChromaDB?",
|
||||||
if q.lower() in {"exit", "quit"}:
|
"Что такое MCP‑tool?",
|
||||||
break
|
"Когда проходят экзамены?"
|
||||||
response = agent.run(q)
|
]
|
||||||
print(response)
|
for q in questions:
|
||||||
|
print("Q:", q)
|
||||||
|
print("A:", ask(q)["answer"], "(source:", ask(q)["source"], ")")
|
||||||
|
print()
|
||||||
|
|||||||
Reference in New Issue
Block a user