Skip to content

Commit 3ad969f

Browse files
committed
Convert multi-turn conversations back to messages
- remove the User: and Assistant: prefixes
1 parent ab84273 commit 3ad969f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

optillm.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,43 @@ def parse_conversation(messages):
400400
initial_query = "\n".join(conversation)
401401
return system_prompt, initial_query, optillm_approach
402402

403+
def tagged_conversation_to_messages(response_text):
404+
"""Convert a tagged conversation string or list of strings into a list of messages.
405+
406+
Args:
407+
response_text: Either a string containing "User:" and "Assistant:" tags,
408+
or a list of such strings.
409+
410+
Returns:
411+
If input is a string: A list of message dictionaries.
412+
If input is a list: A list of lists of message dictionaries.
413+
"""
414+
def process_single_response(text):
415+
messages = []
416+
# Split on "User:" or "Assistant:" while keeping the delimiter
417+
parts = re.split(r'(?=(User:|Assistant:))', text.strip())
418+
# Remove empty strings
419+
parts = [p for p in parts if p.strip()]
420+
421+
for part in parts:
422+
part = part.strip()
423+
if part.startswith('User:'):
424+
messages.append({
425+
'role': 'user',
426+
'content': part[5:].strip()
427+
})
428+
elif part.startswith('Assistant:'):
429+
messages.append({
430+
'role': 'assistant',
431+
'content': part[10:].strip()
432+
})
433+
return messages
434+
435+
if isinstance(response_text, list):
436+
return [process_single_response(text) for text in response_text]
437+
else:
438+
return process_single_response(response_text)
439+
403440
def extract_optillm_approach(content):
404441
match = re.search(r'<optillm_approach>(.*?)</optillm_approach>', content)
405442
if match:
@@ -500,6 +537,15 @@ def proxy():
500537
logger.error(f"Error processing request: {str(e)}")
501538
return jsonify({"error": str(e)}), 500
502539

540+
# Convert tagged conversation to messages format if needed
541+
if isinstance(response, list):
542+
response = [msg[-1]['content'] if isinstance(msg, list) and msg else msg
543+
for msg in tagged_conversation_to_messages(response)]
544+
else:
545+
messages = tagged_conversation_to_messages(response)
546+
if messages: # Only take the last message if we have any
547+
response = messages[-1]['content']
548+
503549
if stream:
504550
return Response(generate_streaming_response(response, model), content_type='text/event-stream')
505551
else:

optillm/plugins/readurls_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def extract_urls(text: str) -> List[str]:
2525
def fetch_webpage_content(url: str, max_length: int = 100000) -> str:
2626
try:
2727
headers = {
28-
'User-Agent': 'optillm/0.0.1 (https://github.com/codelion/optillm)'
28+
'User-Agent': 'optillm/0.0.17 (https://github.com/codelion/optillm)'
2929
}
3030

3131
response = requests.get(url, headers=headers, timeout=10)

0 commit comments

Comments
 (0)