发送消息
适用于:文本生成应用。
向文本生成型应用发送请求并返回生成的文本。
1 min read
post/completion-messages
Request example
curl --request POST \
--url https://api.dify.ai/v1/completion-messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"inputs": {
"city": "New York"
},
"query": "Summarize the following text: ...",
"response_mode": "blocking",
"user": "def-456"
}'import requests
url = "https://api.dify.ai/v1/completion-messages"
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json",
}
payload = {
"inputs": {
"city": "New York"
},
"query": "Summarize the following text: ...",
"response_mode": "blocking",
"user": "def-456"
}
response = requests.request("POST", url, headers=headers, json=payload)
print(response.text)const response = await fetch('https://api.dify.ai/v1/completion-messages', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"inputs": {
"city": "New York"
},
"query": "Summarize the following text: ...",
"response_mode": "blocking",
"user": "def-456"
})
});
const data = await response.json();
console.log(data);<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.dify.ai/v1/completion-messages',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"inputs": {
"city": "New York"
},
"query": "Summarize the following text: ...",
"response_mode": "blocking",
"user": "def-456"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer <token>',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.dify.ai/v1/completion-messages"
payload := strings.NewReader("{\n \"inputs\": {\n \"city\": \"New York\"\n },\n \"query\": \"Summarize the following text: ...\",\n \"response_mode\": \"blocking\",\n \"user\": \"def-456\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.dify.ai/v1/completion-messages"))
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString("{\n \"inputs\": {\n \"city\": \"New York\"\n },\n \"query\": \"Summarize the following text: ...\",\n \"response_mode\": \"blocking\",\n \"user\": \"def-456\"\n}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());require 'uri'
require 'net/http'
url = URI('https://api.dify.ai/v1/completion-messages')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = url.scheme == 'https'
request = Net::HTTP::Post.new(url)
request['Authorization'] = 'Bearer <token>'
request['Content-Type'] = 'application/json'
request.body = '{
"inputs": {
"city": "New York"
},
"query": "Summarize the following text: ...",
"response_mode": "blocking",
"user": "def-456"
}'
response = http.request(request)
puts response.read_bodyResponse
{
"answer": "Hello World!...",
"created_at": 1705407629,
"event": "message",
"id": "b01a39de-3480-4f3e-9f1e-4841a80f8e5e",
"message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
"metadata": {
"usage": {
"completion_price": "0.0002560",
"completion_price_unit": "0.001",
"completion_tokens": 128,
"completion_unit_price": "0.002",
"currency": "USD",
"latency": 0.7682376249867957,
"prompt_price": "0.0010330",
"prompt_price_unit": "0.001",
"prompt_tokens": 1033,
"prompt_unit_price": "0.001",
"total_price": "0.0012890",
"total_tokens": 1161
}
},
"mode": "completion",
"task_id": "c3800678-a077-43df-a102-53f23ed20b88"
}data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": "'m", "created_at": 1679586595}
data: {"event": "message_end", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "id": "5e52ce04-874b-4d27-9045-b3bc80def685", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "metadata": {"usage": {"prompt_tokens": 1033, "prompt_unit_price": "0.001", "prompt_price_unit": "0.001", "prompt_price": "0.0010330", "completion_tokens": 135, "completion_unit_price": "0.002", "completion_price_unit": "0.001", "completion_price": "0.0002700", "total_tokens": 1168, "total_price": "0.0013030", "currency": "USD", "latency": 1.381760165997548}}}
{
"code": "app_unavailable",
"message": "App unavailable, please check your app configurations.",
"status": 400
}{
"code": "completion_request_error",
"message": "Completion request failed.",
"status": 400
}{
"code": "invalid_param",
"message": "Arg user must be provided.",
"status": 400
}{
"code": "model_currently_not_support",
"message": "Dify Hosted OpenAI trial currently not support the GPT-4 model.",
"status": 400
}{
"code": "provider_not_initialize",
"message": "No valid model provider credentials found. Please go to Settings -> Model Provider to complete your provider credentials.",
"status": 400
}{
"code": "provider_quota_exceeded",
"message": "Your quota for Dify Hosted OpenAI has been exhausted. Please go to Settings -> Model Provider to complete your own provider credentials.",
"status": 400
}{
"code": "too_many_requests",
"message": "Too many requests. Please try again later. The current maximum concurrent requests allowed for 5b3d4f2a-1c9e-4a7b-8f6d-2e0c9a1b3d4f is 10.",
"status": 429
}{
"code": "internal_server_error",
"message": "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.",
"status": 500
}post/completion-messages
Authorization
Body
application/jsonShow child attributesHide child attributes
响应返回模式。
streaming(推荐)使用 SSE。blocking 等待完成后返回(长时间处理可能会被中断)。Cloudflare 超时为 100 s。省略时默认为阻塞式返回。Options:
streamingblockingResponses
请求成功。内容类型和结构取决于请求中的 response_mode 参数。
- 如果
response_mode为blocking,返回application/json和CompletionResponse对象。 - 如果
response_mode为streaming,返回text/event-stream和ChunkCompletionEvent对象流。
完整的响应内容。
消息创建时间戳(Unix 纪元秒)。
事件类型,固定为
message。该响应事件的唯一 ID。
唯一的消息 ID。调用反馈或推荐问题接口时,将此值作为
message_id 参数使用。包含用量和检索资源的元数据。
Show child attributesHide child attributes
使用的检索资源列表。
Show child attributesHide child attributes
资源的内容片段。
创建时间戳(Unix 纪元秒)。
数据源类型。
知识库 ID。
知识库名称。
文档 ID。
文档名称。
该块被命中的次数。
检索资源的唯一 ID。
索引节点的哈希值。
该资源所属消息的 ID。
资源在列表中的位置。
资源的相关性评分。
文档中特定块的 ID。
块在文档中的位置。
块内容的摘要。
块的字数。
模型使用信息。
Show child attributesHide child attributes
补全令牌的总价。
补全令牌的价格单位。
补全内容中的令牌数。
每个补全令牌的单价。
计价货币。
延迟时间(秒)。
提示词令牌的总价。
提示词令牌的价格单位。
提示词中的令牌数。
每个提示词令牌的单价。
所有令牌的总价。
使用的令牌总数。
应用模式,固定为
completion。