importosimportopenaios.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"# replace with your API keyopenai.api_key = os.getenv("OPENAI_API_KEY")
completion = 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?"},
],
)
print(completion.choices[0].message)
首先我们设置了 system 的消息,这个消息的内容是 You are a helpful assistant.,随后我们设置了 user 的消息,这个消息的内容是 Who won the world series in 2020?,这个消息相当于前一轮对话的信息,然后我们设置了 assistant 的消息,这个消息的内容是 The Los Angeles Dodgers won the World Series in 2020.,意思是 ChatGPT 在上一轮的回复。最后我们又设置了 user 的消息,这个消息的内容是 Where was it played?,表示本轮消息的问题,也就是当前想让 GPT 回复的问题。
可以看到,收到的回复内容是 The 2020 World Series was played at Globe Life Field in Arlington, Texas.,我们询问时只询问了地点,而 ChatGPT 已经通过之前的回复,知道了这次的回复是关于 2020 年世界系列赛的,并且回答了这个问题。
importosimportopenaios.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"# replace with your API keyopenai.api_key = os.getenv("OPENAI_API_KEY")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": 'I want you to act as an Chinese translator, spelling corrector and improver. I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in Chinese. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level Chinese words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. My first sentence is "Non terrae plus ultra"',
},
],
temperature=0.9, # 0.0 to 2.0 (default 1.0) top_p=1, # 0.0 to 1.0 (default 1.0) (not used if temperature is set) n=5, # number (default 1) How many chat completion choices to generate for each input message. stream=False, # boolean (default False) stop=None, # string or array (default None) max_tokens=10, # inf (default 4096-prompt_token) presence_penalty=2.0, # -2.0 to 2.0 (default 0) frequency_penalty=0, # -2.0 to 2.0 (default 0)# logit_bias=# user=)
print(completion)
for choice in completion.choices:
print(choice.message.content)
importosimportopenaios.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"openai.api_key = os.getenv("OPENAI_API_KEY")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": 'I want you to act as an Chinese translator, spelling corrector and improver. I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in Chinese. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level Chinese words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. My first sentence is "Non terrae plus ultra"',
},
],
temperature=1, # 0.0 to 2.0 (default 1.0) top_p=1, # 0.0 to 1.0 (default 1.0) (not used if temperature is set) n=1, # number (default 1) How many chat completion choices to generate for each input message. stream=True, # boolean (default False) stop=None, # string or array (default None)# max_tokens=100, # inf (default 4096-prompt_token) presence_penalty=2.0, # -2.0 to 2.0 (default 0) frequency_penalty=0, # -2.0 to 2.0 (default 0)# logit_bias=# user=)
for completion_ in completion:
# print(completion_)for choice in completion_.choices:
print(choice.delta.content if"content"in choice.delta else"")
importosimportopenaios.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"openai.api_key = os.getenv("OPENAI_API_KEY")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": 'I want you to act as an Chinese translator, spelling corrector and improver. I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in Chinese. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level Chinese words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. My first sentence is "Non terrae plus ultra"',
},
],
temperature=1, # 0.0 to 2.0 (default 1.0) top_p=1, # 0.0 to 1.0 (default 1.0) (not used if temperature is set) n=1, # number (default 1) How many chat completion choices to generate for each input message. stream=False, # boolean (default False) stop="无", # string or array (default None)# max_tokens=100, # inf (default 4096-prompt_token) presence_penalty=0, # -2.0 to 2.0 (default 0) frequency_penalty=0, # -2.0 to 2.0 (default 0)# logit_bias=# user=)
for choice in completion.choices:
print(choice.message.content)
openai.error.APIError: The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID 7d8d2f6b67d92ff7850ef3e17d742827 in your email.) {
"error": {
"message": "The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID 7d8d2f6b67d92ff7850ef3e17d742827 in your email.)",
"type": "server_error",
"param": null,
"code": null
}
}
500 {'error': {'message': 'The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID 7d8d2f6b67d92ff7850ef3e17d742827 in your email.)', 'type': 'server_error', 'param': None, 'code': None}} {'Date': 'Thu, 02 Mar 2023 12:37:21 GMT', 'Content-Type': 'application/json', 'Content-Length': '366', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Openai-Model': 'gpt-3.5-turbo-0301', 'Openai-Organization': 'user-8qnumkqgd3l02hvzq5rqz0y1', 'Openai-Processing-Ms': '750', 'Openai-Version': '2020-10-01', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains', 'X-Request-Id': '7d8d2f6b67d92ff7850ef3e17d742827'}