Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiturn conversation error Groq #312

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ell/providers/groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class GroqProvider(OpenAIProvider):
def translate_to_provider(self, *args, **kwargs):
params = super().translate_to_provider(*args, **kwargs)
params.pop('stream_options', None)
assert 'response_format' not in params, 'Groq does not support response_format.'
params['messages'] = messages_to_groq_message_format(params['messages'])
return params

def translate_from_provider(self, *args, **kwargs):
Expand All @@ -24,3 +26,22 @@ def translate_from_provider(self, *args, **kwargs):
except ImportError:
pass

def messages_to_groq_message_format(messages):
"""Assistant messages to Groq must take the format: {'role': 'assistant', 'content': <string>}"""
# XXX: Issue #289: groq.BadRequestError: Error code: 400 - {'error': {'message': "'messages.1' : for 'role:assistant' the following must be satisfied[('messages.1.content' : value must be a string)]", 'type': 'invalid_request_error'}}
new_messages = []
for message in messages:
if message['role'] == 'assistant':
# Assistant messages must be strings
# If content is a list, only one string element is allowed
if isinstance(message['content'], str):
new_messages.append({'role': 'assistant', 'content': message['content']})
elif isinstance(message['content'], list) and len(message['content']) == 1 and message['content'][0]['type'] == 'text':
new_messages.append({'role': 'assistant', 'content': message['content'][0]['text']})
else:
raise ValueError('Groq assistant messages must contain exactly one string content.')
else:
new_messages.append(message)

return new_messages

Loading