feat: solution for 'Повторный экзамен: FAQ-бот — ChromaDB + один MCP-tool'
This commit is contained in:
+27
-50
@@ -1,64 +1,41 @@
|
||||
"""
|
||||
Main entry point for the FAQ bot.
|
||||
Command‑line interface for the FAQ bot.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import json
|
||||
from src.embedding import embed_text
|
||||
from src.database import ChromaDB
|
||||
from src.mcp_tools import generate_answer
|
||||
|
||||
def load_sample_faq() -> list:
|
||||
"""
|
||||
Load a small sample FAQ dataset.
|
||||
"""
|
||||
return [
|
||||
{"id": "0", "text": "What is the return policy? Our return policy allows returns within 30 days of purchase."},
|
||||
{"id": "1", "text": "How do I track my order? You can track your order using the tracking link sent to your email."},
|
||||
{"id": "2", "text": "What payment methods are accepted? We accept Visa, MasterCard, and PayPal."},
|
||||
{"id": "3", "text": "Do you ship internationally? Yes, we ship to most countries worldwide."},
|
||||
{"id": "4", "text": "How can I contact customer support? You can contact us via email at support@example.com."},
|
||||
]
|
||||
from .bot import FAQBot
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="FAQ Bot using ChromaDB and MCP-tools")
|
||||
parser.add_argument("--question", type=str, help="Your question to ask the bot")
|
||||
parser = argparse.ArgumentParser(description="FAQ Bot CLI")
|
||||
parser.add_argument(
|
||||
"--persist-dir",
|
||||
type=str,
|
||||
default="chromadb_persist",
|
||||
help="Directory to persist ChromaDB data",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--openai-key",
|
||||
type=str,
|
||||
default=os.getenv("OPENAI_API_KEY"),
|
||||
help="OpenAI API key (optional)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.question:
|
||||
print("Please provide a question using --question")
|
||||
return
|
||||
bot = FAQBot(persist_dir=args.persist_dir, openai_api_key=args.openai_key)
|
||||
|
||||
# Initialize database
|
||||
db = ChromaDB()
|
||||
|
||||
# If the collection is empty, load sample data
|
||||
if db.is_empty():
|
||||
print("Database empty. Loading sample FAQ data...")
|
||||
sample_data = load_sample_faq()
|
||||
for item in sample_data:
|
||||
text = item["text"]
|
||||
doc_id = item.get("id")
|
||||
embedding = embed_text(text)
|
||||
db.add_document(text, embedding, doc_id=doc_id)
|
||||
print("Sample data loaded.")
|
||||
|
||||
# Generate embedding for the question
|
||||
question_embedding = embed_text(args.question)
|
||||
|
||||
# Query the database for relevant documents
|
||||
results = db.query(question_embedding, k=5)
|
||||
|
||||
# Extract context documents
|
||||
context = [res["document"] for res in results]
|
||||
|
||||
# Generate answer using MCP-tools
|
||||
answer = generate_answer(context, args.question)
|
||||
|
||||
# Output the answer
|
||||
print("\nAnswer:")
|
||||
print(answer)
|
||||
print("FAQ Bot is ready. Type your question (Ctrl+C to exit).")
|
||||
while True:
|
||||
try:
|
||||
question = input("\n> ")
|
||||
if not question.strip():
|
||||
continue
|
||||
answer = bot.ask(question)
|
||||
print(f"\nAnswer: {answer}")
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
print("\nGoodbye!")
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user