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

问题描述 投票:0回答:1

我正在使用 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
  }
}

FWIW,我正在使用 Python 3.12 和 Windows 终端。

python json openai-api pretty-print
1个回答
0
投票

使用

.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}}

如果需要漂亮的打印效果,请将其转换为字典,然后使用

json.dumps()
转换回带有缩进的字符串:

import json
data = json.loads(completion.model_dump_json())
json_str = json.dumps(data, indent=3)
print(json_str)

输出:

{
   "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
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.