Skip to content

नोट

ओलामा का क्लाउड वर्तमान में संरचित आउटपुट का समर्थन नहीं करता है।

संरचित आउटपुट आपको मॉडल प्रतिक्रियाओं पर JSON स्कीमा लागू करने की अनुमति देता है ताकि आप विश्वसनीय रूप से संरचित डेटा निकाल सकें, छवियों का वर्णन कर सकें, या हर प्रतिक्रिया को समरूप रख सकें।

संरचित JSON जनरेट करना

cURL

```shell
curl -X POST http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
  "model": "gpt-oss",
  "messages": [{"role": "user", "content": "Tell me about Canada in one line"}],
  "stream": false,
  "format": "json"
}'
```

Python

```python
from ollama import chat

response = chat(
  model='gpt-oss',
  messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
  format='json'
)
print(response.message.content)
```

JavaScript

```javascript
import ollama from 'ollama'

const response = await ollama.chat({
  model: 'gpt-oss',
  messages: [{ role: 'user', content: 'Tell me about Canada.' }],
  format: 'json'
})
console.log(response.message.content)
```

स्कीमा के साथ संरचित JSON जनरेट करना

format फ़ील्ड में एक JSON स्कीमा प्रदान करें।

नोट

मॉडल की प्रतिक्रिया को ग्राउंड करने के लिए प्रॉम्प्ट में JSON स्कीमा को स्ट्रिंग के रूप में भी पास करना आदर्श है।

cURL

```shell
curl -X POST http://localhost:11434/api/chat -H "Content-Type: application/json" -d '{
  "model": "gpt-oss",
  "messages": [{"role": "user", "content": "Tell me about Canada."}],
  "stream": false,
  "format": {
    "type": "object",
    "properties": {
      "name": {"type": "string"},
      "capital": {"type": "string"},
      "languages": {
        "type": "array",
        "items": {"type": "string"}
      }
    },
    "required": ["name", "capital", "languages"]
  }
}'
```

Python

Pydantic मॉडल का उपयोग करें और `format` को `model_json_schema()` पास करें, फिर प्रतिक्रिया को वेलिडेट करें:

```python
from ollama import chat
from pydantic import BaseModel

class Country(BaseModel):
  name: str
  capital: str
  languages: list[str]

response = chat(
  model='gpt-oss',
  messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
  format=Country.model_json_schema(),
)

country = Country.model_validate_json(response.message.content)
print(country)
```

JavaScript

Zod स्कीमा को `z.toJSONSchema()` के साथ सीरियलाइज़ करें और संरचित प्रतिक्रिया को पार्स करें:

```javascript
import ollama from 'ollama'
import * as z from 'zod'

const Country = z.object({
  name: z.string(),
  capital: z.string(),
  languages: z.array(z.string()),
})

const response = await ollama.chat({
  model: 'gpt-oss',
  messages: [{ role: 'user', content: 'Tell me about Canada.' }],
  format: z.toJSONSchema(Country),
})

const country = Country.parse(JSON.parse(response.message.content))
console.log(country)
```

उदाहरण: संरचित डेटा निकालें

वह ऑब्जेक्ट्स परिभाषित करें जो आप वापस चाहते हैं और मॉडल को फ़ील्ड भरने दें:

python
from ollama import chat
from pydantic import BaseModel

class Pet(BaseModel):
  name: str
  animal: str
  age: int
  color: str | None
  favorite_toy: str | None

class PetList(BaseModel):
  pets: list[Pet]

response = chat(
  model='gpt-oss',
  messages=[{'role': 'user', 'content': 'I have two cats named Luna and Loki...'}],
  format=PetList.model_json_schema(),
)

pets = PetList.model_validate_json(response.message.content)
print(pets)

उदाहरण: संरचित आउटपुट के साथ विज़न

विज़न मॉडल समान format पैरामीटर को स्वीकार करते हैं, जो छवियों का निश्चित वर्णन सक्षम करता है:

python
from ollama import chat
from pydantic import BaseModel
from typing import Literal, Optional

class Object(BaseModel):
  name: str
  confidence: float
  attributes: str

class ImageDescription(BaseModel):
  summary: str
  objects: list[Object]
  scene: str
  colors: list[str]
  time_of_day: Literal['Morning', 'Afternoon', 'Evening', 'Night']
  setting: Literal['Indoor', 'Outdoor', 'Unknown']
  text_content: Optional[str] = None

response = chat(
  model='gemma4',
  messages=[{
    'role': 'user',
    'content': 'Describe this photo and list the objects you detect.',
    'images': ['path/to/image.jpg'],
  }],
  format=ImageDescription.model_json_schema(),
  options={'temperature': 0},
)

image_description = ImageDescription.model_validate_json(response.message.content)
print(image_description)

विश्वसनीय संरचित आउटपुट के लिए टिप्स

  • वेलिडेशन के लिए पुन: उपयोग किए जा सकने के लिए Pydantic (Python) या Zod (JavaScript) के साथ स्कीमा परिभाषित करें।
  • अधिक निश्चित पूर्णताओं के लिए टेम्परेचर कम करें (उदाहरण के लिए, इसे 0 पर सेट करें)।
  • संरचित आउटपुट OpenAI-संगत API के माध्यम से response_format के माध्यम से काम करते हैं।