Skip to content

Instantly share code, notes, and snippets.

@czue
Created September 21, 2023 11:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czue/47310785ab768ec469746c5b4cdcded7 to your computer and use it in GitHub Desktop.
Save czue/47310785ab768ec469746c5b4cdcded7 to your computer and use it in GitHub Desktop.
OpenAI JSON Cleaner
def clean_and_parse_json_with_ai(json_string: str, openai_key: str):
clean_json_fn = {
"name": "clean_json",
"description": "Cleans JSON.",
"parameters": {
"type": "object",
"properties": {
"clean_json": {
"type": "string",
"description": "The cleaned JSON.",
},
},
"required": ["clean_json"],
},
}
openai.api_key = openai_key
system_prompt = "You are a helpful assistant."
messages = [
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": f"The bad JSON: {json_string}",
},
]
openai_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
functions=[clean_json_fn],
function_call={"name": clean_json_fn["name"]},
)
# the arguments come back as text, so those get converted to JSON
arguments_raw = openai_response.choices[0].message.function_call.arguments
arguments = json.loads(arguments_raw)
# Then the cleaned json is also text, which needs to be loaded
cleaned_json_raw = arguments["clean_json"]
cleaned_json = json.loads(cleaned_json_raw)
return cleaned_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment