azure-sdk-for-net icon indicating copy to clipboard operation
azure-sdk-for-net copied to clipboard

Setting Response Format to JSON for OpenAI Assistant

Open raysuelzer opened this issue 10 months ago • 5 comments

Library name and version

Azure.AI.OpenAI.Assistants_1.0.0-beta.3

Query/Question

I cannot seem to find anywhere in the Assistants SDK to set the "response format" when using the OpenAI SDKs. This is useful to ensure that the API returns valid JSON. Sorry if this exists but I am missing it.

From OpenAI:

https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format

response_format
object

Optional
An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.

Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.

Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.

Environment

No response

raysuelzer avatar Apr 10 '24 00:04 raysuelzer