-
-
Save Scorpil/7559f072b53d2b62b50c54d530e4050f to your computer and use it in GitHub Desktop.
Ensuring JSON Response Format in OpenAI Assistant API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import json | |
from openai import OpenAI | |
def translate(input): | |
""" | |
translate function takes a sentence in any language and translates | |
it into English, French, Spanish and German. | |
""" | |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | |
assistant = client.beta.assistants.create( | |
name="Structured Response Assistant", | |
model="gpt-4-1106-preview", # newest GPT-4 model at the time of writing | |
instructions=( | |
"Detect the input language and translate user input into " | |
"other languages: English, French, Spanish and German. " | |
# you need to explicitly ask the model to use the response tool | |
"Imporant: always use the response tool to respond to the user. " | |
"Never add any other text to the response." | |
), | |
tools=[{ | |
"type": "function", | |
"function": { | |
"name": "response", | |
"description": "Translate user input into other languages", | |
"parameters": { | |
"type": "object", | |
"required": [ | |
"input", | |
"original_language", | |
"english", | |
"french", | |
"spanish", | |
"german", | |
], | |
"properties": { | |
"input": { | |
"type": "string", | |
"description": "User input" | |
}, | |
"original_language": { # detects original language | |
"type": "string", | |
"description": "Language of the user input" | |
}, | |
# requests translation | |
# note that model uses both field names and descriptions | |
# to correctly fill in the arguments | |
"english": { | |
"type": "string", | |
"description": "Translation to English" | |
}, | |
"french": { | |
"type": "string", | |
"description": "Translation to French" | |
}, | |
"spanish": { | |
"type": "string", | |
"description": "Translation to Spanish" | |
}, | |
"german": { | |
"type": "string", | |
"description": "Translation to German" | |
}, | |
} | |
}, | |
} | |
}] | |
) | |
thread = client.beta.threads.create() | |
client.beta.threads.messages.create( | |
thread_id=thread.id, | |
role="user", | |
content=input, | |
) | |
run = client.beta.threads.runs.create( | |
thread_id=thread.id, | |
assistant_id=assistant.id, | |
) | |
# 60 retries max, which shouldn't take much more than 1 minute | |
for i in range(60): | |
print(f"Waiting for response... ({i} seconds)") | |
# get the latest run state | |
result = client.beta.threads.runs.retrieve( | |
thread_id=thread.id, | |
run_id=run.id | |
) | |
if result.status == "requires_action": # run has executed | |
# parse structured response from the tool call | |
structured_response = json.loads( | |
# fetch json from function arguments | |
result.required_action.submit_tool_outputs.\ | |
tool_calls[0].function.arguments | |
) | |
return structured_response | |
# wait 1 second before retry | |
time.sleep(1) | |
raise Exception("Timeout") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment