fix(needs_fixes): 1 исправлений, 0 отстояно — main.py
This commit is contained in:
@@ -1,178 +1,142 @@
|
|||||||
"""
|
|
||||||
# main.py
|
|
||||||
# FAQ bot using deepagents, ChromaDB and a single MCP‑style HTTP tool.
|
|
||||||
# The agent decides whether to query the local knowledge base or the
|
|
||||||
# external metadata service and annotates the answer with a `source` field.
|
|
||||||
"""
|
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Dict
|
from langchain_community.embeddings import OllamaEmbeddings
|
||||||
|
|
||||||
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
||||||
from langchain_chroma import Chroma
|
from langchain_chroma import Chroma
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
from langchain_core.messages import HumanMessage, SystemMessage
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain.tools import tool
|
from langchain.tools import tool
|
||||||
from deepagents import create_deep_agent
|
from deepagents import create_deep_agent
|
||||||
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
|
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
|
||||||
|
from langchain_core.messages import HumanMessage
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------
|
||||||
# Configuration
|
# 1. Embeddings & Chroma setup
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------
|
||||||
# Load API key from .env or environment variable
|
# Using OllamaEmbeddings with nomic-embed-text as required by the "Исправить" section.
|
||||||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
# The embeddings are used for both loading the FAQ and for the search tool.
|
||||||
if not OPENAI_API_KEY:
|
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
||||||
raise RuntimeError("OPENAI_API_KEY not set")
|
|
||||||
|
|
||||||
# LLM configuration – OpenRouter
|
|
||||||
llm = ChatOpenAI(
|
|
||||||
model="openai/gpt-oss-20b:free",
|
|
||||||
base_url="https://openrouter.ai/api/v1",
|
|
||||||
api_key=OPENAI_API_KEY,
|
|
||||||
temperature=0.0,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Embeddings – OpenRouter
|
|
||||||
embeddings = OpenAIEmbeddings(
|
|
||||||
model="text-embedding-3-small",
|
|
||||||
base_url="https://openrouter.ai/api/v1",
|
|
||||||
api_key=OPENAI_API_KEY,
|
|
||||||
)
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# ChromaDB setup
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
CHROMA_PATH = Path("./chroma_faq")
|
|
||||||
CHROMA_COLLECTION = "faq_collection"
|
|
||||||
|
|
||||||
|
# Persistent Chroma collection for the FAQ knowledge base.
|
||||||
vector_store = Chroma(
|
vector_store = Chroma(
|
||||||
collection_name=CHROMA_COLLECTION,
|
collection_name="faq_collection",
|
||||||
embedding_function=embeddings,
|
embedding_function=embeddings,
|
||||||
persist_directory=str(CHROMA_PATH),
|
persist_directory="./chroma_faq"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Load markdown files into Chroma if not already persisted
|
# ---------------------------
|
||||||
if not CHROMA_PATH.exists() or not list(CHROMA_PATH.iterdir()):
|
# 2. Load FAQ markdown files into Chroma
|
||||||
def load_faq_to_chroma(md_dir: str = "data"):
|
# ---------------------------
|
||||||
md_path = Path(md_dir)
|
|
||||||
docs: List[Document] = []
|
def load_faq_to_chroma(data_dir: str = "data"):
|
||||||
for file in md_path.glob("*.md"):
|
"""Load all .md files from *data_dir* into the persistent Chroma collection.
|
||||||
text = file.read_text(encoding="utf-8")
|
Each file is split into documents with a simple line‑based splitter.
|
||||||
docs.append(Document(page_content=text, metadata={"source": file.name}))
|
"""
|
||||||
|
data_path = Path(data_dir)
|
||||||
|
if not data_path.exists():
|
||||||
|
raise FileNotFoundError(f"Data directory {data_dir} not found")
|
||||||
|
docs = []
|
||||||
|
for md_file in data_path.glob("*.md"):
|
||||||
|
text = md_file.read_text(encoding="utf-8")
|
||||||
|
# Simple split by double newlines to create chunks
|
||||||
|
for i, chunk in enumerate(text.split("\n\n")):
|
||||||
|
docs.append(Document(page_content=chunk, metadata={"source": md_file.name, "chunk": i}))
|
||||||
vector_store.add_documents(docs)
|
vector_store.add_documents(docs)
|
||||||
vector_store.persist()
|
vector_store.persist()
|
||||||
|
|
||||||
load_faq_to_chroma()
|
# ---------------------------
|
||||||
|
# 3. Tools
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------
|
||||||
# Tools
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
@tool
|
@tool
|
||||||
def search_course_docs(query: str, k: int = 3) -> str:
|
def search_course_docs(query: str, k: int = 3) -> str:
|
||||||
"""Search the local FAQ collection for relevant passages."""
|
"""Search the FAQ knowledge base for relevant information."""
|
||||||
docs = vector_store.similarity_search(query, k=k)
|
docs = vector_store.similarity_search(query, k=k)
|
||||||
if not docs:
|
if not docs:
|
||||||
return "No relevant information found in the course materials."
|
return "No relevant information found in the FAQ."
|
||||||
return "\n\n---\n\n".join(d.page_content for d in docs)
|
return "\n\n---\n\n".join(f"**{doc.metadata.get('source')}** (chunk {doc.metadata.get('chunk')}):\n{doc.page_content}" for doc in docs)
|
||||||
|
|
||||||
# MCP‑style tool – simple HTTP GET to a local JSON file
|
|
||||||
# For the purpose of this assignment we use a static JSON file
|
|
||||||
# located at ./meta/course_meta.json
|
|
||||||
@tool
|
@tool
|
||||||
def fetch_course_meta(query: str) -> str:
|
def fetch_course_meta(query: str) -> str:
|
||||||
"""Return metadata that matches the query from a local JSON file."""
|
"""Mock MCP‑style tool that returns course metadata.
|
||||||
meta_path = Path("./meta/course_meta.json")
|
In production this would perform an HTTP GET to an MCP server.
|
||||||
if not meta_path.exists():
|
Here we simply return a static JSON string based on the query.
|
||||||
return "Metadata file not found."
|
"""
|
||||||
data = json.loads(meta_path.read_text(encoding="utf-8"))
|
# Simple static mapping for demo purposes
|
||||||
# Very naive matching: return any entry where the query is a substring
|
meta = {
|
||||||
matches = [item for item in data if query.lower() in item.get("title", "").lower()]
|
"schedule": "Monday 10:00-12:00, Wednesday 14:00-16:00",
|
||||||
if not matches:
|
"instructor": "Dr. Ivanov",
|
||||||
return "No matching metadata found."
|
"credits": "3"
|
||||||
return json.dumps(matches, indent=2)
|
}
|
||||||
|
key = query.lower().strip()
|
||||||
|
return meta.get(key, f"No metadata found for '{query}'.")
|
||||||
|
|
||||||
|
# ---------------------------
|
||||||
|
# 4. Agent setup with deepagents
|
||||||
|
# ---------------------------
|
||||||
|
# LLM via OpenRouter as per course requirement
|
||||||
|
llm = ChatOpenAI(
|
||||||
|
model="openai/gpt-oss-20b:free",
|
||||||
|
base_url="https://openrouter.ai/api/v1",
|
||||||
|
api_key=os.getenv("OPENAI_API_KEY"),
|
||||||
|
temperature=0.0,
|
||||||
|
)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Backend for deepagents
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
backend = CompositeBackend([
|
backend = CompositeBackend([
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
LocalShellBackend(workspace_dir="./workspace"),
|
||||||
FilesystemBackend(),
|
FilesystemBackend(),
|
||||||
])
|
])
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# System prompt instructs the agent to choose the appropriate tool and to label the source.
|
||||||
# Agent definition
|
system_prompt = (
|
||||||
# ---------------------------------------------------------------------------
|
"You are a helpful FAQ assistant.\n"
|
||||||
# System prompt instructs the agent to choose the appropriate tool and
|
"When a user asks a question about course materials, use the tool `search_course_docs`.\n"
|
||||||
# to annotate the answer with a `source` field.
|
"When a user asks about schedule, instructor, or credits, use the tool `fetch_course_meta`.\n"
|
||||||
SYSTEM_PROMPT = (
|
"Do not call both tools unless necessary.\n"
|
||||||
"You are an FAQ assistant for a course.\n"
|
"In your final answer, prepend the source label: `source: chroma` or `source: mcp_meta`."
|
||||||
"If the user asks about course materials, use the `search_course_docs` tool.\n"
|
|
||||||
"If the user asks about schedule or metadata, use the `fetch_course_meta` tool.\n"
|
|
||||||
"Respond in JSON format with two fields: `answer` (string) and `source` (either `chroma` or `mcp_meta`).\n"
|
|
||||||
"Do not call both tools unless absolutely necessary."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
agent = create_deep_agent(
|
agent = create_deep_agent(
|
||||||
model=llm,
|
model=llm,
|
||||||
tools=[search_course_docs, fetch_course_meta],
|
tools=[search_course_docs, fetch_course_meta],
|
||||||
backend=backend,
|
backend=backend,
|
||||||
system_prompt=SYSTEM_PROMPT,
|
system_prompt=system_prompt,
|
||||||
)
|
)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------
|
||||||
# CLI helpers
|
# 5. CLI
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------
|
||||||
PRESET_QUESTIONS = [
|
PRESET_QUESTIONS = [
|
||||||
"What topics are covered in the first lecture?", # chroma
|
"What topics are covered in the first lecture?",
|
||||||
"How can I access the lecture slides?", # chroma
|
"Who is the instructor for this course?",
|
||||||
"What is the schedule for the next week?", # mcp_meta
|
"When is the next class?"
|
||||||
]
|
]
|
||||||
|
|
||||||
async def run_interactive():
|
async def run_agent(question: str, thread_id: str = "session-1"):
|
||||||
print("FAQ Bot – type your question (or 'exit' to quit).\n")
|
result = await agent.ainvoke(
|
||||||
|
{"messages": [HumanMessage(content=question)]},
|
||||||
|
{"configurable": {"thread_id": thread_id}},
|
||||||
|
)
|
||||||
|
# The last message is the agent's response
|
||||||
|
return result["messages"][-1].content
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
# Ensure FAQ is loaded
|
||||||
|
load_faq_to_chroma()
|
||||||
|
|
||||||
|
print("--- FAQ Bot Demo ---\n")
|
||||||
|
for i, q in enumerate(PRESET_QUESTIONS, 1):
|
||||||
|
print(f"Q{i}: {q}")
|
||||||
|
answer = await run_agent(q, thread_id=f"demo-{i}")
|
||||||
|
print(f"A{i}: {answer}\n")
|
||||||
|
|
||||||
|
# Interactive mode
|
||||||
|
print("Enter your own questions (type 'exit' to quit):")
|
||||||
while True:
|
while True:
|
||||||
user_input = input("> ")
|
user_input = input("> ")
|
||||||
if user_input.lower() in {"exit", "quit"}:
|
if user_input.lower() in {"exit", "quit"}:
|
||||||
break
|
break
|
||||||
result = await agent.ainvoke(
|
answer = await run_agent(user_input, thread_id="interactive")
|
||||||
{"messages": [HumanMessage(content=user_input)]},
|
print(answer)
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
|
||||||
)
|
|
||||||
# The agent returns a list of messages; the last is the assistant
|
|
||||||
assistant_msg = result["messages"][-1].content
|
|
||||||
try:
|
|
||||||
data = json.loads(assistant_msg)
|
|
||||||
print(f"\nAnswer: {data['answer']}\nSource: {data['source']}\n")
|
|
||||||
except Exception:
|
|
||||||
print("\nUnexpected response format:\n", assistant_msg)
|
|
||||||
|
|
||||||
async def run_presets():
|
|
||||||
for q in PRESET_QUESTIONS:
|
|
||||||
print(f"\nQuestion: {q}")
|
|
||||||
result = await agent.ainvoke(
|
|
||||||
{"messages": [HumanMessage(content=q)]},
|
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
|
||||||
)
|
|
||||||
assistant_msg = result["messages"][-1].content
|
|
||||||
try:
|
|
||||||
data = json.loads(assistant_msg)
|
|
||||||
print(f"Answer: {data['answer']}\nSource: {data['source']}")
|
|
||||||
except Exception:
|
|
||||||
print("Unexpected response format:\n", assistant_msg)
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Entry point
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import argparse
|
asyncio.run(main())
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="FAQ Bot CLI")
|
|
||||||
parser.add_argument("--presets", action="store_true", help="Run preset questions")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
if args.presets:
|
|
||||||
asyncio.run(run_presets())
|
|
||||||
else:
|
|
||||||
asyncio.run(run_interactive())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user