milwright commited on
Commit
d3a61af
·
1 Parent(s): 796e312

append grounding urls to system message instead of user message

Browse files
Files changed (2) hide show
  1. app.py +19 -19
  2. space_template.py +8 -6
app.py CHANGED
@@ -537,23 +537,6 @@ class SpaceGenerator:
537
  "Content-Type": "application/json"
538
  }
539
 
540
- # Build messages for API
541
- messages = [{"role": "system", "content": config.get('system_prompt', 'You are a helpful AI assistant.')}]
542
-
543
- # Add conversation history
544
- for msg in chat_history:
545
- if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
546
- messages.append({
547
- "role": msg['role'],
548
- "content": msg['content']
549
- })
550
-
551
- # Add current message
552
- messages.append({
553
- "role": "user",
554
- "content": message
555
- })
556
-
557
  # Get grounding context from URLs if configured
558
  grounding_context = ""
559
  urls = config.get('grounding_urls', [])
@@ -567,9 +550,26 @@ class SpaceGenerator:
567
  except:
568
  pass
569
 
570
- # Add grounding context to the user message if available
 
571
  if grounding_context:
572
- messages[-1]["content"] = f"{grounding_context}\n\n{message}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
573
 
574
  data = {
575
  "model": config.get('model', 'openai/gpt-3.5-turbo'),
 
537
  "Content-Type": "application/json"
538
  }
539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540
  # Get grounding context from URLs if configured
541
  grounding_context = ""
542
  urls = config.get('grounding_urls', [])
 
550
  except:
551
  pass
552
 
553
+ # Build messages for API with grounding context in system prompt
554
+ system_content = config.get('system_prompt', 'You are a helpful AI assistant.')
555
  if grounding_context:
556
+ system_content = f"{system_content}\n\n{grounding_context}"
557
+
558
+ messages = [{"role": "system", "content": system_content}]
559
+
560
+ # Add conversation history
561
+ for msg in chat_history:
562
+ if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
563
+ messages.append({
564
+ "role": msg['role'],
565
+ "content": msg['content']
566
+ })
567
+
568
+ # Add current message
569
+ messages.append({
570
+ "role": "user",
571
+ "content": message
572
+ })
573
 
574
  data = {
575
  "model": config.get('model', 'openai/gpt-3.5-turbo'),
space_template.py CHANGED
@@ -402,8 +402,12 @@ Get your API key at: https://openrouter.ai/keys"""
402
  dynamic_context += f"\\n{{content}}"
403
  grounding_context += dynamic_context
404
 
405
- # Build messages
406
- messages = [{{"role": "system", "content": SYSTEM_PROMPT}}]
 
 
 
 
407
 
408
  # Add conversation history
409
  for msg in history:
@@ -413,12 +417,10 @@ Get your API key at: https://openrouter.ai/keys"""
413
  "content": msg['content']
414
  }})
415
 
416
- # Add current message with context
417
  full_message = message
418
- if grounding_context:
419
- full_message = f"{{grounding_context}}\\n\\n{{message}}"
420
  if file_context:
421
- full_message = f"{{file_context}}\\n\\n{{full_message}}"
422
 
423
  messages.append({{
424
  "role": "user",
 
402
  dynamic_context += f"\\n{{content}}"
403
  grounding_context += dynamic_context
404
 
405
+ # Build messages with grounding context in system prompt
406
+ system_content = SYSTEM_PROMPT
407
+ if grounding_context:
408
+ system_content = f"{{SYSTEM_PROMPT}}\\n\\n{{grounding_context}}"
409
+
410
+ messages = [{{"role": "system", "content": system_content}}]
411
 
412
  # Add conversation history
413
  for msg in history:
 
417
  "content": msg['content']
418
  }})
419
 
420
+ # Add current message with file context if any
421
  full_message = message
 
 
422
  if file_context:
423
+ full_message = f"{{file_context}}\\n\\n{{message}}"
424
 
425
  messages.append({{
426
  "role": "user",