Skip to content

语言模型 ​

语言模型(LLM)使用说明手册。

支持通过 OpenAI、Anthropic 和 Gemini AI Studio 兼容接口进行文本生成调用。

OpenAI 兼容接口(推荐) ​

如部分功能本文档未提及,请参考 OpenAI 官方文档。

消息体结构说明 ​

消息类型功能描述示例内容
system模型指令,设定 AI 角色,描述模型应一般如何行为和响应“你是有 10 年经验的儿科医生”
user用户输入,将最终用户的消息传递给模型“幼儿持续低烧应如何处理?”
assistant模型生成的历史回复,为模型提供示例,说明它应该如何回应当前请求“建议先测量体温……”

若希望模型按照分层指令进行响应,可以利用消息角色来提升输出的质量。不过,消息角色并不总是有确定性的表现,因此建议尝试多种用法,比较不同方法带来的效果,找出最适合您的方案。

基础对话 ​

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.calgine.com.cn/v1",
    api_key="API_KEY",  # 替换为您在控制台的 API Key
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "你好!"}],
)

print(response.choices[0].message.content)
js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.calgine.com.cn/v1",
  apiKey: "API_KEY", // 替换为您在控制台的 API Key
});

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "你好!" }],
});

console.log(response.choices[0].message.content);

流式响应 ​

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.calgine.com.cn/v1",
    api_key="API_KEY",
)

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

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.calgine.com.cn/v1",
  apiKey: "API_KEY",
});

const stream = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "写一首关于春天的诗" }],
  stream: true,
});

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

工具调用(Function Calling) ​

更多详情参考 OpenAI 函数调用指南。

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.calgine.com.cn/v1",
    api_key="API_KEY",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气信息",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "城市名称"},
                },
                "required": ["city"],
            },
        },
    }
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}],
    tools=tools,
)

print(response.choices[0].message)
js
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.calgine.com.cn/v1",
  apiKey: "API_KEY",
});

const tools = [
  {
    type: "function",
    function: {
      name: "get_weather",
      description: "获取指定城市的天气信息",
      parameters: {
        type: "object",
        properties: {
          city: { type: "string", description: "城市名称" },
        },
        required: ["city"],
      },
    },
  },
];

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "北京今天天气怎么样?" }],
  tools: tools,
});

console.log(response.choices[0].message);

Response 接口 ​

注意事项 ​

  1. 使用时需要将 OPENAI_BASE_URL 设置为 https://api.calgine.com.cn/v1
  2. OPENAI_API_KEY 应设置为您的 API Key
  3. 部分模型不支持 presence_penalty、frequency_penalty、logit_bias 等参数,传入会被忽略
  4. 旧版的 function_call 参数已废弃,请使用 tools

Anthropic 兼容接口 ​

如部分功能本文档未提及,请参考 Anthropic 官方文档。

基础对话 ​

python
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.calgine.com.cn/anthropic",
    api_key="API_KEY",  # 替换为您在控制台的 API Key
)

message = client.messages.create(
    model="claude-opus-4-5-20251101",
    max_tokens=1024,
    messages=[{"role": "user", "content": "请用简单的语言解释什么是机器学习"}],
)

print(message.content[0].text)
js
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.calgine.com.cn/anthropic",
  apiKey: "API_KEY", // 替换为您在控制台的 API Key
});

const message = await client.messages.create({
  model: "claude-opus-4-5-20251101",
  maxTokens: 1024,
  messages: [{ role: "user", content: "请用简单的语言解释什么是机器学习" }],
});

console.log(message.content[0].text);

流式响应 ​

python
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.calgine.com.cn/anthropic",
    api_key="API_KEY",
)

with client.messages.stream(
    model="claude-opus-4-5-20251101",
    max_tokens=1024,
    messages=[{"role": "user", "content": "写一首关于春天的诗"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
js
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.calgine.com.cn/anthropic",
  apiKey: "API_KEY",
});

const stream = await client.messages.stream({
  model: "claude-opus-4-5-20251101",
  maxTokens: 1024,
  messages: [{ role: "user", content: "写一首关于春天的诗" }],
});

for await (const chunk of stream) {
  if (chunk.type === "content_block_delta" && chunk.delta.type === "text_delta") {
    process.stdout.write(chunk.delta.text);
  }
}

工具调用(Function Calling) ​

更多详情参考 Anthropic 工具调用指南。

python
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.calgine.com.cn/anthropic",
    api_key="API_KEY",
)

tools = [
    {
        "name": "get_weather",
        "description": "获取指定城市的天气信息",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "城市名称"},
            },
            "required": ["city"],
        },
    }
]

message = client.messages.create(
    model="claude-opus-4-5-20251101",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}],
)

print(message.content)
js
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.calgine.com.cn/anthropic",
  apiKey: "API_KEY",
});

const tools = [
  {
    name: "get_weather",
    description: "获取指定城市的天气信息",
    input_schema: {
      type: "object",
      properties: {
        city: { type: "string", description: "城市名称" },
      },
      required: ["city"],
    },
  },
];

const message = await client.messages.create({
  model: "claude-opus-4-5-20251101",
  maxTokens: 1024,
  tools: tools,
  messages: [{ role: "user", content: "北京今天天气怎么样?" }],
});

console.log(message.content);
  1. 使用时需要将 ANTHROPIC_BASE_URL 设置为 https://api.calgine.com.cn/anthropic
  2. ANTHROPIC_API_KEY 应设置为您的 API Key

Gemini AI Studio 兼容接口 ​

Gemini 模型支持通过 Google AI Studio 官方 API 格式直接调用,无需转换为 OpenAI 格式。适用于已使用 Google genai SDK 的项目。如部分功能本文档未提及,请参考 Google AI Studio 官方文档。

基础对话 ​

python
from google import genai
from google.genai import types

client = genai.Client(
    api_key="API_KEY",  # 替换为您在控制台的 API Key
    http_options=types.HttpOptions(
        api_version="v1beta",
        base_url="https://api.calgine.com.cn",
    ),
)

response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="你好,请做一下自我介绍",
)

print(response.text)
js
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "API_KEY", // 替换为您在控制台的 API Key
  httpOptions: {
    apiVersion: "v1beta",
    baseUrl: "https://api.calgine.com.cn",
  },
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-pro",
  contents: "你好,请做一下自我介绍",
});

console.log(response.text);

流式响应 ​

python
from google import genai
from google.genai import types

client = genai.Client(
    api_key="API_KEY",
    http_options=types.HttpOptions(
        api_version="v1beta",
        base_url="https://api.calgine.com.cn",
    ),
)

for chunk in client.models.generate_content_stream(
    model="gemini-2.5-pro",
    contents="写一首关于春天的诗",
):
    print(chunk.text, end="", flush=True)
js
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "API_KEY",
  httpOptions: {
    apiVersion: "v1beta",
    baseUrl: "https://api.calgine.com.cn",
  },
});

const response = await ai.models.generateContentStream({
  model: "gemini-2.5-pro",
  contents: "写一首关于春天的诗",
});

for await (const chunk of response) {
  process.stdout.write(chunk.text());
}

工具调用(Function Calling) ​

更多详情参考 Google AI Studio 函数调用指南。

python
from google import genai
from google.genai import types

client = genai.Client(
    api_key="API_KEY",
    http_options=types.HttpOptions(
        api_version="v1beta",
        base_url="https://api.calgine.com.cn",
    ),
)

get_weather = types.FunctionDeclaration(
    name="get_weather",
    description="获取指定城市的天气信息",
    parameters=types.Schema(
        type="OBJECT",
        properties={"city": types.Schema(type="STRING", description="城市名称")},
        required=["city"],
    ),
)

response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="北京今天天气怎么样?",
    config=types.GenerateContentConfig(
        tools=[types.Tool(function_declarations=[get_weather])]
    ),
)

print(response.candidates[0].content.parts)
js
import { GoogleGenAI, Type } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "API_KEY",
  httpOptions: {
    apiVersion: "v1beta",
    baseUrl: "https://api.calgine.com.cn",
  },
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-pro",
  contents: "北京今天天气怎么样?",
  config: {
    tools: [
      {
        functionDeclarations: [
          {
            name: "get_weather",
            description: "获取指定城市的天气信息",
            parameters: {
              type: Type.OBJECT,
              properties: { city: { type: Type.STRING, description: "城市名称" } },
              required: ["city"],
            },
          },
        ],
      },
    ],
  },
});

console.log(response.candidates[0].content.parts);
  1. base_url / baseUrl 设置为 https://api.calgine.com.cn(不含 /v1beta 后缀,SDK 会自动拼接)
  2. api_version / apiVersion 建议设为 v1beta
  3. api_key / apiKey 应设置为您的 API Key
  4. 依赖安装:Python 使用 pip install google-genai,Node.js 使用 npm install @google/genai

相关链接 ​