Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

chat_model.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. from openai import OpenAI
  18. import openai
  19. class Base(ABC):
  20. def __init__(self, key, model_name):
  21. pass
  22. def chat(self, system, history, gen_conf):
  23. raise NotImplementedError("Please implement encode method!")
  24. class GptTurbo(Base):
  25. def __init__(self, key, model_name="gpt-3.5-turbo"):
  26. self.client = OpenAI(api_key=key)
  27. self.model_name = model_name
  28. def chat(self, system, history, gen_conf):
  29. if system: history.insert(0, {"role": "system", "content": system})
  30. try:
  31. res = self.client.chat.completions.create(
  32. model=self.model_name,
  33. messages=history,
  34. **gen_conf)
  35. return res.choices[0].message.content.strip(), res.usage.completion_tokens
  36. except openai.APIError as e:
  37. return "ERROR: "+str(e), 0
  38. from dashscope import Generation
  39. class QWenChat(Base):
  40. def __init__(self, key, model_name=Generation.Models.qwen_turbo):
  41. import dashscope
  42. dashscope.api_key = key
  43. self.model_name = model_name
  44. def chat(self, system, history, gen_conf):
  45. from http import HTTPStatus
  46. if system: history.insert(0, {"role": "system", "content": system})
  47. response = Generation.call(
  48. self.model_name,
  49. messages=history,
  50. result_format='message',
  51. **gen_conf
  52. )
  53. if response.status_code == HTTPStatus.OK:
  54. return response.output.choices[0]['message']['content'], response.usage.output_tokens
  55. return "ERROR: " + response.message, 0
  56. from zhipuai import ZhipuAI
  57. class ZhipuChat(Base):
  58. def __init__(self, key, model_name="glm-3-turbo"):
  59. self.client = ZhipuAI(api_key=key)
  60. self.model_name = model_name
  61. def chat(self, system, history, gen_conf):
  62. from http import HTTPStatus
  63. if system: history.insert(0, {"role": "system", "content": system})
  64. response = self.client.chat.completions.create(
  65. self.model_name,
  66. messages=history,
  67. **gen_conf
  68. )
  69. if response.status_code == HTTPStatus.OK:
  70. return response.output.choices[0]['message']['content'], response.usage.completion_tokens
  71. return "ERROR: " + response.message, 0