મુખ્ય વિષય-સામગ્રી પર જાવો
OpenAI

6 ઑગસ્ટ, 2024

કંપની

APIમાં સ્ટ્રક્ચર્ડ આઉટપુટ્સની રજૂઆત

અમે APIમાં સ્ટ્રક્ચર્ડ આઉટપુટ્સ રજૂ કરી રહ્યા છીએ—મોડલ આઉટપુટ્સ હવે ડેવલપર દ્વારા પૂરી પાડેલી JSON સ્કીમાઓનું વિશ્વસનીય રીતે પાલન કરે છે.

છબીમાં વાદળી, લીલા અને હળવા પીળા રંગના વિવિધ શેડ્સમાં નાના ચોરસોનું અમૂર્ત પેટર્ન દેખાય છે. ચોરસો ગ્રીડ જેવા ગોઠવાયેલા છે, જે નરમ, પેસ્ટલ રંગછટા સાથે મોઝેક અસર બનાવે છે.

ગયા વર્ષે DevDayમાં, અમે JSON mode રજૂ કર્યો હતો—અમારા મોડલ્સ સાથે વિશ્વસનીય એપ્લિકેશન્સ બનાવવા ઇચ્છતા ડેવલપર્સ માટે ઉપયોગી બિલ્ડિંગ બ્લોક. JSON mode માન્ય JSON આઉટપુટ્સ જનરેટ કરવા માટે મોડલની વિશ્વસનીયતા સુધારે છે, પરંતુ તે મોડલનો જવાબ કોઈ ચોક્કસ સ્કીમા સાથે અનુરૂપ રહેશે તેની ખાતરી આપતું નથી. આજે અમે APIમાં સ્ટ્રક્ચર્ડ આઉટપુટ્સ રજૂ કરી રહ્યા છીએ, જે ડેવલપર્સ દ્વારા પૂરી પાડેલી JSON સ્કીમાઓ સાથે મોડલ-જનરેટેડ આઉટપુટ્સ ચોક્કસ રીતે મેળ ખાય તેની ખાતરી કરવા માટે રચાયેલ નવી સુવિધા છે.

અસંરચિત ઇનપુટ્સમાંથી સ્ટ્રક્ચર્ડ ડેટા જનરેટ કરવું આજના એપ્લિકેશન્સમાં AI માટેના મુખ્ય ઉપયોગ કિસ્સાઓમાંનું એક છે. ડેવલપર્સ OpenAI API નો ઉપયોગ શક્તિશાળી assistants બનાવવા માટે કરે છે, જેઓ function calling(નવી વિન્ડોમાં ખૂલે છે) દ્વારા ડેટા મેળવવાની અને પ્રશ્નોના જવાબ આપવાની ક્ષમતા ધરાવે છે, ડેટા એન્ટ્રી માટે સ્ટ્રક્ચર્ડ ડેટા કાઢે છે, અને LLMsને ક્રિયા કરવાની મંજૂરી આપતા બહુ-પગલાવાળા agentic workflows બનાવે છે. ડેવલપર્સ આ ક્ષેત્રમાં LLMsની મર્યાદાઓને ઉકેલવા માટે લાંબા સમયથી open source tooling, prompting, અને વારંવાર વિનંતિઓ ફરીથી કરીને કામ કરતા આવ્યા છે, જેથી મોડલ આઉટપુટ્સ તેમની સિસ્ટમ્સ સાથે આંતરક્રિયા કરવા માટે જરૂરી ફોર્મેટ્સ સાથે મેળ ખાતા રહે. સ્ટ્રક્ચર્ડ આઉટપુટ્સ આ સમસ્યાનો ઉકેલ OpenAI મોડલ્સને ડેવલપર દ્વારા પૂરી પાડેલી સ્કીમાઓ સાથે મેળ કરવા માટે નિયંત્રિત કરીને અને અમારા મોડલ્સને જટિલ સ્કીમાઓ વધુ સારી રીતે સમજવા માટે તાલીમ આપીને લાવે છે.

જટિલ JSON સ્કીમા અનુસરણ માટેના અમારા evals પર, સ્ટ્રક્ચર્ડ આઉટપુટ્સ સાથેનું અમારું નવું મોડલ gpt-4o-2024-08-06 સંપૂર્ણ 100% સ્કોર કરે છે. તુલનાત્મક રીતે, gpt-4-0613 40% કરતાં ઓછું સ્કોર કરે છે.

સ્ટ્રક્ચર્ડ આઉટપુટ્સ સાથે, gpt-4o-2024-08-06 અમારા evalsમાં 100% વિશ્વસનીયતા હાંસલ કરે છે અને આઉટપુટ સ્કીમાઓ સાથે સંપૂર્ણ મેળ ખાય છે.

સ્ટ્રક્ચર્ડ આઉટપુટ્સનો ઉપયોગ કેવી રીતે કરવો

અમે APIમાં સ્ટ્રક્ચર્ડ આઉટપુટ્સને બે સ્વરૂપોમાં રજૂ કરી રહ્યા છીએ: 

1. ફંક્શન કોલિંગ: tools દ્વારા સ્ટ્રક્ચર્ડ આઉટપુટ્સ તમારા ફંક્શન definitionમાં strict: true સેટ કરીને ઉપલબ્ધ છે. આ સુવિધા toolsને સપોર્ટ કરતા બધા મોડલ્સ સાથે કામ કરે છે, જેમાં gpt-4-0613 અને gpt-3.5-turbo-0613 તથા તે પછીના બધા મોડલ્સનો સમાવેશ થાય છે. જ્યારે સ્ટ્રક્ચર્ડ આઉટપુટ્સ સક્ષમ હોય, ત્યારે મોડલ આઉટપુટ્સ પૂરી પાડેલી tool definition સાથે મેળ ખાતા હશે.

JSON

1
POST /v1/chat/completions
2
{
3
"model": "gpt-4o-2024-08-06",
4
"messages": [
5
{
6
"role": "system",
7
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function."
8
},
9
{
10
"role": "user",
11
"content": "look up all my orders in may of last year that were fulfilled but not delivered on time"
12
}
13
],
14
"tools": [
15
{
16
"type": "function",
17
"function": {
18
"name": "query",
19
"description": "Execute a query.",
20
"strict": true,
21
"parameters": {
22
"type": "object",
23
"properties": {
24
"table_name": {
25
"type": "string",
26
"enum": ["orders"]
27
},
28
"columns": {
29
"type": "array",
30
"items": {
31
"type": "string",
32
"enum": [
33
"id",
34
"status",
35
"expected_delivery_date",
36
"delivered_at",
37
"shipped_at",
38
"ordered_at",
39
"canceled_at"
40
]
41
}
42
},
43
"conditions": {
44
"type": "array",
45
"items": {
46
"type": "object",
47
"properties": {
48
"column": {
49
"type": "string"
50
},
51
"operator": {
52
"type": "string",
53
"enum": ["=", ">", "<", ">=", "<=", "!="]
54
},
55
"value": {
56
"anyOf": [
57
{
58
"type": "string"
59
},
60
{
61
"type": "number"
62
},
63
{
64
"type": "object",
65
"properties": {
66
"column_name": {
67
"type": "string"
68
}
69
},
70
"required": ["column_name"],
71
"additionalProperties": false
72
}
73
]
74
}
75
},
76
"required": ["column", "operator", "value"],
77
"additionalProperties": false
78
}
79
},
80
"order_by": {
81
"type": "string",
82
"enum": ["asc", "desc"]
83
}
84
},
85
"required": ["table_name", "columns", "conditions", "order_by"],
86
"additionalProperties": false
87
}
88
}
89
}
90
]
91
}

2. response_format પેરામિટર માટે એક નવો વિકલ્પ: ડેવલપર્સ હવે json_schema દ્વારા JSON સ્કીમા આપી શકે છે, જે response_format પેરામિટર માટેનો નવો વિકલ્પ છે. જ્યારે મોડલ ટૂલ કોલ કરતું નથી પરંતુ વપરાશકર્તાને સ્ટ્રક્ચર્ડ રીતે જવાબ આપે છે ત્યારે આ ઉપયોગી છે. આ સુવિધા અમારા નવા GPT‑4o મોડલ્સ સાથે કામ કરે છે: આજે રિલીઝ થયેલ gpt-4o-2024-08-06, અને gpt-4o-mini-2024-07-18. જ્યારે response_format ને strict: true સાથે આપવામાં આવે છે, ત્યારે મોડલ આઉટપુટ્સ પૂરા પાડેલી સ્કીમા સાથે મેળ ખાતા હશે.

વિનંતી

1
POST /v1/chat/completions
2
{
3
"model": "gpt-4o-2024-08-06",
4
"messages": [
5
{
6
"role": "system",
7
"content": "You are a helpful math tutor."
8
},
9
{
10
"role": "user",
11
"content": "solve 8x + 31 = 2"
12
}
13
],
14
"response_format": {
15
"type": "json_schema",
16
"json_schema": {
17
"name": "math_response",
18
"strict": true,
19
"schema": {
20
"type": "object",
21
"properties": {
22
"steps": {
23
"type": "array",
24
"items": {
25
"type": "object",
26
"properties": {
27
"explanation": {
28
"type": "string"
29
},
30
"output": {
31
"type": "string"
32
}
33
},
34
"required": ["explanation", "output"],
35
"additionalProperties": false
36
}
37
},
38
"final_answer": {
39
"type": "string"
40
}
41
},
42
"required": ["steps", "final_answer"],
43
"additionalProperties": false
44
}
45
}
46
}
47
}

સુરક્ષિત સ્ટ્રક્ચર્ડ આઉટપુટ્સ

સુરક્ષા OpenAI માટે સર્વોચ્ચ પ્રાથમિકતા છે—નવી સ્ટ્રક્ચર્ડ આઉટપુટ્સ સુવિધા અમારી હાલની સુરક્ષા નીતિઓનું પાલન કરશે અને મોડલને અસુરક્ષિત વિનંતીને નકારવાની મંજૂરી આપશે. વિકાસને વધુ સરળ બનાવવા માટે, API રિસ્પોન્સમાં એક નવી refusal સ્ટ્રિંગ કિંમત છે જે ડેવલપર્સને પ્રોગ્રામેટિક રીતે શોધવાની મંજૂરી આપે છે કે મોડલે સ્કીમા સાથે મેળ ખાતા આઉટપુટના બદલે નકાર જનરેટ કર્યો છે કે નહીં. જ્યારે રિસ્પોન્સમાં નકાર સામેલ ન હોય અને મોડલનો રિસ્પોન્સ સમય પહેલાં અવરોધિત ન થયો હોય (જેમ કે finish_reason દ્વારા સૂચવવામાં આવે છે), ત્યારે મોડલનો રિસ્પોન્સ વિશ્વસનીય રીતે પૂરી પાડેલી સ્કીમા સાથે મેળ ખાતું માન્ય JSON ઉત્પન્ન કરશે.

JSON

1
{
2
"id": "chatcmpl-9nYAG9LPNonX8DAyrkwYfemr3C8HC",
3
"object": "chat.completion",
4
"created": 1721596428,
5
"model": "gpt-4o-2024-08-06",
6
"choices": [
7
{
8
"index": 0,
9
"message": {
10
"role": "assistant",
11
"refusal": "I'm sorry, I cannot assist with that request."
12
},
13
"logprobs": null,
14
"finish_reason": "stop"
15
}
16
],
17
"usage": {
18
"prompt_tokens": 81,
19
"completion_tokens": 11,
20
"total_tokens": 92
21
},
22
"system_fingerprint": "fp_3407719c7f"
23
}

નેટિવ SDK સપોર્ટ

અમારા Python અને Node SDKs ને સ્ટ્રક્ચર્ડ આઉટપુટ્સ માટે નેટિવ સપોર્ટ સાથે અપડેટ કરવામાં આવ્યા છે. tools માટે અથવા response format તરીકે સ્કીમા આપવી એટલી જ સરળ છે જેટલી Pydantic અથવા Zod object આપવી, અને અમારા SDKs ડેટા પ્રકારને સપોર્ટેડ JSON સ્કીમામાં રૂપાંતરિત કરવાનું, JSON રિસ્પોન્સને typed data structure માં આપમેળે ડીસિરિયલાઇઝ કરવાનું, અને જો નકારો આવે તો તેને પાર્સ કરવાનું સંભાળશે.

નીચેના ઉદાહરણો ફંક્શન કોલિંગ સાથે સ્ટ્રક્ચર્ડ આઉટપુટ્સ માટેનો નેટિવ સપોર્ટ દર્શાવે છે.

Python

1
from enum import Enum
2
from typing import Union
3

4
from pydantic import BaseModel
5

6
import openai
7
from openai import OpenAI
8

9

10
class Table(str, Enum):
11
orders = "orders"
12
customers = "customers"
13
products = "products"
14

15

16
class Column(str, Enum):
17
id = "id"
18
status = "status"
19
expected_delivery_date = "expected_delivery_date"
20
delivered_at = "delivered_at"
21
shipped_at = "shipped_at"
22
ordered_at = "ordered_at"
23
canceled_at = "canceled_at"
24

25

26
class Operator(str, Enum):
27
eq = "="
28
gt = ">"
29
lt = "<"
30
le = "<="
31
ge = ">="
32
ne = "!="
33

34

35
class OrderBy(str, Enum):
36
asc = "asc"
37
desc = "desc"
38

39

40
class DynamicValue(BaseModel):
41
column_name: str
42

43

44
class Condition(BaseModel):
45
column: str
46
operator: Operator
47
value: Union[str, int, DynamicValue]
48

49

50
class Query(BaseModel):
51
table_name: Table
52
columns: list[Column]
53
conditions: list[Condition]
54
order_by: OrderBy
55

56

57
client = OpenAI()
58

59
completion = client.beta.chat.completions.parse(
60
model="gpt-4o-2024-08-06",
61
messages=[
62
{
63
"role": "system",
64
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",
65
},
66
{
67
"role": "user",
68
"content": "look up all my orders in may of last year that were fulfilled but not delivered on time",
69
},
70
],
71
tools=[
72
openai.pydantic_function_tool(Query),
73
],
74
)
75

76
print(completion.choices[0].message.tool_calls[0].function.parsed_arguments)

response_format માટે નેટિવ સ્ટ્રક્ચર્ડ આઉટપુટ્સ સપોર્ટ પણ ઉપલબ્ધ છે.

Python

1
from pydantic import BaseModel
2

3
from openai import OpenAI
4

5

6
class Step(BaseModel):
7
explanation: str
8
output: str
9

10

11
class MathResponse(BaseModel):
12
steps: list[Step]
13
final_answer: str
14

15

16
client = OpenAI()
17

18
completion = client.beta.chat.completions.parse(
19
model="gpt-4o-2024-08-06",
20
messages=[
21
{"role": "system", "content": "You are a helpful math tutor."},
22
{"role": "user", "content": "solve 8x + 31 = 2"},
23
],
24
response_format=MathResponse,
25
)
26

27
message = completion.choices[0].message
28
if message.parsed:
29
print(message.parsed.steps)
30
print(message.parsed.final_answer)
31
else:
32
print(message.refusal)

વધારાના ઉપયોગ કિસ્સાઓ

ડેવલપર્સ વિવિધ ઉપયોગ કિસ્સાઓ માટે સ્ટ્રક્ચર્ડ ડેટા જનરેટ કરવા OpenAIના મોડલ્સનો વારંવાર ઉપયોગ કરે છે. કેટલાક વધારાના ઉદાહરણોમાં સામેલ છે:

વપરાશકર્તાના ઇરાદા આધારે ડાયનામિક રીતે યુઝર ઇન્ટરફેસ જનરેટ કરવું

ઉદાહરણ તરીકે, ડેવલપર્સ કોડ- અથવા UI-જનરેટિંગ એપ્લિકેશન્સ બનાવવા માટે સ્ટ્રક્ચર્ડ આઉટપુટ્સનો ઉપયોગ કરી શકે છે. નીચેના બધા ઉદાહરણો એક જ response_format નો ઉપયોગ કરે છે, અને વપરાશકર્તા ઇનપુટના આધારે વિવિધ UI જનરેટ કરવા માટે ઉપયોગમાં લઈ શકાય છે.

સિસ્ટમ
તમે એક યુઝર ઇન્ટરફેસને સહાયક છો. તમારું કામ વપરાશકર્તાને તેમની વેબસાઇટ અને એપના વિચારોને દૃશ્યરૂપે કલ્પના કરવામાં મદદ કરવાનો છે.
પ્રતિભાવનું ફોર્મેટ
આસિસ્ટન્ટ
1
{
2
"type": "div",
3
"label": "",
4
"children": [
5
{
6
"type": "header",
7
"label": "",
8
"children": [
9
{
10
"type": "div",
11
"label": "Green Thumb Gardening",
12
"children": [],
13
"attributes": [{ "name": "className", "value": "site-title" }]
14
},
15
{
16
"type": "div",
17
"label": "Bringing Life to Your Garden",
18
"children": [],
19
"attributes": [{ "name": "className", "value": "site-tagline" }]
20
}
21
],
22
"attributes": [{ "name": "className", "value": "header" }]
23
},
24
{
25
"type": "section",
26
"label": "",
27
"children": [
28
{
29
"type": "div",
30
"label": "",
31
"children": [
32
{
33
"type": "div",
34
"label": "About Us",
35
"children": [
36
{
37
"type": "div",
38
"label": "At Green Thumb Gardening, we specialize in transforming your outdoor spaces into beautiful, thriving gardens. Our team has decades of experience in horticulture and landscape design.",
39
"children": [],
40
"attributes": [
41
{ "name": "className", "value": "about-description" }
42
]
43
}
44
],
45
"attributes": [{ "name": "className", "value": "about-section" }]
46
}
47
],
48
"attributes": [{ "name": "className", "value": "content" }]
49
}
50
],
51
"attributes": [{ "name": "className", "value": "about-container" }]
52
},
53
{
54
"type": "section",
55
"label": "",
56
"children": [
57
{
58
"type": "div",
59
"label": "",
60
"children": [
61
{
62
"type": "div",
63
"label": "Our Services",
64
"children": [
65
{
66
"type": "div",
67
"label": "Garden Design",
68
"children": [],
69
"attributes": [
70
{ "name": "className", "value": "service-item" }
71
]
72
},
73
{
74
"type": "div",
75
"label": "Plant Care & Maintenance",
76
"children": [],
77
"attributes": [
78
{ "name": "className", "value": "service-item" }
79
]
80
},
81
{
82
"type": "div",
83
"label": "Seasonal Cleanup",
84
"children": [],
85
"attributes": [
86
{ "name": "className", "value": "service-item" }
87
]
88
},
89
{
90
"type": "div",
91
"label": "Custom Landscaping",
92
"children": [],
93
"attributes": [
94
{ "name": "className", "value": "service-item" }
95
]
96
}
97
],
98
"attributes": [{ "name": "className", "value": "services-list" }]
99
}
100
],
101
"attributes": [{ "name": "className", "value": "content" }]
102
}
103
],
104
"attributes": [{ "name": "className", "value": "services-container" }]
105
}
106
],
107
"attributes": [{ "name": "className", "value": "landing-page" }]
108
}
109

Welcome to Green Thumb Gardening
Bringing Life to Your Garden
At Green Thumb Gardening, we specialize in transforming your outdoor spaces into beautiful, thriving gardens. Our team has decades of experience in horticulture and landscape design.
Our services
Garden Design
Plant Care & Maintenance
Seasonal Cleanup
Custom Landscaping

અંતિમ જવાબને સહાયક રિઝનિંગ અથવા વધારાની ટિપ્પણીથી અલગ કરવું

જવાબની અંતિમ ગુણવત્તા સુધારવા માટે મોડલને chain of thought માટે અલગ ફીલ્ડ આપવું ઉપયોગી બની શકે છે.

JSON

1
{
2
"model": "gpt-4o-2024-08-06",
3
"messages": [
4
{
5
"role": "system",
6
"content": "You are a helpful assistant"
7
},
8
{
9
"role": "user",
10
"content": "9.11 and 9.9 -- which is bigger?"
11
}
12
],
13
"response_format": {
14
"type": "json_schema",
15
"json_schema": {
16
"name": "reasoning_schema",
17
"strict": true,
18
"schema": {
19
"type": "object",
20
"properties": {
21
"reasoning_steps": {
22
"type": "array",
23
"items": {
24
"type": "string"
25
},
26
"description": "The reasoning steps leading to the final conclusion."
27
},
28
"answer": {
29
"type": "string",
30
"description": "The final answer, taking into account the reasoning steps."
31
}
32
},
33
"required": ["reasoning_steps", "answer"],
34
"additionalProperties": false
35
}
36
}
37
}
38
}

અસંરચિત ડેટામાંથી સ્ટ્રક્ચર્ડ ડેટા કાઢવું

ઉદાહરણ તરીકે, મોડલને મીટિંગ નોંધોમાંથી to-dos, due dates અને assignments જેવી બાબતો કાઢવા માટે સૂચના આપવી.

JSON

1
POST /v1/chat/completions
2
{
3
"model": "gpt-4o-2024-08-06",
4
"messages": [
5
{
6
"role": "system",
7
"content": "Extract action items, due dates, and owners from meeting notes."
8
},
9
{
10
"role": "user",
11
"content": "...meeting notes go here..."
12
}
13
],
14
"response_format": {
15
"type": "json_schema",
16
"json_schema": {
17
"name": "action_items",
18
"strict": true,
19
"schema": {
20
"type": "object",
21
"properties": {
22
"action_items": {
23
"type": "array",
24
"items": {
25
"type": "object",
26
"properties": {
27
"description": {
28
"type": "string",
29
"description": "Description of the action item."
30
},
31
"due_date": {
32
"type": ["string", "null"],
33
"description": "Due date for the action item, can be null if not specified."
34
},
35
"owner": {
36
"type": ["string", "null"],
37
"description": "Owner responsible for the action item, can be null if not specified."
38
}
39
},
40
"required": ["description", "due_date", "owner"],
41
"additionalProperties": false
42
},
43
"description": "List of action items from the meeting."
44
}
45
},
46
"required": ["action_items"],
47
"additionalProperties": false
48
}
49
}
50
}
51
}

આંતરિક રીતે

JSON સ્કીમા સાથે મેળ ખાતા મોડલ આઉટપુટ્સની વિશ્વસનીયતા સુધારવા માટે અમે બે ભાગનો અભિગમ અપનાવ્યો. પ્રથમ, અમે અમારા નવા મોડલ gpt-4o-2024-08-06 ને જટિલ સ્કીમાઓ સમજવા અને તેની સાથે મેળ ખાતા આઉટપુટ્સ શ્રેષ્ઠ રીતે કેવી રીતે ઉત્પન્ન કરવાના તે માટે તાલીમ આપી. પરંતુ મોડલનું વર્તન મૂળભૂત રીતે non-deterministic છે—આ મોડલના પ્રદર્શન સુધારાઓ છતાં (અમારા benchmark પર 93%), તે હજુ પણ ડેવલપર્સને મજબૂત એપ્લિકેશન્સ બનાવવા માટે જરૂરી વિશ્વસનીયતા સુધી પહોંચ્યું નહોતું. તેથી અમે 100% વિશ્વસનીયતા હાંસલ કરવા માટે મોડલના આઉટપુટ્સને નિયંત્રિત કરવા deterministic, engineering-based અભિગમ પણ અપનાવ્યો.

નિયંત્રિત ડિકોડિંગ

અમારો અભિગમ constrained sampling અથવા constrained decoding તરીકે જાણીતી તકનીક પર આધારિત છે. ડિફોલ્ટ રીતે, જ્યારે મોડલ્સને આઉટપુટ્સ ઉત્પન્ન કરવા માટે સેમ્પલ કરવામાં આવે છે, ત્યારે તેઓ સંપૂર્ણપણે અનિયંત્રિત હોય છે અને આગળના આઉટપુટ તરીકે શબ્દભંડારમાંથી કોઈપણ ટોકન પસંદ કરી શકે છે. આ લવચીકતા જ મોડલ્સને ભૂલો કરવાની મંજૂરી આપે છે; ઉદાહરણ તરીકે, તેઓ સામાન્ય રીતે કોઈપણ સમયે curly brace ટોકન સેમ્પલ કરવા માટે મુક્ત હોય છે, ભલે તે માન્ય JSON ઉત્પન્ન ન કરતું હોય. માન્ય આઉટપુટ્સને બળપૂર્વક સુનિશ્ચિત કરવા માટે, અમે અમારા મોડલ્સને બધા ઉપલબ્ધ ટોકન્સની બદલે માત્ર પૂરી પાડેલી સ્કીમા અનુસાર માન્ય એવા ટોકન્સ સુધી મર્યાદિત કરીએ છીએ.

વ્યવહારમાં આ મર્યાદા લાગુ કરવી પડકારજનક હોઈ શકે છે, કારણ કે મોડલના સમગ્ર આઉટપુટ દરમિયાન માન્ય ટોકન્સ બદલાતા રહે છે. માનો કે અમારી પાસે નીચેની સ્કીમા છે:

JSON

1
{
2
"type": "object",
3
"properties": {
4
"value": { "type": "number" }
5
},
6
"required": ["value"],
7
"additionalProperties": false
8
}

આઉટપુટની શરૂઆતમાં માન્ય એવા ટોકન્સમાં {, {“, {
વગેરે જેવી વસ્તુઓનો સમાવેશ થાય છે. પરંતુ એકવાર મોડલે પહેલેથી જ {“val સેમ્પલ કરી લીધું હોય, પછી { હવે માન્ય ટોકન રહેતું નથી. તેથી અમારે ડાયનામિક નિયંત્રિત ડિકોડિંગ અમલમાં મૂકવાની અને જવાબની શરૂઆતમાં પહેલેથી નક્કી કરવાની બદલે, દરેક ટોકન જનરેટ થયા પછી કયા ટોકન્સ માન્ય છે તે નક્કી કરવાની જરૂર પડે છે.

આ કરવા માટે, અમે પૂરી પાડેલી JSON સ્કીમાને context-free grammar (CFG)માં રૂપાંતરિત કરીએ છીએ. grammar એ નિયમોનો સમૂહ છે જે ભાષાને વ્યાખ્યાયિત કરે છે, અને context-free grammar એ એવી grammar છે જે ચોક્કસ નિયમોનું પાલન કરે છે. તમે JSON અને JSON સ્કીમાને એવા વિશિષ્ટ ભાષાઓ તરીકે વિચારી શકો છો, જેમા ભાષાની અંદર શું માન્ય છે તે વ્યાખ્યાયિત કરવા માટે નિયમો હોય છે. જેમ અંગ્રેજીમાં ક્રિયાપદ વગરનું વાક્ય માન્ય નથી, તેમ JSONમાં trailing comma માન્ય નથી.

આથી, દરેક JSON સ્કીમા માટે અમે એવી grammar ગણતરી કરીએ છીએ જે તે સ્કીમાનું પ્રતિનિધિત્વ કરે છે, અને તેની ઘટકોને પ્રી-પ્રોસેસ કરીએ છીએ જેથી મોડલ સેમ્પલિંગ દરમિયાન તેને સરળતાથી ઍક્સેસ કરી શકાય. આ કારણે નવી સ્કીમા સાથેની પહેલી વિનંતિમાં લેટન્સી દંડ આવે છે—અમારે આ આર્ટિફેક્ટ જનરેટ કરવા માટે સ્કીમાને પ્રીપ્રોસેસ કરવી પડે છે, જેથી સેમ્પલિંગ દરમિયાન તેનો કાર્યક્ષમ રીતે ઉપયોગ કરી શકીએ.

સેમ્પલિંગ દરમિયાન, દરેક ટોકન પછી, અમારું inference engine અગાઉ જનરેટ થયેલા ટોકન્સ અને grammarની અંદરના નિયમોના આધારે, આગળ કયા ટોકન્સ માન્ય રીતે ઉત્પન્ન થઈ શકે તે નક્કી કરશે. પછી અમે આ ટોકન્સની યાદીનો ઉપયોગ આગળના સેમ્પલિંગ પગલાને mask કરવા માટે કરીએ છીએ, જે અસરકારક રીતે અમાન્ય ટોકન્સની શક્યતાને 0 સુધી ઘટાડે છે. કારણ કે અમે સ્કીમાને પ્રીપ્રોસેસ કરી છે, અમે આ કામ કાર્યક્ષમ રીતે કરવા માટે cached data structure નો ઉપયોગ કરી શકીએ છીએ, ખૂબ ઓછા લેટન્સી ઓવરહેડ સાથે.

વૈકલ્પિક અભિગમો

આ સમસ્યાના વૈકલ્પિક અભિગમો ઘણીવાર નિયંત્રિત ડિકોડિંગ માટે finite state machines (FSMs) અથવા regexes (સામાન્ય રીતે FSMs સાથે અમલમાં મૂકાતા) નો ઉપયોગ કરે છે. તે આ રીતે સમાન રીતે કાર્ય કરે છે કે દરેક ટોકન ઉત્પન્ન થયા પછી કયા ટોકન્સ માન્ય છે તે ગતિશીલ રીતે અપડેટ કરે છે, પરંતુ CFG અભિગમની સરખામણીએ તેમાં કેટલાક મુખ્ય તફાવતો છે. ખાસ કરીને, CFGs, FSMs કરતાં ભાષાઓના વધુ વ્યાપક વર્ગને વ્યક્ત કરી શકે છે. વ્યવહારમાં, ઉપર બતાવેલી value સ્કીમા જેવી ખૂબ સરળ સ્કીમાઓ માટે આ ફરક મહત્વનો નથી. જોકે, nested અથવા recursive ડેટા સ્ટ્રક્ચર્સને સામેલ કરતી વધુ જટિલ સ્કીમાઓ માટે આ તફાવત અર્થપૂર્ણ છે એવું અમને લાગે છે. ઉદાહરણ તરીકે, FSMs સામાન્ય રીતે recursive types વ્યક્ત કરી શકતા નથી, એટલે FSM આધારિત અભિગમો deeply nested JSONમાં parentheses મેચ કરવામાં મુશ્કેલી અનુભવી શકે છે. નીચેનું recursive સ્કીમા ઉદાહરણ OpenAI API પર સ્ટ્રક્ચર્ડ આઉટપુટ્સ સાથે સપોર્ટેડ છે, પરંતુ FSM સાથે તેને વ્યક્ત કરવું શક્ય ન હોત.

JSON

1
{
2
"name": "ui",
3
"description": "Dynamically generated UI",
4
"strict": true,
5
"schema": {
6
"type": "object",
7
"properties": {
8
"type": {
9
"type": "string",
10
"description": "The type of the UI component",
11
"enum": ["div", "button", "header", "section", "field", "form"]
12
},
13
"label": {
14
"type": "string",
15
"description": "The label of the UI component, used for buttons or form fields"
16
},
17
"children": {
18
"type": "array",
19
"description": "Nested UI components",
20
"items": {
21
"$ref": "#"
22
}
23
},
24
"attributes": {
25
"type": "array",
26
"description": "Arbitrary attributes for the UI component, suitable for any element",
27
"items": {
28
"type": "object",
29
"properties": {
30
"name": {
31
"type": "string",
32
"description": "The name of the attribute, for example onClick or className"
33
},
34
"value": {
35
"type": "string",
36
"description": "The value of the attribute"
37
}
38
}
39
}
40
}
41
},
42
"required": ["type", "label", "children", "attributes"],
43
"additionalProperties": false
44
}
45
}

નોંધ કરો કે દરેક UI એલિમેન્ટ પાસે મનસ્વી children હોઈ શકે છે, જે root સ્કીમાને recursive રીતે રેફર કરે છે. આ લવચીકતા CFG અભિગમ દ્વારા શક્ય બને છે.

મર્યાદાઓ અને પ્રતિબંધો

સ્ટ્રક્ચર્ડ આઉટપુટ્સનો ઉપયોગ કરતી વખતે ધ્યાનમાં રાખવાની કેટલીક મર્યાદાઓ છે:

  • સ્ટ્રક્ચર્ડ આઉટપુટ્સ JSON સ્કીમાના માત્ર એક ઉપસેટને જ મંજૂરી આપે છે, જેનું વિગતવાર વર્ણન અમારા docsમાં(નવી વિન્ડોમાં ખૂલે છે) છે. આ અમને શક્ય તેટલું શ્રેષ્ઠ પ્રદર્શન સુનિશ્ચિત કરવામાં મદદ કરે છે.
  • નવી સ્કીમા સાથેના પહેલા API રિસ્પોન્સમાં વધારાનો લેટન્સી લાગશે, પરંતુ ત્યારબાદના રિસ્પોન્સ ઝડપી હશે અને કોઈ લેટન્સી દંડ નહીં હોય. આવું એ કારણે છે કે પ્રથમ વિનંતિ દરમિયાન, અમે ઉપર દર્શાવ્યા મુજબ સ્કીમાને પ્રોસેસ કરીએ છીએ અને પછી આગળ ઝડપી પુનઃઉપયોગ માટે આ આર્ટિફેક્ટ્સને કેશ કરીએ છીએ. સામાન્ય સ્કીમાઓને પ્રથમ વિનંતિ પર પ્રોસેસ થવામાં 10 સેકન્ડથી ઓછો સમય લાગે છે, પરંતુ વધુ જટિલ સ્કીમાઓને એક મિનિટ સુધી લાગી શકે છે.
  • જો મોડલ અસુરક્ષિત વિનંતિને નકારવાનું પસંદ કરે તો તે સ્કીમાનું પાલન કરવામાં નિષ્ફળ જઈ શકે છે. જો તે નકારવાનું પસંદ કરે, તો આ દર્શાવવા માટે રિટર્ન મેસેજમાં refusal બૂલિયન true પર સેટ હશે. 
  • જો જનરેશન પૂર્ણ થાય તે પહેલાં max_tokens અથવા બીજી કોઈ stop condition આવી જાય, તો મોડલ સ્કીમાનું પાલન કરવામાં નિષ્ફળ જઈ શકે છે. 
  • સ્ટ્રક્ચર્ડ આઉટપુટ્સ દરેક પ્રકારની મોડલ ભૂલોને અટકાવતા નથી. ઉદાહરણ તરીકે, મોડલ JSON ઑબ્જેક્ટના મૂલ્યોની અંદર હજુ પણ ભૂલો કરી શકે છે (જેમ કે ગણિતીય સમીકરણમાં પગલું ખોટું આવવું). જો ડેવલપર્સને ભૂલો મળે, તો અમે સિસ્ટમ સૂચનાઓમાં ઉદાહરણો આપવાની અથવા કાર્યોને વધુ સરળ ઉપલક્ષ્યોમાં વહેંચવાની ભલામણ કરીએ છીએ.
  • સ્ટ્રક્ચર્ડ આઉટપુટ્સ સમાંતર ફંક્શન કોલ્સ સાથે સુસંગત નથી. જ્યારે સમાંતર ફંક્શન કોલ જનરેટ થાય છે, ત્યારે તે પૂરી પાડેલી સ્કીમાઓ સાથે મેળ ન ખાઈ શકે. સમાંતર ફંક્શન કોલિંગને અક્ષમ કરવા માટે parallel_tool_calls: false સેટ કરો.
  • સ્ટ્રક્ચર્ડ આઉટપુટ્સ સાથે પૂરી પાડવામાં આવેલી JSON સ્કીમાઓ ઝીરો ડેટા રિટેન્શન(નવી વિન્ડોમાં ખૂલે છે) (ZDR) માટે પાત્ર નથી.

ઉપલબ્ધતા

સ્ટ્રક્ચર્ડ આઉટપુટ્સ આજે APIમાં સામાન્ય રીતે ઉપલબ્ધ છે. 

ફંક્શન કોલિંગ સાથેના સ્ટ્રક્ચર્ડ આઉટપુટ્સ APIમાં ફંક્શન કોલિંગને સપોર્ટ કરતા બધા મોડલ્સ પર ઉપલબ્ધ છે. તેમાં અમારા નવા મોડલ્સ (
gpt-4o, gpt-4o-mini), gpt-4-0613 અને gpt-3.5-turbo-0613 સહિત અને તે પછીના બધા મોડલ્સ, તેમજ ફંક્શન કોલિંગને સપોર્ટ કરતા કોઈપણ ફાઇન-ટ્યુન્ડ મોડલ્સનો સમાવેશ થાય છે. આ સુવિધા Chat Completions API, Assistants API અને Batch API પર ઉપલબ્ધ છે. ફંક્શન કોલિંગ સાથેના સ્ટ્રક્ચર્ડ આઉટપુટ્સ વિઝન ઇનપુટ્સ સાથે પણ સુસંગત છે.

રિસ્પોન્સ ફોર્મેટ્સ સાથેના સ્ટ્રક્ચર્ડ આઉટપુટ્સ
gpt-4o-mini અને gpt-4o-2024-08-06 તેમજ આ મોડલ્સ પર આધારિત કોઈપણ ફાઇન ટ્યુન્સ પર ઉપલબ્ધ છે. આ સુવિધા Chat Completions API, Assistants API અને Batch API પર ઉપલબ્ધ છે. રિસ્પોન્સ ફોર્મેટ્સ સાથેના સ્ટ્રક્ચર્ડ આઉટપુટ્સ વિઝન ઇનપુટ્સ સાથે પણ સુસંગત છે. 

નવા
gpt-4o-2024-08-06 પર સ્વિચ કરીને, ડેવલપર્સ gpt-4o-2024-05-13ની સરખામણીએ ઇનપુટ્સ પર 50% ($2.50/1M input tokens) અને આઉટપુટ્સ પર 33% ($10.00/1M output tokens) બચાવે છે.

સ્ટ્રક્ચર્ડ આઉટપુટ્સનો ઉપયોગ શરૂ કરવા માટે, અમારા
docs(નવી વિન્ડોમાં ખૂલે છે) જુઓ. 

આભારવિધિ

સ્ટ્રક્ચર્ડ આઉટપુટ્સ ઓપન સોર્સ સમુદાયના ઉત્તમ કાર્યથી પ્રેરણા લે છે: ખાસ કરીને outlines(નવી વિન્ડોમાં ખૂલે છે), jsonformer(નવી વિન્ડોમાં ખૂલે છે), instructor(નવી વિન્ડોમાં ખૂલે છે), guidance(નવી વિન્ડોમાં ખૂલે છે), અને lark(નવી વિન્ડોમાં ખૂલે છે) લાઇબ્રેરીઓ.

લેખક

Michelle Pokrass

મુખ્ય યોગદાનકર્તાઓ

Chris Colby, Melody Guan, Michelle Pokrass, Ted Sanders, Brian Zhang

આભારવિધિ

John Allard, Filipe de Avila Belbute Peres, Ilan Bigio, Owen Campbell-Moore, Chen Ding, Atty Eleti, Elie Georges, Katia Gil Guzman, Jeff Harris, Johannes Heidecke, Beth Hoover, Romain Huet, Tomer Kaftan, Jillian Khoo, Karolis Kosas, Ryan Liu, Kevin Lu, Lindsay McCallum, Rohan Nuttall, Joe Palermo, Leher Pathak, Ishaan Singal, Felipe Petroski Such, Freddie Sulit, David Weedon