Sora Chat 创建并产出 - TimRouter API 中转端点
▼
Authorization
在发起请求头中加入配置项 Authorization,其值为 Bearer 之后拼接 Token
用例:
Authorization: Bearer ********************
向模型传入内容提示词后,它会产出一个或多个预测补全结果,同时还能给出每个位置上各候选词的概率分布。 依据所传入的提示词与各项参数来产出补全结果 官方参考说明文档:https://platform.openai.com/docs/api-reference/chat/create
入参配置
发起请求头配置项
Content-Type
string
必须传入
用例: application/json
Accept
string
必须传入
用例: application/json
Authorization
string
非必须传入
用例: Bearer {{YOUR_API_KEY}} X-Forwarded-Host
string
非必须传入
用例: api.timrouter.com
发起请求体配置项 application/json
model
string
必须传入
所需发起调用的模型标识。如需了解哪些模型适配聊天端点,请参照模型与端点的兼容性对照表。
messages
array [object]
必须传入
当前对话已累计的通信消息条目集合。附 Python 代码用例。
role
string
非必须传入
content
string
非必须传入
temperature
integer
非必须传入
采样温度的取值范围介于 0 至 2 之间。数值偏高(例如 0.8)会让产出结果更具随机性,数值偏低(例如 0.2)则会让结果更聚焦、更稳定。 通常建议二选一地调整本项或 top_p,请勿同时修改两者。
top_p
integer
非必须传入
作为温度采样的替代方案,即核采样机制,模型仅评估累计概率达到 top_p 的候选词。举例来说,0.1 表示只纳入概率质量前 10% 的标记。 通常建议二选一地调整本项或 temperature,请勿同时修改两者。
n
integer
非必须传入
默认为 1 为每个传入内容通信消息产出多少个聊天补全选择。
stream
boolean
非必须传入
默认值为 false。一旦开启,便会如同 ChatGPT 那样以增量形式推送通信消息片段。标记会以纯数据的服务器发送事件形式按需推送,最终以 data: [DONE] 这条通信消息结束整个流。附 Python 代码用例。
stop
string
非必须传入
默认值为 null,最多可设 4 个序列,命中后 API 会停止后续的标记产出。
max_tokens
integer
非必须传入
默认值为 inf,即聊天补全过程中允许产出的最大 token 数量。传入内容 token 与产出 token 的总量需服从模型的上下文长度约束。旨在计算 token 数量的 Python 代码用例。
presence_penalty
number
非必须传入
取值范围为 -2.0 至 2.0 的数值。正值会依照新标记此前是否出现过予以惩罚,进而提升模型转向新话题的概率。查阅关于频率惩罚与存在惩罚的更多详细说明。
frequency_penalty
number
非必须传入
默认取 0,数值介于 -2.0 至 2.0 之间。正值会依据文本当前的出现频率对新标记施加惩罚,从而削弱模型重复相同语句的倾向。更多关于频率惩罚与存在惩罚的详细说明。
logit_bias
null
非必须传入
用以调整特定标记在补全结果里出现的概率。需传入一个 JSON 对象,把标记(由分词器给出的标记 ID)映射为对应的偏差数值(区间为 -100 至 100)。从数学原理上看,该偏差会在采样前先行叠加到模型产出的 logit 上。实际效果会随模型不同而变化,不过处于 -1 到 1 区间的数值会弱化或强化对应标记被选中的概率;而像 -100 或 100 这类极端值则会让相关标记被彻底禁用或被独占选中。
user
string
非必须传入
旨在标识终端用户的唯一编号,便于 OpenAI 监测并识别潜在的滥用行为。了解更多。
response_format
object
非必须传入
旨在约束模型输出格式的配置对象。把 { "type": "json_object" } 设为开启 JSON 模式后,可保证模型产出的通信消息均为合法的 JSON 数据。 友情提示:启用 JSON 模式的同时,务必在 system 或 user 通信消息中明确告知模型输出 JSON。否则模型可能持续输出空白内容,直到触及 token 上限,进而造成延迟攀升、发起请求呈现“卡死”状态。另外提示,当 finish_reason="length",通信消息正文有可能被截断,这往往意味着产出量超出了 max_tokens,或对话已达到上下文长度的上限。 显示属性
seen
integer
非必须传入
该能力尚处测试阶段。一旦指定,系统会尽力以确定性方式执行采样,从而在相同种子与参数下,重复发起的发起请求可得到一致的结果。不过确定性无法被绝对保证,建议结合 system_fingerprint 响应字段来跟踪后端的变更。
tools
array[string]
必须传入
模型可发起调用的工具集合。现阶段仅函数可作为可用工具。借助该能力,可向模型供应能够为其产出 JSON 入参的函数清单。
tool_choice
object
必须传入
控制模型发起调用哪个函数(如果有的话)。none 表示模型不会发起调用函数,而是产出通信消息。auto 表示模型可以在产出通信消息和发起调用函数之间进行选择。通过 {"type": "function", "function": {"name": "my_function"}} 强制模型发起调用该函数。 如果没有函数存在,默认为 none。如果有函数存在,默认为 auto。 显示可能的类型 { "model": "sora-2", "max_tokens": 1000, "messages": [ { "role": "user", "content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur." } ], "stream": true } 发起请求
用例
{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}
发起调用代码用例
curl --location --request POST 'https://api.timrouter.com/v1/chat/completions' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}'
var myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "Bearer YOUR_API_KEY");
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://api.timrouter.com/v1/chat/completions", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
import java.io.*;
import java.net.*;
import java.util.*;
URL url = new URL("https://api.timrouter.com/v1/chat/completions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
String jsonInputString = "{
\"model\": \"sora-2\",
\"max_tokens\": 1000,
\"messages\": [
{
\"role\": \"user\",
\"content\": \"an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur.\"
}
],
\"stream\": true
}";
try(OutputStream os = conn.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
import Foundation
let urlString = "https://api.timrouter.com/v1/chat/completions"
guard let url = URL(string: urlString) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("Bearer YOUR_API_KEY", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let httpBody = "{
\"model\": \"sora-2\",
\"max_tokens\": 1000,
\"messages\": [
{
\"role\": \"user\",
\"content\": \"an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur.\"
}
],
\"stream\": true
}"
request.httpBody = httpBody.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
print(String(data: data, encoding: .utf8)!)
}
}
task.resume()
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
body := strings.NewReader(`{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}`)
req, _ := http.NewRequest("POST", "https://api.timrouter.com/v1/chat/completions", body)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
fmt.Println(string(bodyBytes))
}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.timrouter.com/v1/chat/completions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}',
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Authorization: Bearer YOUR_API_KEY",
"Content-Type: application/json",
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import http.client
import json
conn = http.client.HTTPSConnection("api.timrouter.com")
payload = json.dumps({
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
})
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
POST https://api.timrouter.com/v1/chat/completions HTTP/1.1
Accept: application/json
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.timrouter.com/v1/chat/completions");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer YOUR_API_KEY");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{
\"model\": \"sora-2\",
\"max_tokens\": 1000,
\"messages\": [
{
\"role\": \"user\",
\"content\": \"an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur.\"
}
],
\"stream\": true
}");
CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://api.timrouter.com/v1/chat/completions");
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Authorization", "Bearer YOUR_API_KEY");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", @"{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
#import <Foundation/Foundation.h>
NSURL *url = [NSURL URLWithString:@"https://api.timrouter.com/v1/chat/completions"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Bearer YOUR_API_KEY" forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[@"{
\"model\": \"sora-2\",
\"max_tokens\": 1000,
\"messages\": [
{
\"role\": \"user\",
\"content\": \"an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur.\"
}
],
\"stream\": true
}" dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
require "uri"
require "net/http"
require "json"
url = URI("https://api.timrouter.com/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer YOUR_API_KEY"
request["Content-Type"] = "application/json"
request.body = '{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}'
response = http.request(request)
puts response.read_body
(* Requires cohttp and lwt *)
let url = "https://api.timrouter.com/v1/chat/completions" in
let headers = Cohttp.Header.of_list [
("Accept", "application/json");
("Authorization", "Bearer YOUR_API_KEY");
("Content-Type", "application/json");
] in
let body = Cohttp_lwt.Body.of_string '{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}' in
Lwt_main.run (
Cohttp_lwt_unix.Client.request ?body:(Some body) ~method_:`POST ~headers (Uri.of_string url)
>>= fun (resp, body) ->
Cohttp_lwt.Body.to_string body >|= fun s -> print_endline s
)
import 'package:http/http.dart' as http;
import 'dart:convert';
var headers = {
"Accept": "application/json",
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
};
var body = json.encode({
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
});
var response = await http.post(Uri.parse("https://api.timrouter.com/v1/chat/completions"), headers: headers, body: body);
print(response.body);
library(httr)
url <- "https://api.timrouter.com/v1/chat/completions"
body <- '{
"model": "sora-2",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "an astronaut golden retriever named Sora levitates around an intergalactic pup-themed space station with a tiny jet back that propels him. gorgeous specular lighting and comets fly through the sky, retro-future astro-themed music plays in the background. light glimmers off the dog's eyes. the dog initially propels towards the space station with the doors opening to let him in. the shot then changes. now inside the space station, many tennis balls are flying around in zero gravity. the dog's astronaut helmet opens up so he can grab one. 35mm film, the intricate details and texturing of the dog's hair are clearly visible and the light of the comets shimmers off the fur."
}
],
"stream": true
}'
response <- post(url, body = body, add_headers("Accept" = "application/json", "Authorization" = "Bearer YOUR_API_KEY", "Content-Type" = "application/json"))
content(response, "text", encoding = "UTF-8")
端点响应
输出参数详细说明 🟢 200 OK · application/json
id
string
必须传入
object
string
必须传入
created
integer
必须传入
choices
array [object]
必须传入
index
integer
非必须传入
message
object
非必须传入
finish_reason
string
非必须传入
usage
object
必须传入
prompt_tokens
integer
必须传入
completion_tokens
integer
必须传入
total_tokens
integer
必须传入
{ "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "choices": [ { "index": 0, "message": { "role": "assistant", "content": "\n\nHello there, how may I assist you today?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } }
用例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}