Skip to content

代码示例

代码示例

Python — 流式输出

from openai import OpenAI

client = OpenAI(
    api_key="your-key-here",
    base_url="https://novalinkchina.com/v1"
)

stream = client.chat.completions.create(
    model="hy3",
    messages=[{"role": "user", "content": "写一首关于秋天的诗"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Python — 异步调用

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key="your-key-here",
    base_url="https://novalinkchina.com/v1"
)

async def main():
    tasks = [client.chat.completions.create(
        model="claude-sonnet-5",
        messages=[{"role": "user", "content": f"Question {i}"}]
    ) for i in range(5)]
    results = await asyncio.gather(*tasks)
    for r in results:
        print(r.choices[0].message.content)

asyncio.run(main())

Node.js — 基础调用

npm install openai

const OpenAI = require("openai");

const client = new OpenAI({
  apiKey: "your-key-here",
  baseURL: "https://novalinkchina.com/v1"
});

async function main() {
  const response = await client.chat.completions.create({
    model: "hy3",
    messages: [{ role: "user", content: "你好!" }]
  });
  console.log(response.choices[0].message.content);
}

main();

Node.js — 流式输出

const stream = await client.chat.completions.create({
  model: "deepseek-v4-flash",
  messages: [{ role: "user", content: "讲讲量子计算" }],
  stream: true
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}

Go

package main

import (
  "bytes"
  "encoding/json"
  "fmt"
  "net/http"
)

func main() {
  body := map[string]interface{}{
    "model": "hy3",
    "messages": []map[string]string{
      {"role": "user", "content": "你好!"},
    },
  }
  jsonBody, _ := json.Marshal(body)

  req, _ := http.NewRequest("POST", "https://novalinkchina.com/v1/chat/completions", bytes.NewBuffer(jsonBody))
  req.Header.Set("Content-Type", "application/json")
  req.Header.Set("Authorization", "Bearer your-key-here")

  client := &http.Client{}
  resp, _ := client.Do(req)
  defer resp.Body.Close()

  var result map[string]interface{}
  json.NewDecoder(resp.Body).Decode(&result)
  fmt.Println(result)
}

Novalink API 中转站