openai-python
openai-python copied to clipboard
Chat completions API doesnt retuns the completion text in json format.
Describe the bug
I asked the to return the response in JSON type in the chatGPT prompt. But it returns always in a raw_text manner inside {"message":{"role":"assistant","content":
How can I ask/make the actual response inside {"message":{"role":"assistant","content": as JSON object instead of simple strings
To Reproduce
curl --location --insecure --request POST 'https://api.openai.com/v1/chat/completions' --header 'Authorization: Bearer token' --header 'Content-Type: application/json' --data-raw '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "What is the OpenAI mission?Put the result in JSON format"}]
}'
It returns output as
{"id":"chatcmpl-6wpbfhG1c0k0D4d74mhdwBWf66APk","object":"chat.completion","created":1679479419,"model":"gpt-3.5-turbo-0301","usage":{"prompt_tokens":19,"completion_tokens":131,"total_tokens":150},"choices":[{"message":{"role":"assistant","content":"\n\n{\n \"mission\": \"The mission of OpenAI is to ensure that artificial intelligence (AI) benefits humanity as a whole, and to create and advance AI in a way that is safe and beneficial for everyone.\",\n \"focus_areas\": [\n \"Developing and advancing cutting-edge AI technologies\",\n \"Conducting research in AI safety and ethics\",\n \"Promoting responsible AI development and deployment\",\n \"Advocating for policy changes that support the safe and ethical development of AI\"\n ],\n \"values\": [\n \"Collaboration\",\n \"Transparency\",\n \"Responsibility\",\n \"Impact\"\n ]\n}"},"finish_reason":"stop","index":0}]}
The content is still in string format not json
Code snippets
No response
OS
Ubuntu
Python version
Python3.9
Library version
openai==0.27.0
You should try to give him an exemple first
It looks like you're trying to get the model to output valid JSON, is that right? If so, that is not something the model is perfect at. You can try to prompt-engineer your way towards it (as you have done above), but fundamentally our models do not guarantee an output format.
@athyuttamre yes. So any solution to make the openai chatgpt API result output JSON? when I tried to give examples as @traosorus suggested, it shows an error like API input only should be a string.
Check out the few-shot prompting examples here: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb.
The content param will always be a string; if you prompt the model well and get good JSON outputs, you can try to parse the string into JSON using json.loads or similar.
I also asked for json format response and it works perfectly on the chat.open.ai page but it ignores the request for json in the api. Is this expected behaviour? Answered: When I have json example over multiple rows the API didn't like it but when I put it all on one row it started working :-)
You can ask GPT model to generate an output in JSON format using prompt engineering. For example, adding this to your system or instruction prompt
The most important rule is that the result must be given only as a JSON file without any additional text introductions or textual descriptions accompanying the output JSON file itself.
However, the result will be always a string but you can parse/cast/convert the generated string into a valid python dictionary using the python AST library.
import ast
result = ast.literal_eval(json_llm_response_string)
@MarlonCajamarca Thankyou