Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 os
  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. history.insert(0, {"role": "system", "content": system})
  30. res = self.client.chat.completions.create(
  31. model=self.model_name,
  32. messages=history,
  33. **gen_conf)
  34. return res.choices[0].message.content.strip()
  35. from dashscope import Generation
  36. class QWenChat(Base):
  37. def __init__(self, key, model_name=Generation.Models.qwen_turbo):
  38. import dashscope
  39. dashscope.api_key = key
  40. self.model_name = model_name
  41. def chat(self, system, history, gen_conf):
  42. from http import HTTPStatus
  43. history.insert(0, {"role": "system", "content": system})
  44. response = Generation.call(
  45. self.model_name,
  46. messages=history,
  47. result_format='message'
  48. )
  49. if response.status_code == HTTPStatus.OK:
  50. return response.output.choices[0]['message']['content']
  51. return response.message