로딩 중
로딩 중
POST /v1/chat/completions — chat 모델, 스트리밍, 도구 및 이미지 입력.
요청 본문은 OpenAI Chat Completions와 동일합니다: model, messages, stream, temperature, tools 등 필드. OpenAI 호환 제공자(OpenAI, DeepSeek, xAI)의 경우 본문은 거의 변경 없이 전달됩니다; Claude와 Gemini의 경우 요청과 응답은 OpenAI 형식으로 상호 변환됩니다.
from openai import OpenAIimport os client = OpenAI( base_url="https://llmproxy.org/v1", # the only difference from OpenAI api_key=os.environ["MAXAI_API_KEY"],) resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{"role": "user", "content": "Explain what an API gateway is in two sentences."}], stream=True,)for chunk in resp: print(chunk.choices[0].delta.content or "", end="")
여기서는 chat 유형의 모델만 작동합니다. 다른 유형을 사용하면 400이 반환됩니다.
stream: true로 설정하면 응답이 OpenAI 형식의 SSE 청크로 도착하며 data: [DONE]로 종료됩니다. OpenAI와 마찬가지로 stream_options: {include_usage: true}를 전달하면 usage를 포함한 청크가 전송됩니다. 추론 모델은 추론 내용을 delta.reasoning_content에 전송합니다.
스트림에서는 마지막 프레임 뒤에 SSE 코멘트 : maxai-charged-cents <number>로 청구 금액이 표시됩니다. SDK는 코멘트를 무시하므로 문제가 발생하지 않습니다.
max_tokens를 전달하지 않으면 출력 토큰에 대해 4096 토큰 상한이 적용되며 동일한 양이 잔액에 보류됩니다. 추론 모델(GPT-5와 GPT-6, DeepSeek Thinking 및 Pro)은 긴 답변에 더 많은 토큰이 필요할 수 있으니 직접 max_completion_tokens를 전달하세요.
OpenAI 모델에는 max_tokens를 보낼 수도 있습니다: 내부적으로 max_completion_tokens로 이름이 바뀝니다. 모델 한도를 초과한 값은 최대치로 조정됩니다.
tools와 tool_choice는 이를 지원하는 모델에서 작동합니다. OpenAI 호환 제공자에는 그대로 전달되며, Claude와 Gemini의 경우 네이티브 형식으로 변환되고 tool_calls는 OpenAI 형식으로 반환됩니다. 도구의 결과는 tool 역할의 메시지로 전송하세요.
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Current weather in a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}] resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{"role": "user", "content": "What is the weather in Lisbon?"}], tools=tools,)call = resp.choices[0].message.tool_calls[0]print(call.function.name, call.function.arguments)
response_format (json_object, json_schema)는 OpenAI 호환 제공자에게 변경 없이 전달됩니다. Claude와 Gemini의 경우 이 필드는 번역되지 않으므로 프롬프트에서 JSON을 요청하고 응답을 자체적으로 검증하세요.
resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{"role": "user", "content": "Return JSON with the city and country fields for Paris."}], response_format={"type": "json_object"},)print(resp.choices[0].message.content)
image_url 파트를 메시지 content에 추가하세요: https:// 링크 또는 data:image/…;base64,…. 모델은 이미지 입력을 지원해야 합니다. 각 이미지는 잔액 보류에 약 2,000 입력 토큰을 추가하며, 실제 사용량에 따라 요금이 부과됩니다.
resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{ "role": "user", "content": [ {"type": "text", "text": "What is in this image?"}, {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}, ], }],)
비스트리밍 응답에는 토큰 수를 포함한 usage가 포함되며, 청구 금액은 x-maxai-charged-cents 및 x-maxai-charged-usd 헤더에 표시됩니다. 제공자가 usage를 반환하지 않으면 요금은 0이 아니라 토큰 추정치를 기준으로 계산됩니다.