This is a very useful trick, but since the June update you can also use the “function calling” feature to extract structured data. I converted the example in the blog post (I used GPT-4 to convert the TypeScript type into a JSON schema):
curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "A user has performed the following searches and visited the following sites recently: weather in New York, NYC weather, NYC chance of rain today, is New York a rainy city (They may or may not have changed goals half way through this session.)"}
],
"functions": [
{
"name": "report_browsing_goal_analysis",
"description": "Reports the analysis the users recent browsing activity",
"parameters": {
"type": "object",
"properties": {
"goal": {
"type": "string",
"description": "The users consistent recent browsing goal"
},
"confidence": {
"type": "string",
"description": "How confident the analysis engine is in this goal",
"enum": ["very low", "low", "med", "high", "very high"]
},
"switched": {
"type": "boolean",
"description": "Whether or not the user seems to have switched goals recently"
},
"advice": {
"type": "array",
"description": "An array of up to 3 creative, specific, non-obvious ways the system recommends for the user to meet their current goal. (Do not return generic suggestions like subscribe to a newsletter or do a Google search, instead recommend specific actions that the user should consider.)",
"items": {
"type": "object",
"properties": {
"text": {
"type": "string"
},
"probabilityOfBeingUseful": {
"type": "string",
"enum": ["very low", "low", "med", "high", "very high"]
},
"creativity": {
"type": "string",
"enum": ["very low", "low", "med", "high", "very high"]
},
"funLevel": {
"type": "string",
"enum": ["very low", "low", "med", "high", "very high"]
}
},
"required": ["text", "probabilityOfBeingUseful", "creativity", "funLevel"]
},
"maxItems": 3
}
},
"required": ["goal", "confidence", "switched", "advice"]
}
}
]
}'
This is what the API returned:
{"id":"chatcmpl-7Xd4BPsW9QBuk46BJRZRCNoFpJtV9","object":"chat.completion","created":1688249351,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"message":{"role":"assistant","content":null,"function_call":{"name":"report_browsing_goal_analysis","arguments":"{\n \"goal\": \"weather in New York\",\n \"confidence\": \"high\",\n \"switched\": false,\n \"advice\": [\n {\n \"text\": \"Check the hourly weather forecast for New York to plan your activities.\",\n \"probabilityOfBeingUseful\": \"high\",\n \"creativity\": \"high\",\n \"funLevel\": \"low\"\n },\n {\n \"text\": \"Subscribe to a weather alert service to stay updated on the weather in New York.\",\n \"probabilityOfBeingUseful\": \"medium\",\n \"creativity\": \"medium\",\n \"funLevel\": \"low\"\n },\n {\n \"text\": \"Join a local weather enthusiasts group to discuss weather patterns in New York.\",\n \"probabilityOfBeingUseful\": \"low\",\n \"creativity\": \"high\",\n \"funLevel\": \"high\"\n }\n ]\n}"}},"finish_reason":"function_call"}],"usage":{"prompt_tokens":303,"completion_tokens":194,"total_tokens":497}}
Here is just the JSON result extracted:
{"goal":"weather in New York","confidence":"high","switched":false,"advice":[{"text":"Check the hourly weather forecast for New York to plan your activities.","probabilityOfBeingUseful":"high","creativity":"high","funLevel":"low"},{"text":"Subscribe to a weather alert service to stay updated on the weather in New York.","probabilityOfBeingUseful":"medium","creativity":"medium","funLevel":"low"},{"text":"Join a local weather enthusiasts group to discuss weather patterns in New York.","probabilityOfBeingUseful":"low","creativity":"high","funLevel":"high"}]}
This is a very useful trick, but since the June update you can also use the “function calling” feature to extract structured data. I converted the example in the blog post (I used GPT-4 to convert the TypeScript type into a JSON schema):
This is what the API returned:
{ "id": "chatcmpl-7Xd4BPsW9QBuk46BJRZRCNoFpJtV9", "object": "chat.completion", "created": 1688249351, "model": "gpt-3.5-turbo-0613", "choices": [ { "index": 0, "message": { "role": "assistant", "content": null, "function_call": { "name": "report_browsing_goal_analysis", "arguments": "{\n \"goal\": \"weather in New York\",\n \"confidence\": \"high\",\n \"switched\": false,\n \"advice\": [\n {\n \"text\": \"Check the hourly weather forecast for New York to plan your activities.\",\n \"probabilityOfBeingUseful\": \"high\",\n \"creativity\": \"high\",\n \"funLevel\": \"low\"\n },\n {\n \"text\": \"Subscribe to a weather alert service to stay updated on the weather in New York.\",\n \"probabilityOfBeingUseful\": \"medium\",\n \"creativity\": \"medium\",\n \"funLevel\": \"low\"\n },\n {\n \"text\": \"Join a local weather enthusiasts group to discuss weather patterns in New York.\",\n \"probabilityOfBeingUseful\": \"low\",\n \"creativity\": \"high\",\n \"funLevel\": \"high\"\n }\n ]\n}" } }, "finish_reason": "function_call" } ], "usage": { "prompt_tokens": 303, "completion_tokens": 194, "total_tokens": 497 } }
Here is just the JSON result extracted:
{ "goal": "weather in New York", "confidence": "high", "switched": false, "advice": [ { "text": "Check the hourly weather forecast for New York to plan your activities.", "probabilityOfBeingUseful": "high", "creativity": "high", "funLevel": "low" }, { "text": "Subscribe to a weather alert service to stay updated on the weather in New York.", "probabilityOfBeingUseful": "medium", "creativity": "medium", "funLevel": "low" }, { "text": "Join a local weather enthusiasts group to discuss weather patterns in New York.", "probabilityOfBeingUseful": "low", "creativity": "high", "funLevel": "high" } ] }
Pretty cool, isn’t it?
(BTW this was GPT-3.5)
Thanks!