csabakecskemeti commited on
Commit
efe6d5a
·
verified ·
1 Parent(s): 549d679

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -30
app.py CHANGED
@@ -4,6 +4,7 @@ from typing import List
4
  import logging
5
  import logging.handlers
6
  import json
 
7
  from langchain_openai import ChatOpenAI
8
  from langchain_core.tools import tool
9
  from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
@@ -41,6 +42,36 @@ else:
41
 
42
  logger = logging.getLogger(__name__)
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  # Configuration from environment variables
45
  llm_ip = os.environ.get('public_ip')
46
  llm_port = os.environ.get('port')
@@ -195,19 +226,14 @@ class ToolCallingAgentChat:
195
  logger.info(f"Message: {message}")
196
  logger.info(f"History length: {len(history)}")
197
 
198
- # Convert history to messages for context with system message
199
  from langchain_core.messages import SystemMessage
200
 
201
- messages = [SystemMessage(content="""You are a helpful AI assistant with web search capabilities.
202
-
203
- IMPORTANT: You have access to a web_search tool. Use it when users ask about:
204
- - Current events, news, or recent information
205
- - Today's date or current time
206
- - Real-time data or recent updates
207
- - Specific facts that may have changed recently
208
- - When users explicitly ask you to search the web
209
-
210
- For general knowledge questions that don't require current information, answer directly without using tools.""")]
211
 
212
  for user_msg, assistant_msg in history:
213
  messages.append(HumanMessage(content=user_msg))
@@ -318,14 +344,13 @@ For general knowledge questions that don't require current information, answer d
318
  # Global agent instance
319
  tool_calling_agent = ToolCallingAgentChat(llm_ip, llm_port, llm_key, llm_model)
320
 
321
- def generate_response(message: str, history: List[List[str]], system_prompt: str,
322
- max_tokens: int, ip: str, port: str, api_key: str, model: str):
323
- """Generate response using tool calling agent"""
324
  global tool_calling_agent
325
 
326
  try:
327
- # Update agent configuration if changed
328
- tool_calling_agent.update_config(ip, port, api_key, model)
329
 
330
  # Reset conversation if history is empty (indicates clear button was pressed)
331
  if len(history) == 0:
@@ -396,21 +421,8 @@ chatbot = gr.ChatInterface(
396
  elem_id="chatbot"
397
  ),
398
  additional_inputs=[
399
- gr.Textbox(
400
- "You are a helpful AI assistant with web search capabilities. Use web search when you need current information, recent events, or real-time data.",
401
- label="System Prompt",
402
- lines=2
403
- ),
404
- gr.Slider(50, 2048, label="Max Tokens", value=512,
405
  info="Maximum number of tokens in the response"),
406
- gr.Textbox(llm_ip, label="LLM IP Address",
407
- info="IP address of the OpenAI-compatible LLM server"),
408
- gr.Textbox(llm_port, label="LLM Port",
409
- info="Port of the LLM server"),
410
- gr.Textbox(llm_key, label="API Key", type="password",
411
- info="API key for the LLM server"),
412
- gr.Textbox(llm_model, label="Model Name",
413
- info="Name of the model to use"),
414
  ],
415
  title="🤖 DQ Micro Agent",
416
  description="DevQuasar self hosted Micro Agent with websearch capabilities",
 
4
  import logging
5
  import logging.handlers
6
  import json
7
+ from datetime import datetime
8
  from langchain_openai import ChatOpenAI
9
  from langchain_core.tools import tool
10
  from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
 
42
 
43
  logger = logging.getLogger(__name__)
44
 
45
+ def get_current_date() -> str:
46
+ """Get current date in YYYY-MM-DD format"""
47
+ return datetime.now().strftime("%Y-%m-%d")
48
+
49
+ def create_system_prompt() -> str:
50
+ """Create dynamic system prompt with current date"""
51
+ current_date = get_current_date()
52
+ return f"""You are a helpful AI assistant with web search capabilities.
53
+
54
+ TODAY'S DATE: {current_date}
55
+
56
+ IMPORTANT: You have access to a web_search tool. Consider your knowledge cutoff date and today's date to decide when to search:
57
+
58
+ USE WEB SEARCH when users ask about:
59
+ - Events after your knowledge cutoff date
60
+ - Current events, breaking news, or recent developments
61
+ - Today's date, current time, or "what's happening now"
62
+ - Real-time data (weather, stock prices, sports scores)
63
+ - Recent updates to ongoing situations
64
+ - Information that changes frequently
65
+ - When users explicitly ask you to search the web
66
+
67
+ DO NOT use web search for:
68
+ - Historical facts before your cutoff date
69
+ - General knowledge that doesn't change (capitals, basic science, etc.)
70
+ - Established facts and concepts
71
+ - Personal advice or opinions
72
+
73
+ When in doubt about whether information might be outdated, use web search to get the most current information."""
74
+
75
  # Configuration from environment variables
76
  llm_ip = os.environ.get('public_ip')
77
  llm_port = os.environ.get('port')
 
226
  logger.info(f"Message: {message}")
227
  logger.info(f"History length: {len(history)}")
228
 
229
+ # Convert history to messages for context with dynamic system message
230
  from langchain_core.messages import SystemMessage
231
 
232
+ # Create dynamic system prompt with current date
233
+ system_prompt = create_system_prompt()
234
+ if ENABLE_DETAILED_LOGGING:
235
+ logger.info(f"System prompt includes today's date: {get_current_date()}")
236
+ messages = [SystemMessage(content=system_prompt)]
 
 
 
 
 
237
 
238
  for user_msg, assistant_msg in history:
239
  messages.append(HumanMessage(content=user_msg))
 
344
  # Global agent instance
345
  tool_calling_agent = ToolCallingAgentChat(llm_ip, llm_port, llm_key, llm_model)
346
 
347
+ def generate_response(message: str, history: List[List[str]], max_tokens: int):
348
+ """Generate response using tool calling agent with dynamic system prompt"""
 
349
  global tool_calling_agent
350
 
351
  try:
352
+ # Configuration is pre-loaded from environment variables
353
+ # No runtime config changes allowed for security
354
 
355
  # Reset conversation if history is empty (indicates clear button was pressed)
356
  if len(history) == 0:
 
421
  elem_id="chatbot"
422
  ),
423
  additional_inputs=[
424
+ gr.Slider(50, 8192, label="Max Tokens", value=1024,
 
 
 
 
 
425
  info="Maximum number of tokens in the response"),
 
 
 
 
 
 
 
 
426
  ],
427
  title="🤖 DQ Micro Agent",
428
  description="DevQuasar self hosted Micro Agent with websearch capabilities",