chat_openai_api.py 966 B

123456789101112131415161718192021222324252627282930
  1. from openai import OpenAI
  2. client = OpenAI(
  3. api_key="none",
  4. base_url="http://localhost:8998/v1"
  5. )
  6. stream = True
  7. conversation_history_origin = []
  8. conversation_history = conversation_history_origin.copy()
  9. while True:
  10. conversation_history = conversation_history_origin.copy()
  11. query = input('[Q]: ')
  12. conversation_history.append({"role": "user", "content": query})
  13. response = client.chat.completions.create(
  14. model="minimind",
  15. messages=conversation_history,
  16. stream=stream
  17. )
  18. if not stream:
  19. assistant_res = response.choices[0].message.content
  20. print('[A]: ', assistant_res)
  21. else:
  22. print('[A]: ', end='')
  23. assistant_res = ''
  24. for chunk in response:
  25. print(chunk.choices[0].delta.content or "", end="")
  26. assistant_res += chunk.choices[0].delta.content or ""
  27. conversation_history.append({"role": "assistant", "content": assistant_res})
  28. print('\n\n')