MCPcopy Create free account
hub / github.com/Paper2Poster/Paper2Poster / run

Method run

camel/models/cohere_model.py:218–269  ·  view source on GitHub ↗

r"""Runs inference of Cohere chat completion. Args: messages (List[OpenAIMessage]): Message list with the chat history in OpenAI API format. Returns: ChatCompletion.

(self, messages: List[OpenAIMessage])

Source from the content-addressed store, hash-verified

216 return self._token_counter
217
218 def run(self, messages: List[OpenAIMessage]) -> ChatCompletion:
219 r"""Runs inference of Cohere chat completion.
220
221 Args:
222 messages (List[OpenAIMessage]): Message list with the chat history
223 in OpenAI API format.
224 Returns:
225 ChatCompletion.
226 """
227 from cohere.core.api_error import ApiError
228
229 cohere_messages = self._to_cohere_chatmessage(messages)
230
231 # Removing 'strict': True from the dictionary for
232 # cohere client
233 if self.model_config_dict.get('tools') is not None:
234 for tool in self.model_config_dict.get('tools', []):
235 function_dict = tool.get('function', {})
236 if 'strict' in function_dict:
237 del function_dict['strict']
238
239 try:
240 response = self._client.chat(
241 messages=cohere_messages,
242 model=self.model_type,
243 **self.model_config_dict,
244 )
245 except ApiError as e:
246 logging.error(f"Cohere API Error: {e.status_code}")
247 logging.error(f"Error body: {e.body}")
248 raise
249 except Exception as e:
250 logging.error(f"Unexpected error when calling Cohere API: {e!s}")
251 raise
252
253 openai_response = self._to_openai_response(response)
254
255 # Add AgentOps LLM Event tracking
256 if LLMEvent:
257 llm_event = LLMEvent(
258 thread_id=openai_response.id,
259 prompt=" ".join(
260 [message.get("content") for message in messages] # type: ignore[misc]
261 ),
262 prompt_tokens=openai_response.usage.prompt_tokens, # type: ignore[union-attr]
263 completion=openai_response.choices[0].message.content,
264 completion_tokens=openai_response.usage.completion_tokens, # type: ignore[union-attr]
265 model=self.model_type,
266 )
267 record(llm_event)
268
269 return openai_response
270
271 def check_model_config(self):
272 r"""Check whether the model configuration contains any unexpected

Callers

nothing calls this directly

Calls 3

_to_openai_responseMethod · 0.95
chatMethod · 0.45

Tested by

no test coverage detected