API

เครื่องมือสร้างโค้ด API

เลือกภาษาและงานเพื่อรับโค้ดที่เป็นไปตามเอกสารของ Reflection รวมถึงรายละเอียดที่แตกต่างจาก OpenAI

Beam-501B-A23B
ภาษา
reasoning_effort
เว้นว่างไว้เพื่อใช้ค่าเริ่มต้นของ API การให้เหตุผลจะนับรวมในขีดจำกัดนี้

ติดตั้งและตั้งค่า key

bash
pip install openai
export REFLECTION_API_KEY="<your API key>"

โค้ด

main.py
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.reflection.ai/openai/v1",
    api_key=os.environ["REFLECTION_API_KEY"],
)

messages = [
    {"role": "user", "content": "Write a short poem about sparse experts."},
]

stream = client.chat.completions.create(
    model="Beam-501B-A23B",
    messages=messages,
    reasoning_effort="medium",
    stream=True,
    stream_options={"include_usage": True},
)

for chunk in stream:
    if not chunk.choices:  # the last chunk carries only usage
        print("\n", chunk.usage)
        continue
    delta = chunk.choices[0].delta
    reasoning = getattr(delta, "reasoning_content", None)
    if reasoning:
        print(reasoning, end="", flush=True)
    if delta.content:
        print(delta.content, end="", flush=True)
การให้เหตุผลจะถูกสตรีมก่อนใน delta.reasoning_content แล้วจึงตามด้วยคำตอบใน delta.content ส่วน include_usage จะเพิ่ม chunk สุดท้ายที่มีจำนวนโทเค็น

การรองรับพารามิเตอร์

รองรับ
  • model, messages ที่มีบทบาท system, developer, user, assistant และ tool
  • reasoning_effort: low, medium, high, xhigh, max
  • stream และ stream_options.include_usage
  • tools, tool_choice, parallel_tool_calls
  • response_format: json_object และ json_schema พร้อม strict
  • temperature, top_p, frequency_penalty, presence_penalty, seed, max_completion_tokens
ไม่รองรับ
  • อินพุตรูปภาพ เสียง และไฟล์
  • n ที่ไม่ใช่ 1, logprobs, logit_bias
  • user, metadata, audio และ prediction จะทำให้เกิดข้อผิดพลาด
  • รับ stop แต่ไม่มีผล
  • API ของ Responses, Embeddings, Images, Audio, Files, Batch และ Assistants

ข้อผิดพลาดที่ควรจัดการ

สถานะและรหัสความหมาย
400 unsupported_valueค่า reasoning_effort ที่โมเดลไม่รองรับ เช่น none หรือ minimal
400 missing_required_parameterมีการส่งข้อความ assistant ก่อนหน้าโดยไม่มี reasoning_content หรือไม่มี json_schema
401ไม่มี API key หรือ API key ไม่ถูกต้อง
429 rate_limit_exceededถึงขีดจำกัดต่อนาทีหรือรายวันแล้ว ให้รอตาม Retry-After
503 inference_capacity_unavailableความจุหมดชั่วคราว ให้ลองใหม่หลังจาก Retry-After

แหล่งที่มา: openai-compatibility · reasoning · tool-calling · structured-outputs · errors