deep_seek_api_official.py 1011 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """
  2. deep_seek 官方 API
  3. """
  4. import json
  5. from openai import OpenAI
  6. from config import deep_seek_official_model
  7. from config import deep_seek_official_api_key
  8. def fetch_deepseek_response(model, prompt, output_type='text'):
  9. """
  10. deep_seek方法
  11. """
  12. client = OpenAI(
  13. api_key=deep_seek_official_api_key,
  14. base_url="https://api.deepseek.com"
  15. )
  16. # get response format
  17. if output_type == "json":
  18. response_format = {"type": "json_object"}
  19. else:
  20. response_format = {"type": "text"}
  21. chat_completion = client.chat.completions.create(
  22. messages=[
  23. {
  24. "role": "user",
  25. "content": prompt,
  26. }
  27. ],
  28. model=deep_seek_official_model.get(model, "deepseek-chat"),
  29. response_format=response_format,
  30. )
  31. response = chat_completion.choices[0].message.content
  32. if output_type == "json":
  33. response_json = json.loads(response)
  34. return response_json
  35. return response