메인 콘텐츠로 건너뛰기
JSON 모드를 활성화하면 모델이 응답을 유효한 JSON 형식으로 반환하도록 지시합니다. 하지만 응답의 스키마가 일관되지 않거나 특정 구조를 따르지 않을 수 있습니다. 일관된 구조의 JSON 응답이 필요한 경우, 가능한 한 structured output을 사용하는 것을 권장합니다. JSON 모드를 활성화하려면 요청 시 “response_format”을 다음과 같이 지정하세요:
import json
import openai

client = openai.OpenAI(
    base_url='https://api.inference.wandb.ai/v1',
    api_key="<your-api-key>",  # https://wandb.ai/settings 에서 API 키를 생성하세요
)

response = client.chat.completions.create(
    model="openai/gpt-oss-20b",
    messages=[
        {"role": "system", "content": "You are a helpful assistant that outputs JSON."},
        {"role": "user", "content": "Give me a list of three fruits with their colors."},
    ],
    response_format={"type": "json_object"}  # JSON 모드를 활성화합니다
)

content = response.choices[0].message.content
parsed = json.loads(content)
print(parsed)