llm_openai.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import os
  2. import openai
  3. from plugins.common import settings
  4. def chat_init(history):
  5. return history
  6. def chat_one(prompt, history_formatted, max_length, top_p, temperature, data):
  7. history_data = [ {"role": "system", "content": "You are a helpful assistant."}]
  8. if history_formatted is not None:
  9. for i, old_chat in enumerate(history_formatted):
  10. if old_chat['role'] == "user":
  11. history_data.append(
  12. {"role": "user", "content": old_chat['content']},)
  13. elif old_chat['role'] == "AI" or old_chat['role'] == 'assistant':
  14. history_data.append(
  15. {"role": "assistant", "content": old_chat['content']},)
  16. history_data.append({"role": "user", "content": prompt},)
  17. response = openai.ChatCompletion.create(
  18. model="gpt-3.5-turbo",
  19. messages=history_data,
  20. stream=True
  21. )
  22. resTemp=""
  23. for chunk in response:
  24. #print(chunk)
  25. if chunk['choices'][0]["finish_reason"]!="stop":
  26. if hasattr(chunk['choices'][0]['delta'], 'content'):
  27. resTemp+=chunk['choices'][0]['delta']['content']
  28. yield resTemp
  29. chatCompletion = None
  30. def load_model():
  31. openai.api_key = os.getenv("OPENAI_API_KEY")
  32. openai.api_base = settings.llm.api_host
  33. class Lock:
  34. def __init__(self):
  35. pass
  36. def get_waiting_threads(self):
  37. return 0
  38. def __enter__(self):
  39. pass
  40. def __exit__(self, exc_type, exc_val, exc_tb):
  41. pass
粤ICP备19079148号