如何将openai ChatCompletion响应中的每个元素以JSON格式打印在单独的行上?

我正在使用Python向OpenAI发起一个简单的请求,询问棒球比赛的地点。

completion = openai.chat.completions.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)

输出显示如下:

ChatCompletion(id='chatcmpl-9UgP85B0gYBjEAiYMcF3Ryt9Y3fdZ', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The 2020 World Series was played at Globe Life Field in Arlington, Texas.', role='assistant', function_call=None, tool_calls=None))], created=1717099870, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=17, prompt_tokens=53, total_tokens=70))

但我希望它显示成这样:

{  "choices": [    {      "finish_reason": "stop",      "index": 0,      "message": {        "content": "The 2020 World Series was played in Texas at Globe Life Field in Arlington.",        "role": "assistant"      },      "logprobs": null    }  ],  "created": 1677664795,  "id": "chatcmpl-7QyqpwdfhqwajicIEznoc6Q47XAyW",  "model": "gpt-3.5-turbo-0613",  "object": "chat.completion",  "usage": {    "completion_tokens": 17,    "prompt_tokens": 57,    "total_tokens": 74  }}

顺便提一下,我使用的是Python 3.12和Windows Terminal。


回答:

使用.model_dump_json()

completion = client.chat.completions.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?"}  ])data = completion.model_dump_json()print(data)

输出:

{"id":"chatcmpl-9VGebGyeosYhcWW19F6f5B6lMFhpl","choices":[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":"The World Series in 2020 was played at Globe Life Field in Arlington, Texas.","role":"assistant","function_call":null,"tool_calls":null}}],"created":1717239213,"model":"gpt-3.5-turbo-0125","object":"chat.completion","system_fingerprint":null,"usage":{"completion_tokens":18,"prompt_tokens":52,"total_tokens":70}}

如果需要美化输出,可以使用ident参数:

data = completion.model_dump_json(indent=3)print(data)

输出:

{   "id": "chatcmpl-9VGphVuZLKh2QuqT9vSAUdWCxoifg",   "choices": [      {         "finish_reason": "stop",         "index": 0,         "logprobs": null,         "message": {            "content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas, home of the Texas Rangers.",            "role": "assistant",            "function_call": null,            "tool_calls": null         }      }   ],   "created": 1717239901,   "model": "gpt-3.5-turbo-0125",   "object": "chat.completion",   "system_fingerprint": null,   "usage": {      "completion_tokens": 23,      "prompt_tokens": 52,      "total_tokens": 75   }}

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注