From 3562d145cd22c7426e3fc8592e557d9a7f3fdb13 Mon Sep 17 00:00:00 2001 From: Reece Holmdahl Date: Tue, 15 Oct 2024 00:49:34 -0500 Subject: [PATCH] Fix multiturn conversation error Groq * Groq assistant turns can only have `content: `, no arrays or multimodal * Groq cannot handle response_format argument the way that its passed, I'm working on a separate commit for that. --- src/ell/providers/groq.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/ell/providers/groq.py b/src/ell/providers/groq.py index de0ae539..f465e263 100644 --- a/src/ell/providers/groq.py +++ b/src/ell/providers/groq.py @@ -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): @@ -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': }""" + # 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 +