Spaces:
Running
Running
File size: 12,256 Bytes
93bede5 3b6e4a1 93bede5 3b6e4a1 93bede5 3b6e4a1 93bede5 3b6e4a1 93bede5 3b6e4a1 93bede5 3b6e4a1 93bede5 3b6e4a1 691b503 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
import os
import gradio as gr
import requests
import json
import asyncio
from typing import List, Dict, Any, Generator
import logging
from duckduckgo_search import DDGS
from bs4 import BeautifulSoup
import re
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configuration from environment variables with defaults
DEFAULT_IP = {public_ip}
DEFAULT_PORT = {port}
DEFAULT_KEY = {api_key}
DEFAULT_MODEL = {model}
llm_ip = os.environ.get('LLM_IP', DEFAULT_IP)
llm_port = os.environ.get('LLM_PORT', DEFAULT_PORT)
llm_key = os.environ.get('LLM_KEY', DEFAULT_KEY)
llm_model = os.environ.get('LLM_MODEL', DEFAULT_MODEL)
class WebTools:
def __init__(self):
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
})
self.ddgs = DDGS()
def search_web(self, query: str, max_results: int = 5) -> str:
"""Search the web using DuckDuckGo"""
try:
results = self.ddgs.text(query, max_results=max_results)
if not results:
return f"No search results found for: {query}"
formatted_results = f"Search results for '{query}':\n\n"
for i, result in enumerate(results, 1):
title = result.get('title', 'No title')
body = result.get('body', 'No description')
href = result.get('href', 'No URL')
formatted_results += f"{i}. **{title}**\n{body}\nURL: {href}\n\n"
return formatted_results
except Exception as e:
logger.error(f"Search error: {e}")
return f"Search error: {str(e)}"
def visit_website(self, url: str) -> str:
"""Visit a website and extract its text content"""
try:
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
response = self.session.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Remove script and style elements
for script in soup(["script", "style", "nav", "footer", "header"]):
script.decompose()
# Get text content
text = soup.get_text()
# Clean up text
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = ' '.join(chunk for chunk in chunks if chunk)
# Limit text length
if len(text) > 3000:
text = text[:3000] + "... (content truncated)"
return f"Content from {url}:\n\n{text}"
except Exception as e:
logger.error(f"Website visit error: {e}")
return f"Error visiting {url}: {str(e)}"
class LLMClient:
def __init__(self, ip: str, port: str, api_key: str, model: str):
self.ip = ip
self.port = port
self.api_key = api_key
self.model = model
self.base_url = f"http://{ip}:{port}/v1/chat/completions"
def call_llm(self, messages: List[Dict], max_tokens: int = 512, stream: bool = False):
"""Call the LLM API"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}"
}
data = {
"model": self.model,
"messages": messages,
"max_tokens": max_tokens,
"stream": stream
}
try:
response = requests.post(self.base_url, headers=headers, json=data,
stream=stream, timeout=30)
response.raise_for_status()
if stream:
return response
else:
result = response.json()
return result["choices"][0]["message"]["content"]
except Exception as e:
logger.error(f"LLM API call failed: {e}")
return f"Error: {str(e)}"
class ReactAgent:
def __init__(self, llm_client: LLMClient):
self.llm_client = llm_client
self.web_tools = WebTools()
self.system_prompt = """You are a helpful AI assistant with access to web browsing capabilities. You can:
1. Search the web using DuckDuckGo
2. Visit and analyze websites
3. Answer questions based on current information
When a user asks something that requires current information or web searching, use the available tools.
Available tools:
- search_web(query): Search DuckDuckGo for information
- visit_website(url): Visit and extract content from a website
Format your tool calls as: TOOL[tool_name: parameters]
For example: TOOL[search_web: latest news about AI] or TOOL[visit_website: https://example.com]
Always explain what you're doing and provide helpful responses based on the information you gather."""
def parse_tool_calls(self, text: str) -> List[Dict]:
"""Parse tool calls from agent response"""
tool_pattern = r'TOOL\[(\w+):\s*([^\]]+)\]'
matches = re.findall(tool_pattern, text)
tools = []
for tool_name, params in matches:
tools.append({
'name': tool_name,
'params': params.strip()
})
return tools
def execute_tool(self, tool_name: str, params: str) -> str:
"""Execute a tool and return results"""
try:
if tool_name == 'search_web':
return self.web_tools.search_web(params)
elif tool_name == 'visit_website':
return self.web_tools.visit_website(params)
else:
return f"Unknown tool: {tool_name}"
except Exception as e:
return f"Tool execution error: {str(e)}"
def process_message(self, message: str, history: List[List[str]], max_tokens: int) -> Generator[str, None, None]:
"""Process user message with ReAct pattern"""
try:
# Format chat history
messages = [{"role": "system", "content": self.system_prompt}]
for user_msg, assistant_msg in history:
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
# Initial LLM call
response = self.llm_client.call_llm(messages, max_tokens, stream=True)
current_response = ""
tool_calls_made = False
# Stream initial response
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_str = line[6:]
if data_str.strip() == '[DONE]':
break
try:
data = json.loads(data_str)
if 'choices' in data and len(data['choices']) > 0:
delta = data['choices'][0].get('delta', {})
content = delta.get('content', '')
if content:
current_response += content
yield current_response
except json.JSONDecodeError:
continue
# Check for tool calls
tool_calls = self.parse_tool_calls(current_response)
if tool_calls:
tool_calls_made = True
for tool_call in tool_calls:
yield current_response + f"\n\nπ Executing {tool_call['name']}..."
tool_result = self.execute_tool(tool_call['name'], tool_call['params'])
# Add tool result to conversation
messages.append({"role": "assistant", "content": current_response})
messages.append({"role": "user", "content": f"Tool result:\n{tool_result}\n\nPlease provide a helpful response based on this information."})
# Get final response
final_response = self.llm_client.call_llm(messages, max_tokens, stream=True)
final_text = current_response + f"\n\n**Tool Results:**\n{tool_result}\n\n**Response:**\n"
for line in final_response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_str = line[6:]
if data_str.strip() == '[DONE]':
break
try:
data = json.loads(data_str)
if 'choices' in data and len(data['choices']) > 0:
delta = data['choices'][0].get('delta', {})
content = delta.get('content', '')
if content:
final_text += content
yield final_text
except json.JSONDecodeError:
continue
break # Only handle first tool call for now
except Exception as e:
error_msg = f"Agent error: {str(e)}"
logger.error(error_msg)
yield error_msg
# Initialize components
llm_client = LLMClient(llm_ip, llm_port, llm_key, llm_model)
agent = ReactAgent(llm_client)
def generate_response(message: str, history: List[List[str]], system_prompt: str,
max_tokens: int, ip: str, port: str, api_key: str, model: str):
"""Generate streaming response using the agent"""
global llm_client, agent
# Update LLM client if parameters changed
if (ip != llm_client.ip or port != llm_client.port or
api_key != llm_client.api_key or model != llm_client.model):
llm_client = LLMClient(ip, port, api_key, model)
agent = ReactAgent(llm_client)
# Update system prompt if provided
if system_prompt.strip():
agent.system_prompt = system_prompt
# Generate response
for response in agent.process_message(message, history, max_tokens):
yield response
# Create Gradio interface
chatbot = gr.ChatInterface(
generate_response,
chatbot=gr.Chatbot(
avatar_images=[
None,
"https://cdn-avatars.huggingface.co/v1/production/uploads/64e6d37e02dee9bcb9d9fa18/o_HhUnXb_PgyYlqJ6gfEO.png"
],
height="64vh"
),
additional_inputs=[
gr.Textbox(
"You are a helpful AI assistant with web browsing capabilities. You can search the web and visit websites to provide current information. Use TOOL[search_web: query] to search or TOOL[visit_website: url] to browse websites.",
label="System Prompt",
lines=3
),
gr.Slider(50, 2048, label="Max Tokens", value=512,
info="Maximum number of tokens in the response"),
gr.Textbox(llm_ip, label="LLM IP Address",
info="IP address of the LLM server"),
gr.Textbox(llm_port, label="LLM Port",
info="Port of the LLM server"),
gr.Textbox(llm_key, label="API Key", type="password",
info="API key for the LLM server"),
gr.Textbox(llm_model, label="Model Name",
info="Name of the model to use"),
],
title="π€ AI Agent with Web Browsing",
description="Chat with an AI agent that can search the web and browse websites using DuckDuckGo. Use natural language to ask for current information!",
theme="finlaymacklon/smooth_slate",
submit_btn="Send",
retry_btn="π Regenerate Response",
undo_btn="β© Delete Previous",
clear_btn="ποΈ Clear Chat"
)
if __name__ == "__main__":
chatbot.queue().launch() |