نسخه بتا · دانا به‌زودی عرضه می‌شود
مستندات

Chat Completions

رابط OpenAI برای تکمیل مکالمه با مدل‌های دانا

POST /v1/chat/completions رابط اصلی سازگار با OpenAI است.

نقطه پایانی

POST https://api.dana.expert/v1/chat/completions

پارامترها

پارامترنوعاجباریتوضیح
modelstringبلهشناسه مدل: dana-1 یا dana-1-fast
messagesarrayبلهآرایه پیام‌های مکالمه
streambooleanخیرارسال پاسخ به صورت SSE (پیش‌فرض: false)
max_tokensintegerخیرحداکثر توکن در پاسخ
temperaturenumberخیرتنوع خروجی: ۰ تا ۲ (پیش‌فرض: ۱)
top_pnumberخیرnucleus sampling (پیش‌فرض: ۱)
nintegerخیرتعداد پاسخ‌های موازی (پیش‌فرض: ۱)
stopstring/arrayخیرتوکن‌های توقف
userstringخیرشناسه کاربر نهایی

ساختار پیام

{"role": "system"|"user"|"assistant", "content": "متن پیام"}

نمونه‌ها

curl:

curl https://api.dana.expert/v1/chat/completions \
  -H "Authorization: Bearer $DANA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "dana-1",
    "messages": [
      {"role": "system", "content": "دستیار فارسی‌زبان هستید."},
      {"role": "user", "content": "پایتخت ایران کجاست؟"}
    ]
  }'

Python (openai SDK):

from openai import OpenAI

client = OpenAI(
    api_key="$DANA_API_KEY",
    base_url="https://api.dana.expert/v1",
)

response = client.chat.completions.create(
    model="dana-1",
    messages=[
        {"role": "system", "content": "دستیار فارسی‌زبان هستید."},
        {"role": "user", "content": "پایتخت ایران کجاست؟"},
    ],
)
print(response.choices[0].message.content)

Node.js (openai SDK):

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.DANA_API_KEY,
  baseURL: "https://api.dana.expert/v1",
});

const response = await client.chat.completions.create({
  model: "dana-1",
  messages: [
    { role: "system", content: "دستیار فارسی‌زبان هستید." },
    { role: "user", content: "پایتخت ایران کجاست؟" },
  ],
});
console.log(response.choices[0].message.content);

Streaming

با تنظیم "stream": true پاسخ به صورت رویدادهای SSE ارسال می‌شود:

curl https://api.dana.expert/v1/chat/completions \
  -H "Authorization: Bearer $DANA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"dana-1","stream":true,"messages":[{"role":"user","content":"یک داستان کوتاه بگو"}]}'

هر رویداد به شکل زیر است:

data: {"id":"chatcmpl-xyz","object":"chat.completion.chunk","choices":[{"delta":{"content":"یک"},"index":0}]}

data: [DONE]

ساختار پاسخ (non-stream)

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1700000000,
  "model": "dana-1",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "تهران پایتخت ایران است."},
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 10,
    "total_tokens": 32
  }
}

خطاها

برای آشنایی با خطاهای احتمالی به خطاها مراجعه کنید.