---
title: Chat Completions
description: رابط OpenAI برای تکمیل مکالمه با مدل‌های دانا
---

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

## نقطه پایانی

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

## پارامترها

| پارامتر | نوع | اجباری | توضیح |
|---|---|---|---|
| `model` | string | بله | شناسه مدل: `dana-1` یا `dana-1-fast` |
| `messages` | array | بله | آرایه پیام‌های مکالمه |
| `stream` | boolean | خیر | ارسال پاسخ به صورت SSE (پیش‌فرض: `false`) |
| `max_tokens` | integer | خیر | حداکثر توکن در پاسخ |
| `temperature` | number | خیر | تنوع خروجی: ۰ تا ۲ (پیش‌فرض: ۱) |
| `top_p` | number | خیر | nucleus sampling (پیش‌فرض: ۱) |
| `n` | integer | خیر | تعداد پاسخ‌های موازی (پیش‌فرض: ۱) |
| `stop` | string/array | خیر | توکن‌های توقف |
| `user` | string | خیر | شناسه کاربر نهایی |

### ساختار پیام

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

## نمونه‌ها

**curl:**

```bash
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):**

```python
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):**

```js
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 ارسال می‌شود:

```bash
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)

```json
{
  "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
  }
}
```

## خطاها

برای آشنایی با خطاهای احتمالی به [خطاها](/docs/errors) مراجعه کنید.
