fix(needs_fixes): 1 исправлений, 0 отстояно — main.py

This commit is contained in:
2026-06-30 20:18:07 +00:00
parent dbe24c8ecb
commit 64b40c9b92
+94 -106
View File
@@ -1,146 +1,134 @@
import os import os
import asyncio import asyncio
import argparse
from pathlib import Path from pathlib import Path
from langchain_openai import ChatOpenAI, OpenAIEmbeddings 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
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
# -------------------- 1. LLM and Embeddings -------------------- # ===================== CONFIG =====================
llm = ChatOpenAI( OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
model="openai/gpt-oss-20b:free", if not OPENAI_API_KEY:
base_url="https://openrouter.ai/api/v1", raise RuntimeError("OPENAI_API_KEY is not set in environment")
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0.0,
)
# ===================== EMBEDDINGS & VECTOR STORE =====================
embeddings = OpenAIEmbeddings( embeddings = OpenAIEmbeddings(
model="text-embedding-3-small", model="text-embedding-3-small",
base_url="https://openrouter.ai/api/v1", base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENAI_API_KEY"), api_key=OPENAI_API_KEY,
) )
# -------------------- 2. Chroma DB --------------------
CHROMA_PATH = Path("./chroma_faq")
CHROMA_COLLECTION = "course_faq"
vector_store = Chroma( vector_store = Chroma(
collection_name=CHROMA_COLLECTION, collection_name="faq_knowledge",
embedding_function=embeddings, embedding_function=embeddings,
persist_directory=str(CHROMA_PATH), persist_directory="./chroma_faq",
) )
# Persist changes # ===================== TOOL: SEARCH IN CHROMA =====================
vector_store.persist()
# -------------------- 3. Tools --------------------
@tool @tool
def search_knowledge(query: str) -> str: def search_course_docs(query: str, k: int = 3) -> str:
"""Search the knowledge base for relevant information.""" """Search the local FAQ knowledge base for relevant passages."""
docs = vector_store.similarity_search(query, k=3) docs: list[Document] = vector_store.similarity_search(query, k=k)
return "\n".join(d.page_content for d in docs) if docs else "No results found in course materials." if not docs:
return "No relevant information found in the course materials."
return "\n\n---\n\n".join(f"**{d.metadata.get('title', 'Untitled')}**\n{d.page_content}" for d in docs)
# ===================== TOOL: FETCH METADATA (MCPSTYLE) =====================
# For the purpose of this assignment we use a static JSON file as the mock MCP server response.
METADATA_JSON = {
"schedule": {
"Monday": "Lecture 1: Introduction",
"Wednesday": "Lecture 2: Advanced Topics",
"Friday": "Lecture 3: Practical Applications"
},
"instructors": {
"Dr. Smith": "smith@example.com",
"Prof. Doe": "doe@example.com"
}
}
@tool @tool
def fetch_course_meta(query: str) -> str: def fetch_course_meta(query: str) -> str:
"""Fetch course metadata (schedule, syllabus, etc.) from a static JSON file.""" """Mock MCP tool that returns course metadata based on the query.
meta_path = Path("meta.json") In production this would be an HTTP GET to an MCP server.
if not meta_path.exists(): """
return "Metadata file not found." query = query.lower()
import json if "schedule" in query:
data = json.loads(meta_path.read_text()) return "\n".join(f"{day}: {info}" for day, info in METADATA_JSON["schedule"].items())
query_lower = query.lower() if "instructor" in query or "email" in query:
if "schedule" in query_lower: return "\n".join(f"{name}: {email}" for name, email in METADATA_JSON["instructors"].items())
return f"Course schedule: {data.get('schedule', 'Not available')}" return "No metadata matches your query."
if "syllabus" in query_lower:
return f"Syllabus URL: {data.get('syllabus_url', 'Not available')}" # ===================== LOAD FAQ TO CHROMA =====================
if "instructor" in query_lower: MD_DIR = Path("data")
return f"Instructor: {data.get('instructor', 'Not available')}" if not MD_DIR.exists():
# Default: return all MD_DIR.mkdir(parents=True, exist_ok=True)
return json.dumps(data, indent=2) # Create example markdown files if none exist
(MD_DIR / "faq1.md").write_text("# FAQ 1\nWhat is the course about?\nThe course covers advanced AI techniques.")
(MD_DIR / "faq2.md").write_text("# FAQ 2\nHow to install dependencies?\nRun `pip install -r requirements.txt`.")
(MD_DIR / "faq3.md").write_text("# FAQ 3\nWhere to find the schedule?\nCheck the course website.")
# Chunking and persisting
from langchain_text_splitters import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
for md_file in MD_DIR.glob("*.md"):
content = md_file.read_text(encoding="utf-8")
docs = [Document(page_content=chunk, metadata={"title": md_file.stem}) for chunk in text_splitter.split_text(content)]
vector_store.add_documents(docs)
vector_store.persist()
# ===================== AGENT SETUP =====================
llm = ChatOpenAI(
model="openai/gpt-oss-20b:free",
base_url="https://openrouter.ai/api/v1",
api_key=OPENAI_API_KEY,
temperature=0.0,
)
# -------------------- 4. Backend --------------------
backend = CompositeBackend([ backend = CompositeBackend([
LocalShellBackend(workspace_dir="./workspace"), LocalShellBackend(workspace_dir="./workspace"),
FilesystemBackend(), FilesystemBackend(),
]) ])
# -------------------- 5. Agent --------------------
SYSTEM_PROMPT = (
"You are a helpful FAQ bot for the course.\n"
"Use the 'search_knowledge' tool to answer questions about course materials.\n"
"Use the 'fetch_course_meta' tool for questions about schedule, syllabus, instructor, etc.\n"
"Do not call both tools unnecessarily.\n"
"In your final answer, prepend 'source: chroma' or 'source: mcp_meta' to indicate which tool provided the information."
)
agent = create_deep_agent( agent = create_deep_agent(
model=llm, model=llm,
tools=[search_knowledge, fetch_course_meta], tools=[search_course_docs, fetch_course_meta],
backend=backend, backend=backend,
system_prompt=SYSTEM_PROMPT, system_prompt="You are a helpful FAQ assistant. Use only the provided tools. In your final answer, prefix the source with `source: chroma` or `source: mcp_meta` accordingly.",
) )
# -------------------- 6. Data Loader -------------------- # ===================== CLI =====================
PRESET_QUESTIONS = [
"What is the course about?", # chroma
"How to install dependencies?", # chroma
"What is the lecture schedule?", # mcp_meta
]
def load_faq_to_chroma(): async def run_cli():
data_dir = Path("data") print("=== FAQ Assistant ===")
if not data_dir.exists(): print("Type your question or 'exit' to quit.")
data_dir.mkdir() for i, q in enumerate(PRESET_QUESTIONS, 1):
# Create sample markdown files if missing print(f"\nPreset {i}: {q}")
(data_dir / "file1.md").write_text( await handle_question(q)
"## Course Overview\nThis course covers advanced topics in AI. Topics include machine learning, deep learning, and natural language processing."
)
(data_dir / "file2.md").write_text(
"## FAQ\nQ: What is the schedule?\nA: Sessions are held on Mondays and Wednesdays.\nQ: Where can I find the syllabus?\nA: Syllabus is available on the course website."
)
docs = []
for md_file in data_dir.glob("*.md"):
text = md_file.read_text()
docs.append(Document(page_content=text, metadata={"source": md_file.name}))
vector_store.add_documents(docs)
vector_store.persist()
print(f"Loaded {len(docs)} documents into Chroma collection '{CHROMA_COLLECTION}'.")
# -------------------- 7. CLI --------------------
PRESET_QUESTIONS = {
"1": "What topics are covered in the course?",
"2": "Where can I find the syllabus?",
"3": "What is the course schedule?",
}
async def run_question(question: str, thread_id: str = "session-1"):
result = await agent.ainvoke(
{"messages": ["HumanMessage(content=\"{}\")".format(question)]},
{"configurable": {"thread_id": thread_id}},
)
# The agent returns a dict with 'messages'; extract last message
content = result["messages"][-1].content
print(content)
async def main():
# Ensure data is loaded
load_faq_to_chroma()
parser = argparse.ArgumentParser(description="FAQ Bot CLI")
parser.add_argument("--preset", choices=["1", "2", "3"], help="Run a preset question")
args = parser.parse_args()
if args.preset:
question = PRESET_QUESTIONS[args.preset]
print(f"Preset question {args.preset}: {question}")
await run_question(question)
else:
print("Enter your question (type 'exit' to quit):")
while True: while True:
q = input("> ") user_input = input("\nYour question: ")
if q.lower() in {"exit", "quit"}: if user_input.lower() in {"exit", "quit"}:
break break
if q.strip(): await handle_question(user_input)
await run_question(q)
async def handle_question(question: str):
result = await agent.ainvoke(
{"messages": [HumanMessage(content=question)]},
{"configurable": {"thread_id": "session-1"}},
)
answer = result["messages"][-1].content
print("\nAnswer:\n", answer)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(run_cli())