llm_backend.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import logging
  2. from typing import List
  3. from openai import OpenAI, Stream
  4. from openai.types.chat import ChatCompletionChunk, ChatCompletion
  5. class LLMBackend:
  6. """
  7. openai chat 接口封装
  8. """
  9. def __init__(self, base_url: str, api_key) -> None:
  10. self.base_url = base_url + "/" if base_url else None
  11. self.api_key = api_key
  12. self.client = OpenAI(base_url=self.base_url, api_key=self.api_key)
  13. def run(
  14. self,
  15. messages: List,
  16. model: str,
  17. tools: List = None,
  18. tool_choice="auto",
  19. stream=False,
  20. stream_options=None,
  21. extra_body=None,
  22. temperature=None,
  23. top_p=None,
  24. response_format=None,
  25. parallel_tool_calls=False,
  26. audio=None,
  27. modalities=None,
  28. ) -> ChatCompletion | Stream[ChatCompletionChunk]:
  29. if any(model.startswith(prefix) for prefix in ["o1", "o3", "gpt-5"]):
  30. temperature = None
  31. top_p = None
  32. chat_params = {
  33. "messages": messages,
  34. "model": model,
  35. "stream": stream,
  36. "presence_penalty": None,
  37. # "parallel_tool_calls": parallel_tool_calls,
  38. }
  39. if extra_body:
  40. model_params = extra_body.get("model_params")
  41. if model_params:
  42. if "n" in model_params:
  43. raise ValueError("n is not allowed in model_params")
  44. chat_params.update(model_params)
  45. stream_options_params = extra_body.get("stream_options")
  46. if stream_options_params:
  47. chat_params["stream_options"] = {
  48. "include_usage": bool(stream_options_params["include_usage"])
  49. }
  50. print("stream_optionsstream_optionsstream_optionsstream_optionsstream_options")
  51. print(stream_options)
  52. if stream_options:
  53. print(isinstance(stream_options, dict))
  54. if isinstance(stream_options, dict):
  55. if "include_usage" in stream_options:
  56. chat_params["stream_options"] = {
  57. "include_usage": bool(stream_options["include_usage"])
  58. }
  59. if audio:
  60. chat_params["audio"] = audio
  61. if modalities:
  62. chat_params["modalities"] = modalities
  63. if temperature:
  64. chat_params["temperature"] = temperature
  65. if top_p:
  66. chat_params["top_p"] = top_p
  67. if tools:
  68. chat_params["tools"] = tools
  69. chat_params["parallel_tool_calls"] = parallel_tool_calls
  70. chat_params["tool_choice"] = tool_choice if tool_choice else "auto"
  71. if (
  72. isinstance(response_format, dict)
  73. and response_format.get("type") == "json_object"
  74. ):
  75. chat_params["response_format"] = {"type": "json_object"}
  76. for message in chat_params["messages"]:
  77. if "content" not in message:
  78. message["content"] = ""
  79. chat_params["timeout"] = 300
  80. logging.info("chat_params: %s", chat_params)
  81. response = self.client.chat.completions.create(**chat_params)
  82. logging.info("chat_response: %s", response)
  83. return response