Get GPT to Output JSON Consistently

  • Aktualisiert
  • Veröffentlicht in Uncategorized
  • 3 Minuten zum Lesen

Automated Information Extraction from Text using GPT and Python


2023-11-20, Johannes Köppern, Update
With openai package version >= 1.2.3 this is the way to go:


# 20231120_openai_json_output.py
# 2023-11-20
# Get an answer from OpenAI's GPT-3 API in JSON format
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
import os

load_dotenv(find_dotenv())

api_key = os.getenv("OPENAI_API_KEY")

client = OpenAI(api_key=api_key)

prompt_text = """
Please provide a translation of "bitte verlassen Sie mein Grundstück" into English in the following format:
{
"polite": "Translation in a polite tone",
"demanding": "Translation in a demanding tone"
}
Ensure the response is in valid JSON format.
"""

response = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt_text,
}
],
model="gpt-3.5-turbo",
)

print(response.choices[0].message.content)
# {
# "polite": "Please leave my property",
# "demanding": "Leave my property immediately"
# }

2023-09-16, Johannes Köppern
To get OpenAI's GPT to output consistently JSON I use its so called Function Calling capabilities, as I show in this post for the example of extracting text. Big shout out to Hubel Labs also presents this in one of her YouTube videos.

Let's say we want to extract the writer's name and age from the text
> I'm Johann and I'm 12 years old.

Both shall be returned within the JSON dictionary


{
	"name":string,
	"age":int
}

This is how we can achieve it:


# function_calling.py
# 2023-09-16, JK
import openai
from dotenv import find_dotenv, load_dotenv


load_dotenv(find_dotenv(), override=True)


function = {
    "name": "name_and_age",
    "description": "A function that takes in an writer's name and his age",
    "parameters": {
        "type": "object",
        "properties": {
            "name":{
                "type": "string",
                "description":"The writer's name of the provided text"
            },
            "age":{
                "type": "integer",
                "description":"Age of the writer of the provided text"
            },            
        }
    },
    "required": ["name", "age",],
}


messages = [
    {"role":"system", "content":"You're a system for text extraction. Please extract information from a provided text and then extract them from your answer to pass them to a given function."},
    {"role":"user", "content":"Please identify writer's name and age."},
    {"role":"user", "content":"This is the text: I'm Johann and I'm 12 years old"},
]


response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=messages,
    functions=[function],
    function_call={"name": "name_and_age"}, # this forces calling function
)


content = response["choices"][0]["message"]["function_call"]["arguments"]
print(content)


print("------------")
print(response)

We'll get this output:


{
  "name": "Johann",
  "age": 12
}
------------
{
  "id": "chatcmpl-7zPF8cK1bwpqztfu28T7GA4qMwwmn",
  "object": "chat.completion",
  "created": 1694869398,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "name_and_age",
          "arguments": "{\n  \"name\": \"Johann\",\n  \"age\": 12\n}"
        }
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
...
    "completion_tokens": 18,
    "total_tokens": 159
  }
}

After you called a function with parameters from GPT you might get a repsonse (in variable response_from_function) you want to feed back to GPT. To do this append this response to the list of messages:


messages.append(
        {
            "role": "function",
            "name": "get_current_weather",
            "content": name_and_age,
        }
)

Happy coding and thanks to Dal-le 2 for the images!
<

y

Schreibe einen Kommentar