外观
Go / Java / PHP / C#
以下示例只使用各语言的标准库发送 HTTP 请求,不依赖第三方 SDK。各语言社区的 OpenAI SDK 同样可用,只需把 Base URL 设置为 https://vip.zen-ai.top/v1。
go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
body, _ := json.Marshal(map[string]any{
"model": "gpt-4.1-mini",
"messages": []map[string]string{
{"role": "user", "content": "你好,请介绍一下你自己"},
},
})
req, _ := http.NewRequest("POST", "https://vip.zen-ai.top/v1/chat/completions", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("VIP_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var result struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
data, _ := io.ReadAll(resp.Body)
if err := json.Unmarshal(data, &result); err != nil || len(result.Choices) == 0 {
fmt.Println(resp.Status, string(data))
return
}
fmt.Println(result.Choices[0].Message.Content)
}java
// Java 15+(文本块语法),只使用 JDK 自带的 HttpClient
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Main {
public static void main(String[] args) throws Exception {
String body = """
{
"model": "gpt-4.1-mini",
"messages": [{"role": "user", "content": "你好,请介绍一下你自己"}]
}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://vip.zen-ai.top/v1/chat/completions"))
.timeout(Duration.ofMinutes(5))
.header("Authorization", "Bearer " + System.getenv("VIP_API_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body()); // 用 Jackson / Gson 解析 choices[0].message.content
}
}php
<?php
$payload = [
'model' => 'gpt-4.1-mini',
'messages' => [
['role' => 'user', 'content' => '你好,请介绍一下你自己'],
],
];
$ch = curl_init('https://vip.zen-ai.top/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 300,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('VIP_API_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE),
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
echo $data['choices'][0]['message']['content'] ?? $response;csharp
// .NET 6+
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
var http = new HttpClient { Timeout = TimeSpan.FromMinutes(5) };
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Environment.GetEnvironmentVariable("VIP_API_KEY"));
var response = await http.PostAsJsonAsync("https://vip.zen-ai.top/v1/chat/completions", new
{
model = "gpt-4.1-mini",
messages = new[] { new { role = "user", content = "你好,请介绍一下你自己" } }
});
using var doc = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
Console.WriteLine(doc.RootElement
.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetString());流式输出要点
所有语言处理流式响应的方式相同:
- 请求体加上
"stream": true。 - 逐行读取响应体,只处理以
data:开头的行。 - 遇到
data: [DONE]结束;其余每行去掉前缀后是一个 JSON,增量文本位于choices[0].delta.content。 - 读取超时要设置得足够长,或按行读取时不设置总超时。