cohere.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import importlib
  2. import os
  3. from typing import Any, Optional
  4. from langchain_cohere import ChatCohere
  5. from embedchain.config import BaseLlmConfig
  6. from embedchain.helpers.json_serializable import register_deserializable
  7. from embedchain.llm.base import BaseLlm
  8. @register_deserializable
  9. class CohereLlm(BaseLlm):
  10. def __init__(self, config: Optional[BaseLlmConfig] = None):
  11. try:
  12. importlib.import_module("cohere")
  13. except ModuleNotFoundError:
  14. raise ModuleNotFoundError(
  15. "The required dependencies for Cohere are not installed."
  16. "Please install with `pip install langchain_cohere==1.16.0`"
  17. ) from None
  18. super().__init__(config=config)
  19. if not self.config.api_key and "COHERE_API_KEY" not in os.environ:
  20. raise ValueError("Please set the COHERE_API_KEY environment variable or pass it in the config.")
  21. def get_llm_model_answer(self, prompt) -> tuple[str, Optional[dict[str, Any]]]:
  22. if self.config.system_prompt:
  23. raise ValueError("CohereLlm does not support `system_prompt`")
  24. if self.config.token_usage:
  25. response, token_info = self._get_answer(prompt, self.config)
  26. model_name = "cohere/" + self.config.model
  27. if model_name not in self.config.model_pricing_map:
  28. raise ValueError(
  29. f"Model {model_name} not found in `model_prices_and_context_window.json`. \
  30. You can disable token usage by setting `token_usage` to False."
  31. )
  32. total_cost = (
  33. self.config.model_pricing_map[model_name]["input_cost_per_token"] * token_info["input_tokens"]
  34. ) + self.config.model_pricing_map[model_name]["output_cost_per_token"] * token_info["output_tokens"]
  35. response_token_info = {
  36. "prompt_tokens": token_info["input_tokens"],
  37. "completion_tokens": token_info["output_tokens"],
  38. "total_tokens": token_info["input_tokens"] + token_info["output_tokens"],
  39. "total_cost": round(total_cost, 10),
  40. "cost_currency": "USD",
  41. }
  42. return response, response_token_info
  43. return self._get_answer(prompt, self.config)
  44. @staticmethod
  45. def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
  46. api_key = config.api_key or os.environ["COHERE_API_KEY"]
  47. kwargs = {
  48. "model_name": config.model or "command-r",
  49. "temperature": config.temperature,
  50. "max_tokens": config.max_tokens,
  51. "together_api_key": api_key,
  52. }
  53. chat = ChatCohere(**kwargs)
  54. chat_response = chat.invoke(prompt)
  55. if config.token_usage:
  56. return chat_response.content, chat_response.response_metadata["token_count"]
  57. return chat_response.content