POST /v1/chat/completions,获取一个完整的响应。对于新的集成,Responses API 用一种请求格式就能覆盖文本、多模态输入、工具和更丰富的输出项。
const chat = await client.generateText({
model: "openai/gpt-6-astra",
messages: [
{ role: "system", content: "You are helpful." },
{ role: "user", content: "Write a limerick about lighthouses." },
],
});
console.log(chat.choices[0].message.content);
chat = client.generate_text({
"model": "openai/gpt-6-astra",
"messages": [
{"role": "system", "content": "You are helpful."},
{"role": "user", "content": "Write a limerick about lighthouses."},
],
})
print(chat["choices"][0]["message"]["content"])
import (
"context"
"fmt"
phaseo "github.com/phaseoteam/Phaseo/packages/sdk/sdk-go/v3"
)
client, err := phaseo.NewFromEnv()
if err != nil {
panic(err)
}
resp, err := client.GenerateText(context.Background(), phaseo.ChatCompletionsRequest{
Model: "openai/gpt-6-astra",
Messages: []map[string]interface{}{
{"role": "user", "content": "Write a one-line haiku."},
},
})
if err != nil {
panic(err)
}
fmt.Println(resp["choices"])
using PhaseoSdk;
var client = new Phaseo();
var response = await client.GenerateText(new Dictionary<string, object>
{
["model"] = "openai/gpt-6-astra",
["messages"] = new[]
{
new Dictionary<string, object>
{
["role"] = "user",
["content"] = "Write a limerick about lighthouses.",
},
},
});
<?php
require 'vendor/autoload.php';
use Phaseo\Sdk\Phaseo;
$client = new Phaseo();
$response = $client->createChatCompletion([
'model' => 'openai/gpt-6-astra',
'messages' => [[
'role' => 'user',
'content' => 'Write a limerick about lighthouses.',
]],
]);
echo $response['choices'][0]['message']['content'];
require 'phaseo_sdk'
client = PhaseoSdk::Phaseo.new
response = client.create_chat_completion(
model: 'openai/gpt-6-astra',
messages: [{ role: 'user', content: 'Write a limerick about lighthouses.' }]
)
import app.phaseo.sdk.Phaseo;
import java.util.List;
import java.util.Map;
Phaseo client = new Phaseo(System.getenv("PHASEO_API_KEY"));
var response = client.createChatCompletion(Map.of(
"model", "openai/gpt-6-astra",
"messages", List.of(
Map.of("role", "user", "content", "Write a one-line haiku.")
)
));
System.out.println(response.path("choices").path(0).path("message").path("content").asText());
use phaseo::Phaseo;
use serde_json::json;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Phaseo::from_env()?;
let response = client.chat_completions(&json!({
"model": "openai/gpt-6-astra",
"messages": [
{
"role": "system",
"content": "Answer concisely."
},
{
"role": "user",
"content": "What is provider fallback?"
}
]
}))?;
let text = response
.body
.pointer("/choices/0/message/content")
.and_then(|value| value.as_str())
.unwrap_or("");
println!("{text}");
Ok(())
}
| 语言 | 方法 | 返回值 |
|---|---|---|
| TypeScript | client.generateText(request);client.chat.completions.create(request) 的作用相同,并在 stream 为 true 时切换为流式输出 | ChatCompletionsResponse |
| Python | client.generate_text(request);也可用 client.chat.completions.create(request) | APIResponse dict |
| Go | client.GenerateText(ctx, req),别名 CreateChatCompletion;接受 phaseo.ChatCompletionsRequest | map[string]interface{} |
| C# | client.GenerateText(request),别名 CreateChatCompletion | Dictionary<string, object>? |
| PHP | $client->generateText($request),别名 createChatCompletion() | 数组 |
| Ruby | client.generate_text(...),别名 create_chat_completion | Hash |
| Java | client.createChatCompletion(request);接受 Map 或 Jackson JsonNode | Jackson JsonNode |
| Rust | client.chat_completions(&request) | PhaseoResponse;JSON 位于 body 中 |
choices[0].message.content,旁边还有 finish_reason 和 usage。
流式输出响应
for await (const chunk of client.streamChat({
model: "openai/gpt-6-astra",
messages: [{ role: "user", content: "Stream a story" }],
})) {
if (chunk.text) process.stdout.write(chunk.text);
}
for chunk in client.stream_chat({
"model": "openai/gpt-6-astra",
"messages": [{"role": "user", "content": "Stream a story"}],
}):
if chunk.get("text"):
print(chunk["text"], end="", flush=True)
import app.phaseo.sdk.Phaseo;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
Phaseo client = new Phaseo(System.getenv("PHASEO_API_KEY"));
var payload = Map.of(
"model", "openai/gpt-6-astra",
"messages", List.of(Map.of("role", "user", "content", "Hello")),
"stream", true
);
try (var lines = client.rawClient().requestLines(
"POST", "/chat/completions", null, null,
new ObjectMapper().writeValueAsString(payload)
)) {
lines.forEach(System.out::println);
}
| 语言 | 如何流式输出 |
|---|---|
| TypeScript | client.streamChat(request) 产出解析后的数据块;client.streamText() 产出原始 SSE 行。 |
| Python | client.stream_chat(request) 产出解析后的数据块;client.stream_text() 产出原始 SSE 行。 |
| Java | createChatCompletion 返回一个 JSON 正文。设置 stream: true,并用 rawClient().requestLines 发送请求,它会在 SSE 行到达时逐行返回;请用 try-with-resources 关闭它。 |
| Go、C#、PHP、Ruby、Rust | 没有 Chat Completions 流式输出辅助方法;请不要设置 stream。在 C#、PHP 和 Ruby 中,Responses 提供了流式输出方法。 |
choices、id 等),并增加 text(本数据块新增的内容)、usage 和推理 token 数(reasoningTokens / reasoning_tokens)。
参数
在每个 SDK 中,字段名都是 JSON 正文字段。Go 的带类型phaseo.ChatCompletionsRequest 使用 PascalCase 结构体字段(Model、Messages)。
| 分组 | 字段 |
|---|---|
| 必填 | model;messages,角色为 system、developer、user、assistant 或 tool,内容为字符串或内容片段 |
| 采样 | temperature(0–2)、top_p(0–1)、top_k(-1 或更大)、min_p(0–1)、seed,仅在模型支持时生效 |
| 长度和惩罚 | max_tokens 或 max_completion_tokens、presence_penalty 和 frequency_penalty(-2 到 2)、repetition_penalty(大于 0)、stop(字符串或数组) |
| 工具 | tools、tool_choice、max_tool_calls、parallel_tool_calls |
| 推理 | reasoning 对象或 reasoning_effort |
| 对数概率 | logprobs、top_logprobs(0–20) |
| 输出 | response_format、n(1–16)、stream、service_tier、metadata(字符串值) |
| Gateway | provider 路由、usage(包含用量)、meta(包含网关 meta 块) |
响应
ChatCompletionsResponse:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "openai/gpt-6-astra",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
stream: true 时,端点会发送以 data: [DONE] 结尾的 SSE 帧:
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"openai/gpt-6-astra","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"openai/gpt-6-astra","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"openai/gpt-6-astra","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":12,"total_tokens":21}}
data: [DONE]