cohere.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. from typing import Any
  2. from huggingface_hub.hf_api import InferenceProviderMapping
  3. from ._common import BaseConversationalTask
  4. _PROVIDER = "cohere"
  5. _BASE_URL = "https://api.cohere.com"
  6. class CohereConversationalTask(BaseConversationalTask):
  7. def __init__(self):
  8. super().__init__(provider=_PROVIDER, base_url=_BASE_URL)
  9. def _prepare_route(self, mapped_model: str, api_key: str) -> str:
  10. return "/compatibility/v1/chat/completions"
  11. def _prepare_payload_as_dict(
  12. self, inputs: Any, parameters: dict, provider_mapping_info: InferenceProviderMapping
  13. ) -> dict | None:
  14. payload = super()._prepare_payload_as_dict(inputs, parameters, provider_mapping_info)
  15. response_format = parameters.get("response_format")
  16. if isinstance(response_format, dict) and response_format.get("type") == "json_schema":
  17. json_schema_details = response_format.get("json_schema")
  18. if isinstance(json_schema_details, dict) and "schema" in json_schema_details:
  19. payload["response_format"] = { # type: ignore
  20. "type": "json_object",
  21. "schema": json_schema_details["schema"],
  22. }
  23. return payload