openai_bot_chat.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import base64
  15. import json
  16. import re
  17. from typing import Dict
  18. from .....utils import logging
  19. from .....utils.deps import class_requires_deps
  20. from .base import BaseChat
  21. @class_requires_deps("openai")
  22. class OpenAIBotChat(BaseChat):
  23. """OpenAI Bot Chat"""
  24. entities = [
  25. "openai",
  26. ]
  27. def __init__(self, config: Dict) -> None:
  28. """Initializes the OpenAIBotChat with given configuration.
  29. Args:
  30. config (Dict): Configuration dictionary containing model_name, api_type, base_url, api_key, end_point.
  31. Raises:
  32. ValueError: If api_type is not one of ['openai'],
  33. base_url is None for api_type is openai,
  34. api_key is None for api_type is openai.
  35. ValueError: If end_point is not one of ['completion', 'chat_completion'].
  36. """
  37. from openai import OpenAI
  38. super().__init__()
  39. model_name = config.get("model_name", None)
  40. # compatible with historical model name
  41. if model_name == "ernie-3.5":
  42. model_name = "ernie-3.5-8k"
  43. api_type = config.get("api_type", None)
  44. api_key = config.get("api_key", None)
  45. base_url = config.get("base_url", None)
  46. end_point = config.get("end_point", "chat_completion")
  47. if api_type not in ["openai"]:
  48. raise ValueError("api_type must be one of ['openai']")
  49. if api_type == "openai" and api_key is None:
  50. raise ValueError("api_key cannot be empty when api_type is openai.")
  51. if base_url is None:
  52. raise ValueError("base_url cannot be empty when api_type is openai.")
  53. if end_point not in ["completion", "chat_completion"]:
  54. raise ValueError(
  55. "end_point must be one of ['completion', 'chat_completion']"
  56. )
  57. self.client = OpenAI(base_url=base_url, api_key=api_key)
  58. self.model_name = model_name
  59. self.config = config
  60. def generate_chat_results(
  61. self,
  62. prompt: str,
  63. image: base64 = None,
  64. temperature: float = 0.001,
  65. max_retries: int = 1,
  66. ) -> Dict:
  67. """
  68. Generate chat results using the specified model and configuration.
  69. Args:
  70. prompt (str): The user's input prompt.
  71. image (base64): The user's input image for MLLM, defaults to None.
  72. temperature (float, optional): The temperature parameter for llms, defaults to 0.001.
  73. max_retries (int, optional): The maximum number of retries for llms API calls, defaults to 1.
  74. Returns:
  75. Dict: The chat completion result from the model.
  76. """
  77. llm_result = {"content": None, "reasoning_content": None}
  78. try:
  79. if image:
  80. chat_completion = self.client.chat.completions.create(
  81. model=self.model_name,
  82. messages=[
  83. {
  84. "role": "system",
  85. # XXX: give a basic prompt for common
  86. "content": "You are a helpful assistant.",
  87. },
  88. {
  89. "role": "user",
  90. "content": [
  91. {"type": "text", "text": prompt},
  92. {
  93. "type": "image_url",
  94. "image_url": {
  95. "url": f"data:image/jpeg;base64,{image}"
  96. },
  97. },
  98. ],
  99. },
  100. ],
  101. stream=False,
  102. temperature=temperature,
  103. top_p=0.001,
  104. )
  105. llm_result["content"] = chat_completion.choices[0].message.content
  106. return llm_result
  107. elif self.config.get("end_point", "chat_completion") == "chat_completion":
  108. chat_completion = self.client.chat.completions.create(
  109. model=self.model_name,
  110. messages=[
  111. {
  112. "role": "user",
  113. "content": prompt,
  114. },
  115. ],
  116. stream=False,
  117. temperature=temperature,
  118. top_p=0.001,
  119. )
  120. llm_result["content"] = chat_completion.choices[0].message.content
  121. try:
  122. llm_result["reasoning_content"] = chat_completion.choices[
  123. 0
  124. ].message.reasoning_content
  125. except:
  126. pass
  127. return llm_result
  128. else:
  129. chat_completion = self.client.completions.create(
  130. model=self.model_name,
  131. prompt=prompt,
  132. max_tokens=self.config.get("max_tokens", 1024),
  133. temperature=float(temperature),
  134. stream=False,
  135. )
  136. if isinstance(chat_completion, str):
  137. chat_completion = json.loads(chat_completion)
  138. llm_result = chat_completion["choices"][0]["text"]
  139. else:
  140. llm_result["content"] = chat_completion.choices[0].text
  141. return llm_result
  142. except Exception as e:
  143. logging.error(e)
  144. self.ERROR_MASSAGE = "大模型调用失败"
  145. return llm_result
  146. def fix_llm_result_format(self, llm_result: str) -> dict:
  147. """
  148. Fix the format of the LLM result.
  149. Args:
  150. llm_result (str): The result from the LLM (Large Language Model).
  151. Returns:
  152. dict: A fixed format dictionary from the LLM result.
  153. """
  154. if not llm_result:
  155. return {}
  156. if "json" in llm_result or "```" in llm_result:
  157. index = llm_result.find("{")
  158. if index != -1:
  159. llm_result = llm_result[index:]
  160. index = llm_result.rfind("}")
  161. if index != -1:
  162. llm_result = llm_result[: index + 1]
  163. llm_result = (
  164. llm_result.replace("```", "").replace("json", "").replace("/n", "")
  165. )
  166. llm_result = llm_result.replace("[", "").replace("]", "")
  167. try:
  168. llm_result = json.loads(llm_result)
  169. llm_result_final = {}
  170. if "问题" in llm_result.keys() and "答案" in llm_result.keys():
  171. key = llm_result["问题"]
  172. value = llm_result["答案"]
  173. if isinstance(value, list):
  174. if len(value) > 0:
  175. llm_result_final[key] = value[0].strip(f"{key}:").strip(key)
  176. else:
  177. llm_result_final[key] = value.strip(f"{key}:").strip(key)
  178. return llm_result_final
  179. for key in llm_result:
  180. value = llm_result[key]
  181. if isinstance(value, list):
  182. if len(value) > 0:
  183. llm_result_final[key] = value[0]
  184. else:
  185. llm_result_final[key] = value
  186. return llm_result_final
  187. except:
  188. results = (
  189. llm_result.replace("\n", "")
  190. .replace(" ", "")
  191. .replace("{", "")
  192. .replace("}", "")
  193. )
  194. if not results.endswith('"'):
  195. results = results + '"'
  196. pattern = r'"(.*?)": "([^"]*)"'
  197. matches = re.findall(pattern, str(results))
  198. if len(matches) > 0:
  199. llm_result = {k: v for k, v in matches}
  200. if "问题" in llm_result.keys() and "答案" in llm_result.keys():
  201. llm_result_final = {}
  202. key = llm_result["问题"]
  203. value = llm_result["答案"]
  204. if isinstance(value, list):
  205. if len(value) > 0:
  206. llm_result_final[key] = value[0].strip(f"{key}:").strip(key)
  207. else:
  208. llm_result_final[key] = value.strip(f"{key}:").strip(key)
  209. return llm_result_final
  210. return llm_result
  211. else:
  212. return {}