Gradio_chat / chat_agent.py
princeta3011's picture
Upload 11 files
69a077e verified
# chat_agent.py
import os
import re
from openai import OpenAI
from main import WebScrapingOrchestrator
class SimpleChatAgent:
def __init__(self):
self.client = OpenAI(
base_url="https://api.studio.nebius.com/v1/",
api_key=os.environ.get("NEBIUS_API_KEY"),
)
self.model = "meta-llama/Meta-Llama-3.1-70B-Instruct"
self.orchestrator = WebScrapingOrchestrator()
async def handle_query(self, user_input, history):
# Web scraping check
url_match = re.search(r"(https?://[^\s]+)", user_input)
if "scrape" in user_input.lower() and url_match:
url = url_match.group(1)
result = await self.orchestrator.process_url(url)
if "error" in result:
return f"❌ Error scraping {url}: {result['error']}"
return (
f"βœ… Scraped Data from {result['title']}:\n"
f"- Topics: {', '.join(result['llm_ready_data']['main_topics'])}\n"
f"- Summary: {result['llm_ready_data']['text_summary'][:500]}..."
)
# Build full chat history
messages = []
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": user_input})
# Call Nebius LLM
response = self.client.chat.completions.create(
model=self.model,
messages=messages,
temperature=0.6,
)
return response.choices[0].message.content