From 59eab04be9076e8e77a98039c46a5a86836c8997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=BC=80=E7=AE=B1?= Date: Fri, 3 Mar 2023 19:42:51 +0800 Subject: [PATCH] =?UTF-8?q?Update=2020230302.2=20=E2=AD=90=EF=B8=8F?= =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Create=20Your=20First=20Program=20Using=20?= =?UTF-8?q?OpenAI=20ChatGPT=20API=20[Beginner=E2=80=99s=20Guide].md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 格式化代码块 --- ...ram Using OpenAI ChatGPT API [Beginner’s Guide].md | 61 ++++++++++--------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/sources/tech/20230302.2 ⭐️⭐️ Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide].md b/sources/tech/20230302.2 ⭐️⭐️ Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide].md index a47a9c27fd..74d4b1a729 100644 --- a/sources/tech/20230302.2 ⭐️⭐️ Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide].md +++ b/sources/tech/20230302.2 ⭐️⭐️ Create Your First Program Using OpenAI ChatGPT API [Beginner’s Guide].md @@ -1,5 +1,5 @@ [#]: subject: "Create Your First Program Using OpenAI ChatGPT API [Beginner’s 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": , -"id": "chatcmpl-", -"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": , + "id": "chatcmpl-", + "model": "gpt-3.5-turbo-0301", + "object": "chat.completion", + "usage": { + "completion_tokens": 91, + "prompt_tokens": 22, + "total_tokens": 113 + } } ``` @@ -252,7 +255,7 @@ Here’s the complete code which is used in the above demo. ``` import openai -openai.api_key="" +openai.api_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 \ No newline at end of file +[10]: https://platform.openai.com/docs/guides/chat/introduction