azure_openai.py 1.3 KB

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