Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

fix(models): map Cohere finish_reason to the OpenAI vocabulary (#4202)

Ehsane7225567c6a3
diff --git a/camel/models/cohere_model.py b/camel/models/cohere_model.py
index 9bd5fd6ff..bf1b29660 100644
--- a/camel/models/cohere_model.py
+++ b/camel/models/cohere_model.py
@@ -55,6 +55,16 @@ except (ImportError, AttributeError):
LLMEvent = None
+# Map successful Cohere V2 finish reasons to the OpenAI vocabulary. ERROR and
+# TIMEOUT are raised because OpenAI has no equivalent finish reason.
+_FINISH_REASON_MAP = {
+ "COMPLETE": "stop",
+ "STOP_SEQUENCE": "stop",
+ "MAX_TOKENS": "length",
+ "TOOL_CALL": "tool_calls",
+}
+
+
class CohereModel(BaseModelBackend):
r"""Cohere API in a unified BaseModelBackend interface.
@@ -119,7 +129,28 @@ class CohereModel(BaseModelBackend):
**kwargs,
)
+ @staticmethod
+ def _map_finish_reason(cohere_reason: str) -> str:
+ r"""Map a successful Cohere reason to its OpenAI equivalent."""
+ if cohere_reason in {"ERROR", "TIMEOUT"}:
+ raise RuntimeError(
+ "Cohere generation failed with finish reason "
+ f"{cohere_reason!r}"
+ )
+ try:
+ return _FINISH_REASON_MAP[cohere_reason]
+ except KeyError as exc:
+ raise ValueError(
+ f"Unknown Cohere finish reason: {cohere_reason!r}"
+ ) from exc
+
def _to_openai_response(self, response: 'ChatResponse') -> ChatCompletion:
+ finish_reason = (
+ self._map_finish_reason(response.finish_reason)
+ if response.finish_reason is not None
+ else None
+ )
+
if response.usage and response.usage.tokens:
input_tokens = response.usage.tokens.input_tokens or 0
output_tokens = response.usage.tokens.output_tokens or 0
@@ -162,9 +193,7 @@ class CohereModel(BaseModelBackend):
"content": content,
"tool_calls": openai_tool_calls,
},
- finish_reason=response.finish_reason
- if response.finish_reason
- else None,
+ finish_reason=finish_reason,
)
choices = [choice]
diff --git a/test/models/test_cohere_model.py b/test/models/test_cohere_model.py
index 840c73984..c168624ad 100644
--- a/test/models/test_cohere_model.py
+++ b/test/models/test_cohere_model.py
@@ -30,15 +30,21 @@ def _mock_cohere_tool_call(call_id, name, arguments):
)
-def _mock_cohere_response(*, tool_calls=None, text=None):
+_UNSET = object()
+
+
+def _mock_cohere_response(*, tool_calls=None, text=None, finish_reason=_UNSET):
message = SimpleNamespace(
tool_plan="calling tools" if tool_calls else None,
content=None if tool_calls else [SimpleNamespace(text=text)],
tool_calls=tool_calls,
)
+ if finish_reason is _UNSET:
+ # Cohere's V2ChatResponse.finish_reason is upper case.
+ finish_reason = "TOOL_CALL" if tool_calls else "COMPLETE"
return SimpleNamespace(
id="gen-1",
- finish_reason="tool_call" if tool_calls else "complete",
+ finish_reason=finish_reason,
usage=SimpleNamespace(
tokens=SimpleNamespace(input_tokens=11, output_tokens=7)
),
@@ -100,6 +106,59 @@ def test_to_openai_response_text_only():
assert result.choices[0].message.tool_calls is None
+ "cohere_reason, expected",
+ [
+ ("COMPLETE", "stop"),
+ ("STOP_SEQUENCE", "stop"),
+ ("MAX_TOKENS", "length"),
+ ("TOOL_CALL", "tool_calls"),
+ ],
+)
+def test_to_openai_response_maps_finish_reason(cohere_reason, expected):
+ r"""Successful Cohere reasons map to OpenAI finish reasons."""
+ model = CohereModel(ModelType.COHERE_COMMAND_R, api_key="dummy")
+ response = _mock_cohere_response(text="hi", finish_reason=cohere_reason)
+
+ result = model._to_openai_response(response)
+
+ assert result.choices[0].finish_reason == expected
+
+
+ "cohere_reason, content", [("ERROR", None), ("TIMEOUT", [])]
+)
+def test_to_openai_response_raises_for_failed_generation(
+ cohere_reason, content
+):
+ r"""Provider failures raise before optional content is read."""
+ model = CohereModel(ModelType.COHERE_COMMAND_R, api_key="dummy")
+ response = _mock_cohere_response(
+ text="partial", finish_reason=cohere_reason
+ )
+ response.message.content = content
+
+ with pytest.raises(RuntimeError, match=cohere_reason):
+ model._to_openai_response(response)
+
+
+def test_to_openai_response_finish_reason_none_stays_none():
+ r"""A response with no finish_reason still maps to None, not to a
+ default, so an unfinished generation is not reported as completed."""
+ model = CohereModel(ModelType.COHERE_COMMAND_R, api_key="dummy")
+ response = _mock_cohere_response(text="hi", finish_reason=None)
+
+ result = model._to_openai_response(response)
+
+ assert result.choices[0].finish_reason is None
+
+
+def test_map_finish_reason_unknown_value_raises():
+ r"""Unknown provider reasons are not reported as successful stops."""
+ with pytest.raises(ValueError, match="SOME_NEW_REASON"):
+ CohereModel._map_finish_reason("SOME_NEW_REASON")
+
+
@pytest.mark.model_backend
@pytest.mark.parametrize(
"model_type",