Skip to content
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
24 changes: 23 additions & 1 deletion lmms_eval/models/simple/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@
load_dotenv(verbose=True)


def _normalize_openai_message_content(content) -> str:
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
text_parts = []
for part in content:
if isinstance(part, str):
text_parts.append(part)
continue
if not isinstance(part, dict):
continue
if part.get("type") == "text" and isinstance(part.get("text"), str):
text_parts.append(part["text"])
continue
if isinstance(part.get("content"), str):
text_parts.append(part["content"])
return "".join(text_parts)
return str(content)


@register_model("openai")
class OpenAICompatible(lmms):
def __init__(
Expand Down Expand Up @@ -311,7 +333,7 @@ def process_single_request(local_index: int, payload: dict):
for attempt in range(self.max_retries):
try:
response = self.client.chat.completions.create(**payload)
response_text = response.choices[0].message.content
response_text = _normalize_openai_message_content(response.choices[0].message.content)
token_counts = None
if hasattr(response, "usage") and response.usage:
log_usage(
Expand Down