openai.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import json
  2. import os
  3. from typing import Any, Callable, Dict, Optional, Type, Union
  4. from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
  5. from langchain.schema import BaseMessage, HumanMessage, SystemMessage
  6. from langchain_core.tools import BaseTool
  7. from langchain_openai import ChatOpenAI
  8. from pydantic import BaseModel
  9. from embedchain.config import BaseLlmConfig
  10. from embedchain.helpers.json_serializable import register_deserializable
  11. from embedchain.llm.base import BaseLlm
  12. @register_deserializable
  13. class OpenAILlm(BaseLlm):
  14. def __init__(
  15. self,
  16. config: Optional[BaseLlmConfig] = None,
  17. tools: Optional[Union[Dict[str, Any], Type[BaseModel], Callable[..., Any], BaseTool]] = None,
  18. ):
  19. self.tools = tools
  20. super().__init__(config=config)
  21. def get_llm_model_answer(self, prompt) -> tuple[str, Optional[dict[str, Any]]]:
  22. if self.config.token_usage:
  23. response, token_info = self._get_answer(prompt, self.config)
  24. model_name = "openai/" + self.config.model
  25. if model_name not in self.config.model_pricing_map:
  26. raise ValueError(
  27. f"Model {model_name} not found in `model_prices_and_context_window.json`. \
  28. You can disable token usage by setting `token_usage` to False."
  29. )
  30. total_cost = (
  31. self.config.model_pricing_map[model_name]["input_cost_per_token"] * token_info["prompt_tokens"]
  32. ) + self.config.model_pricing_map[model_name]["output_cost_per_token"] * token_info["completion_tokens"]
  33. response_token_info = {
  34. "prompt_tokens": token_info["prompt_tokens"],
  35. "completion_tokens": token_info["completion_tokens"],
  36. "total_tokens": token_info["prompt_tokens"] + token_info["completion_tokens"],
  37. "total_cost": round(total_cost, 10),
  38. "cost_currency": "USD",
  39. }
  40. return response, response_token_info
  41. return self._get_answer(prompt, self.config)
  42. def _get_answer(self, prompt: str, config: BaseLlmConfig) -> str:
  43. messages = []
  44. if config.system_prompt:
  45. messages.append(SystemMessage(content=config.system_prompt))
  46. messages.append(HumanMessage(content=prompt))
  47. kwargs = {
  48. "model": config.model or "gpt-3.5-turbo",
  49. "temperature": config.temperature,
  50. "max_tokens": config.max_tokens,
  51. "model_kwargs": config.model_kwargs or {},
  52. }
  53. api_key = config.api_key or os.environ["OPENAI_API_KEY"]
  54. base_url = config.base_url or os.environ.get("OPENAI_API_BASE", None)
  55. if config.top_p:
  56. kwargs["model_kwargs"]["top_p"] = config.top_p
  57. if config.default_headers:
  58. kwargs["default_headers"] = config.default_headers
  59. if config.stream:
  60. callbacks = config.callbacks if config.callbacks else [StreamingStdOutCallbackHandler()]
  61. chat = ChatOpenAI(
  62. **kwargs,
  63. streaming=config.stream,
  64. callbacks=callbacks,
  65. api_key=api_key,
  66. base_url=base_url,
  67. http_client=config.http_client,
  68. http_async_client=config.http_async_client,
  69. )
  70. else:
  71. chat = ChatOpenAI(
  72. **kwargs,
  73. api_key=api_key,
  74. base_url=base_url,
  75. http_client=config.http_client,
  76. http_async_client=config.http_async_client,
  77. )
  78. if self.tools:
  79. return self._query_function_call(chat, self.tools, messages)
  80. chat_response = chat.invoke(messages)
  81. if self.config.token_usage:
  82. return chat_response.content, chat_response.response_metadata["token_usage"]
  83. return chat_response.content
  84. def _query_function_call(
  85. self,
  86. chat: ChatOpenAI,
  87. tools: Optional[Union[Dict[str, Any], Type[BaseModel], Callable[..., Any], BaseTool]],
  88. messages: list[BaseMessage],
  89. ) -> str:
  90. from langchain.output_parsers.openai_tools import JsonOutputToolsParser
  91. from langchain_core.utils.function_calling import convert_to_openai_tool
  92. openai_tools = [convert_to_openai_tool(tools)]
  93. chat = chat.bind(tools=openai_tools).pipe(JsonOutputToolsParser())
  94. try:
  95. return json.dumps(chat.invoke(messages)[0])
  96. except IndexError:
  97. return "Input could not be mapped to the function!"