当前位置:   article > 正文

Python 的chatGPT API小例子

Python 的chatGPT API小例子

例子1 Simple

  1. from openai import OpenAI
  2. client = OpenAI(
  3. api_key="xxxxxx",
  4. base_url="https://api.chatanywhere.tech/v1"
  5. )
  6. completion = client.chat.completions.create(
  7. model="gpt-3.5-turbo",
  8. messages=[
  9. {"role": "system", "content": "简单计算"},
  10. {"role": "user", "content": "1+12等于多少?"}
  11. ],
  12. temperature=0.3,
  13. )
  14. print(completion.choices[0].message.content)

例子2 Function_call

  1. from openai import OpenAI
  2. client = OpenAI(
  3. api_key="xxxxxxx",
  4. base_url="https://api.chatanywhere.tech/v1"
  5. )
  6. article="the wind"
  7. functions = [
  8. {
  9. "name": "write_post",
  10. "description": "Shows the title and summary of some text.",
  11. "parameters": {
  12. "type": "object",
  13. "properties": {
  14. "title": {
  15. "type": "string",
  16. "description": "Title of the text output."
  17. },
  18. "summary": {
  19. "type": "string",
  20. "description": "Summary of the text output."
  21. }
  22. }
  23. }
  24. }
  25. ]
  26. completion = client.chat.completions.create(
  27. model="gpt-3.5-turbo",
  28. messages = [
  29. {
  30. "role": "system",
  31. "content": "You are a useful assistant."
  32. },
  33. {
  34. "role": "user",
  35. "content": f"Here is an article: {article}. Please return a title and summary."
  36. },
  37. ],
  38. functions = functions,
  39. function_call = {
  40. "name": functions[0]["name"]
  41. }
  42. )
  43. print(completion.choices[0].message.function_call.arguments)

例子3 Embedding 

  1. from openai import OpenAI
  2. client = OpenAI(
  3. api_key="xxxxxx",
  4. base_url="https://api.chatanywhere.tech/v1"
  5. )
  6. completion =client.embeddings.create(
  7. input="what is the berlin wall?",
  8. model="text-embedding-ada-002"
  9. )
  10. print(completion.data[0].embedding)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/337524
推荐阅读
相关标签
  

闽ICP备14008679号