azure_openai.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import logging
  2. from typing import Optional
  3. from embedchain.config import BaseLlmConfig
  4. from embedchain.helpers.json_serializable import register_deserializable
  5. from embedchain.llm.base import BaseLlm
  6. logger = logging.getLogger(__name__)
  7. @register_deserializable
  8. class AzureOpenAILlm(BaseLlm):
  9. def __init__(self, config: Optional[BaseLlmConfig] = None):
  10. super().__init__(config=config)
  11. def get_llm_model_answer(self, prompt):
  12. return AzureOpenAILlm._get_answer(prompt=prompt, config=self.config)
  13. @staticmethod
  14. def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
  15. from langchain_community.chat_models import AzureChatOpenAI
  16. if not config.deployment_name:
  17. raise ValueError("Deployment name must be provided for Azure OpenAI")
  18. chat = AzureChatOpenAI(
  19. deployment_name=config.deployment_name,
  20. openai_api_version="2023-05-15",
  21. model_name=config.model or "gpt-3.5-turbo",
  22. temperature=config.temperature,
  23. max_tokens=config.max_tokens,
  24. streaming=config.stream,
  25. )
  26. if config.top_p and config.top_p != 1:
  27. logger.warning("Config option `top_p` is not supported by this model.")
  28. messages = BaseLlm._get_messages(prompt, system_prompt=config.system_prompt)
  29. return chat(messages).content