You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 copy import deepcopy
  18. from openai import OpenAI
  19. import openai
  20. from rag.nlp import is_english
  21. class Base(ABC):
  22. def __init__(self, key, model_name):
  23. pass
  24. def chat(self, system, history, gen_conf):
  25. raise NotImplementedError("Please implement encode method!")
  26. class GptTurbo(Base):
  27. def __init__(self, key, model_name="gpt-3.5-turbo"):
  28. self.client = OpenAI(api_key=key)
  29. self.model_name = model_name
  30. def chat(self, system, history, gen_conf):
  31. if system: history.insert(0, {"role": "system", "content": system})
  32. try:
  33. response = self.client.chat.completions.create(
  34. model=self.model_name,
  35. messages=history,
  36. **gen_conf)
  37. ans = response.output.choices[0]['message']['content'].strip()
  38. if response.output.choices[0].get("finish_reason", "") == "length":
  39. ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
  40. [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
  41. return ans, response.usage.completion_tokens
  42. except openai.APIError as e:
  43. return "**ERROR**: "+str(e), 0
  44. from dashscope import Generation
  45. class QWenChat(Base):
  46. def __init__(self, key, model_name=Generation.Models.qwen_turbo):
  47. import dashscope
  48. dashscope.api_key = key
  49. self.model_name = model_name
  50. def chat(self, system, history, gen_conf):
  51. from http import HTTPStatus
  52. if system: history.insert(0, {"role": "system", "content": system})
  53. response = Generation.call(
  54. self.model_name,
  55. messages=history,
  56. result_format='message',
  57. **gen_conf
  58. )
  59. ans = ""
  60. tk_count = 0
  61. if response.status_code == HTTPStatus.OK:
  62. ans += response.output.choices[0]['message']['content']
  63. tk_count += response.usage.output_tokens
  64. if response.output.choices[0].get("finish_reason", "") == "length":
  65. ans += "...\nFor the content length reason, it stopped, continue?" if is_english([ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
  66. return ans, tk_count
  67. return "**ERROR**: " + response.message, tk_count
  68. from zhipuai import ZhipuAI
  69. class ZhipuChat(Base):
  70. def __init__(self, key, model_name="glm-3-turbo"):
  71. self.client = ZhipuAI(api_key=key)
  72. self.model_name = model_name
  73. def chat(self, system, history, gen_conf):
  74. from http import HTTPStatus
  75. if system: history.insert(0, {"role": "system", "content": system})
  76. try:
  77. response = self.client.chat.completions.create(
  78. self.model_name,
  79. messages=history,
  80. **gen_conf
  81. )
  82. ans = response.output.choices[0]['message']['content'].strip()
  83. if response.output.choices[0].get("finish_reason", "") == "length":
  84. ans += "...\nFor the content length reason, it stopped, continue?" if is_english(
  85. [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"
  86. return ans, response.usage.completion_tokens
  87. except Exception as e:
  88. return "**ERROR**: " + str(e), 0