together.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import importlib
  2. import os
  3. from typing import Optional
  4. from langchain_community.llms import Together
  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 TogetherLlm(BaseLlm):
  10. def __init__(self, config: Optional[BaseLlmConfig] = None):
  11. if "TOGETHER_API_KEY" not in os.environ:
  12. raise ValueError("Please set the TOGETHER_API_KEY environment variable.")
  13. try:
  14. importlib.import_module("together")
  15. except ModuleNotFoundError:
  16. raise ModuleNotFoundError(
  17. "The required dependencies for Together are not installed."
  18. 'Please install with `pip install --upgrade "embedchain[together]"`'
  19. ) from None
  20. super().__init__(config=config)
  21. def get_llm_model_answer(self, prompt):
  22. if self.config.system_prompt:
  23. raise ValueError("TogetherLlm does not support `system_prompt`")
  24. return TogetherLlm._get_answer(prompt=prompt, config=self.config)
  25. @staticmethod
  26. def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
  27. llm = Together(
  28. together_api_key=os.environ["TOGETHER_API_KEY"],
  29. model=config.model,
  30. max_tokens=config.max_tokens,
  31. temperature=config.temperature,
  32. top_p=config.top_p,
  33. )
  34. return llm(prompt)