SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

Trying out OpenAI API's JSON mode

Overview

Last time, I used GPT-4V to create a program from an image. This time, I will continue using the new version of the OpenAI API. I will try using JSON mode, which ensures that the output is always in JSON format.

Previous challenges

Taking advantage of GPT's versatility, it is being used in various places not just as a simple chat, but as a program module. Agent mechanisms also output the next action as text in a prescribed format, which the program side then parses to call APIs, generate text, or terminate. When parsing programs via text, the format must be fixed, but GPT did not always output in the same format. Therefore, the recent trend has been to use Function Calling, but with this update, a mode that outputs results in JSON has been added. I definitely have to use this.

Let's try it out quickly

For now, I tried it out quickly while reading the manual. In a translation sample, I output the original text with the key 'original' and the translation result with the key 'translated'. As for how to use it, you just need to specify 'json_object' in 'response_format'. Furthermore, I had to specify in the system message to output JSON (I got stuck here, I should read the manual properly).

import openai
import json
api_key = "<API-KEY>"

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo-1106",
  messages=[
      {
        "role": "system",
        "content": "あなたは優秀なアシスタントです。JSONで結果を出力します。"
      },
      {
        "role": "user",
        "content": "以下を英語に翻訳してください。元のテキストをoriginal、変換後のテキストをtranslatedというキーとしてください。\n==\nこんにちは"
      }  
  ],
  response_format={ "type": "json_object" }
)

print(json.loads(response.choices[0]["message"]["content"]))

Result

It turned out like this. It's done properly, wonderful.

{'original': 'こんにちは', 'translated': 'Hello'}


いいなと思ったら応援しよう!