Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

chat_model.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #
  2. # Copyright 2019 The RAG Flow 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 chat(self, system, history, gen_conf):
  21. raise NotImplementedError("Please implement encode method!")
  22. class GptTurbo(Base):
  23. def __init__(self):
  24. self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
  25. def chat(self, system, history, gen_conf):
  26. history.insert(0, {"role": "system", "content": system})
  27. res = self.client.chat.completions.create(
  28. model="gpt-3.5-turbo",
  29. messages=history,
  30. **gen_conf)
  31. return res.choices[0].message.content.strip()
  32. class QWenChat(Base):
  33. def chat(self, system, history, gen_conf):
  34. from http import HTTPStatus
  35. from dashscope import Generation
  36. # export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
  37. history.insert(0, {"role": "system", "content": system})
  38. response = Generation.call(
  39. Generation.Models.qwen_turbo,
  40. messages=history,
  41. result_format='message'
  42. )
  43. if response.status_code == HTTPStatus.OK:
  44. return response.output.choices[0]['message']['content']
  45. return response.message