Update 20230302.2 ️ Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide].md

格式化代码块
This commit is contained in:
六开箱 2023-03-03 19:42:51 +08:00 committed by GitHub
parent 6b7a3c6259
commit 59eab04be9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
[#]: subject: "Create Your First Program Using OpenAI ChatGPT API [Beginners Guide]"
[#]: via: "https://www.debugpoint.com/openai-chatgpt-api-python/"
[#]: via: "https://www.debugpoint.com/openai-chatgpt-api-python"
[#]: author: "Arindam https://www.debugpoint.com/author/admin1/"
[#]: collector: "lkxed"
[#]: translator: " "
@ -137,11 +137,14 @@ The basic function of OpenAI chat API is below. The `openai.ChatCompletion.creat
```
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"}, {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
] )
model = "gpt-3.5-turbo",
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
```
**Explanation**
@ -203,25 +206,25 @@ Alright! I am ready to be your friendly chatbot
You can now type your messages.
what do you think about kindness?
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "As an AI language model, I don't have personal opinions, but I can tell you that kindness is a very positive and essential trait to have. Kindness is about being considerate and compassionate towards others, which creates positive emotions and reduces negativity. People who are kind towards others are more likely to inspire kindness and compassion in return. It is an important quality that helps to build positive relationships, promote cooperation, and create a more peaceful world.",
"role": "assistant"
}
}
],
"created": <removed>,
"id": "chatcmpl-<removed>",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 91,
"prompt_tokens": 22,
"total_tokens": 113
}
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "As an AI language model, I don't have personal opinions, but I can tell you that kindness is a very positive and essential trait to have. Kindness is about being considerate and compassionate towards others, which creates positive emotions and reduces negativity. People who are kind towards others are more likely to inspire kindness and compassion in return. It is an important quality that helps to build positive relationships, promote cooperation, and create a more peaceful world.",
"role": "assistant"
}
}
],
"created": <removed>,
"id": "chatcmpl-<removed>",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 91,
"prompt_tokens": 22,
"total_tokens": 113
}
}
```
@ -252,7 +255,7 @@ Heres the complete code which is used in the above demo.
```
import openai
openai.api_key="<your key>"
openai.api_key = "<your key>"
messages = []
system_message = input("What type of chatbot you want me to be?")
messages.append({"role":"system","content":system_message})
@ -261,7 +264,7 @@ print("Alright! I am ready to be your friendly chatbot" + "\n" + "You can now ty
message = input("")
messages.append({"role":"user","content": message})
response=openai.ChatCompletion.create(
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
@ -284,7 +287,7 @@ _[Reference][10]_
--------------------------------------------------------------------------------
via: https://www.debugpoint.com/openai-chatgpt-api-python/
via: https://www.debugpoint.com/openai-chatgpt-api-python
作者:[Arindam][a]
选题:[lkxed][b]
@ -304,4 +307,4 @@ via: https://www.debugpoint.com/openai-chatgpt-api-python/
[7]: https://www.debugpoint.com/5-best-python-ide-code-editor/
[8]: https://www.debugpoint.com/wp-content/uploads/2023/03/unformatted-JSON-output.jpg
[9]: https://www.debugpoint.com/wp-content/uploads/2023/03/OpenAI-ChatGPT-API-output-from-a-Python-prgram.jpg
[10]: https://platform.openai.com/docs/guides/chat/introduction
[10]: https://platform.openai.com/docs/guides/chat/introduction