Skip to content

国产大模型API

概述

国产大模型近年来发展迅速,在中文理解和生成方面具有独特优势。本章将介绍主流国产大模型API的使用方法,包括通义千问、文心一言、智谱GLM、DeepSeek等,帮助你选择合适的国产模型。

通义千问(阿里云)

通义千问是阿里云推出的大语言模型,中文能力强,支持多模态。

环境准备

bash
pip install dashscope

基础调用

python
from dashscope import Generation

response = Generation.call(
    model='qwen-plus',
    messages=[
        {'role': 'system', 'content': '你是一个有帮助的助手。'},
        {'role': 'user', 'content': '介绍一下Python的特点'}
    ]
)

print(response.output.choices[0].message.content)

流式输出

python
responses = Generation.call(
    model='qwen-plus',
    messages=[
        {'role': 'user', 'content': '写一首关于春天的诗'}
    ],
    stream=True,
    incremental_output=True
)

for response in responses:
    if response.output.choices[0].message.content:
        print(response.output.choices[0].message.content, end='', flush=True)

参数配置

python
response = Generation.call(
    model='qwen-max',
    messages=[
        {'role': 'user', 'content': '解释机器学习'}
    ],
    temperature=0.8,
    top_p=0.9,
    max_tokens=2000,
    stop=['结束', 'END']
)

多模态能力

python
from dashscope import MultiModalConversation

messages = [
    {
        "role": "user",
        "content": [
            {"image": "https://example.com/image.jpg"},
            {"text": "这张图片里有什么?"}
        ]
    }
]

response = MultiModalConversation.call(
    model='qwen-vl-plus',
    messages=messages
)

print(response.output.choices[0].message.content)

Function Calling

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

response = Generation.call(
    model='qwen-plus',
    messages=[
        {'role': 'user', 'content': '北京今天天气怎么样?'}
    ],
    tools=tools
)

if response.output.choices[0].message.tool_calls:
    tool_call = response.output.choices[0].message.tool_calls[0]
    print(f"调用函数: {tool_call['function']['name']}")
    print(f"参数: {tool_call['function']['arguments']}")

模型列表

模型上下文窗口特点
qwen-max32K最强能力
qwen-plus128K平衡性能
qwen-turbo8K快速响应
qwen-vl-plus8K多模态

文心一言(百度)

文心一言是百度推出的大语言模型,知识图谱能力强。

环境准备

bash
pip install qianfan

基础调用

python
import qianfan

chat_completion = qianfan.ChatCompletion()

response = chat_completion.do(
    model="ERNIE-Bot-4",
    messages=[
        {"role": "user", "content": "介绍一下深度学习"}
    ]
)

print(response.body['result'])

流式输出

python
response = chat_completion.do(
    model="ERNIE-Bot-4",
    messages=[
        {"role": "user", "content": "写一个Python爬虫示例"}
    ],
    stream=True
)

for r in response:
    print(r.body['result'], end='', flush=True)

参数配置

python
response = chat_completion.do(
    model="ERNIE-Bot-4",
    messages=[
        {"role": "user", "content": "生成创意文案"}
    ],
    temperature=0.9,
    top_p=0.8,
    max_output_tokens=2000
)

Embedding接口

python
from qianfan import Embedding

emb = Embedding()

response = emb.do(
    model="Embedding-V1",
    texts=["你好世界", "机器学习"]
)

for embedding in response.body['data']:
    print(f"向量维度: {len(embedding['embedding'])}")

模型列表

模型说明
ERNIE-Bot-4最新版本,能力最强
ERNIE-Bot-turbo快速响应
ERNIE-Bot标准版本

智谱GLM

智谱GLM是清华技术背景的大模型,代码能力强。

环境准备

bash
pip install zhipuai

基础调用

python
from zhipuai import ZhipuAI

client = ZhipuAI(api_key="your-api-key")

response = client.chat.completions.create(
    model="glm-4",
    messages=[
        {"role": "user", "content": "解释一下Transformer架构"}
    ]
)

print(response.choices[0].message.content)

流式输出

python
response = client.chat.completions.create(
    model="glm-4",
    messages=[
        {"role": "user", "content": "写一个快速排序算法"}
    ],
    stream=True
)

for chunk in response:
    print(chunk.choices[0].delta.content, end="", flush=True)

Function Calling

python
tools = [
    {
        "type": "function",
        "function": {
            "name": "query_database",
            "description": "查询数据库",
            "parameters": {
                "type": "object",
                "properties": {
                    "sql": {
                        "type": "string",
                        "description": "SQL查询语句"
                    }
                },
                "required": ["sql"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="glm-4",
    messages=[
        {"role": "user", "content": "查询所有用户信息"}
    ],
    tools=tools
)

print(response.choices[0].message.tool_calls)

长文本处理

python
response = client.chat.completions.create(
    model="glm-4-long",
    messages=[
        {
            "role": "user",
            "content": f"分析以下文档:\n\n{long_document}"
        }
    ]
)

Embedding接口

python
response = client.embeddings.create(
    model="embedding-2",
    input=["文本1", "文本2"]
)

for item in response.data:
    print(f"向量维度: {len(item.embedding)}")

模型列表

模型上下文窗口特点
glm-4128K最新版本
glm-4-long1M超长上下文
glm-4-flash128K快速响应

DeepSeek

DeepSeek是深度求索公司的大模型,性价比高,代码能力强。

环境准备

bash
pip install openai

基础调用

python
from openai import OpenAI

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

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "user", "content": "实现一个Python装饰器"}
    ]
)

print(response.choices[0].message.content)

流式输出

python
stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "user", "content": "写一个Web服务器示例"}
    ],
    stream=True
)

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

代码专用模型

python
response = client.chat.completions.create(
    model="deepseek-coder",
    messages=[
        {
            "role": "system",
            "content": "你是一个专业的程序员助手。"
        },
        {
            "role": "user",
            "content": "优化这段代码的性能"
        }
    ]
)

模型列表

模型特点价格
deepseek-chat通用对话
deepseek-coder代码专用

百川大模型

百川是百川智能推出的大模型。

基础调用

python
from openai import OpenAI

client = OpenAI(
    api_key="your-api-key",
    base_url="https://api.baichuan-ai.com/v1"
)

response = client.chat.completions.create(
    model="Baichuan2-Turbo",
    messages=[
        {"role": "user", "content": "介绍一下你自己"}
    ]
)

print(response.choices[0].message.content)

讯飞星火

讯飞星火是科大讯飞推出的大模型。

基础调用

python
import sparkai

client = sparkai.Client(
    app_id="your-app-id",
    api_key="your-api-key",
    api_secret="your-api-secret"
)

response = client.chat.completions.create(
    model="spark-v3.5",
    messages=[
        {"role": "user", "content": "你好"}
    ]
)

print(response.choices[0].message.content)

统一封装方案

为了方便切换不同模型,可以封装统一的接口。

统一接口设计

python
from abc import ABC, abstractmethod
from typing import List, Dict, Optional

class BaseLLM(ABC):
    @abstractmethod
    def chat(self, messages: List[Dict], **kwargs) -> str:
        pass
    
    @abstractmethod
    def stream_chat(self, messages: List[Dict], **kwargs):
        pass

class QwenLLM(BaseLLM):
    def __init__(self, api_key: str):
        from dashscope import Generation
        self.client = Generation
        self.api_key = api_key
    
    def chat(self, messages: List[Dict], **kwargs) -> str:
        response = self.client.call(
            model='qwen-plus',
            messages=messages,
            **kwargs
        )
        return response.output.choices[0].message.content
    
    def stream_chat(self, messages: List[Dict], **kwargs):
        responses = self.client.call(
            model='qwen-plus',
            messages=messages,
            stream=True,
            incremental_output=True,
            **kwargs
        )
        for response in responses:
            if response.output.choices[0].message.content:
                yield response.output.choices[0].message.content

class DeepSeekLLM(BaseLLM):
    def __init__(self, api_key: str):
        from openai import OpenAI
        self.client = OpenAI(
            api_key=api_key,
            base_url="https://api.deepseek.com/v1"
        )
    
    def chat(self, messages: List[Dict], **kwargs) -> str:
        response = self.client.chat.completions.create(
            model="deepseek-chat",
            messages=messages,
            **kwargs
        )
        return response.choices[0].message.content
    
    def stream_chat(self, messages: List[Dict], **kwargs):
        stream = self.client.chat.completions.create(
            model="deepseek-chat",
            messages=messages,
            stream=True,
            **kwargs
        )
        for chunk in stream:
            if chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content

class GLMLLM(BaseLLM):
    def __init__(self, api_key: str):
        from zhipuai import ZhipuAI
        self.client = ZhipuAI(api_key=api_key)
    
    def chat(self, messages: List[Dict], **kwargs) -> str:
        response = self.client.chat.completions.create(
            model="glm-4",
            messages=messages,
            **kwargs
        )
        return response.choices[0].message.content
    
    def stream_chat(self, messages: List[Dict], **kwargs):
        response = self.client.chat.completions.create(
            model="glm-4",
            messages=messages,
            stream=True,
            **kwargs
        )
        for chunk in response:
            if chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content

工厂模式

python
class LLMFactory:
    @staticmethod
    def create_llm(provider: str, api_key: str) -> BaseLLM:
        providers = {
            "qwen": QwenLLM,
            "deepseek": DeepSeekLLM,
            "glm": GLMLLM
        }
        
        if provider not in providers:
            raise ValueError(f"不支持的提供商: {provider}")
        
        return providers[provider](api_key)

llm = LLMFactory.create_llm("qwen", "your-api-key")
response = llm.chat([{"role": "user", "content": "你好"}])
print(response)

使用示例

python
def compare_models(question: str, providers: List[str]):
    results = {}
    
    for provider in providers:
        api_key = os.environ.get(f"{provider.upper()}_API_KEY")
        llm = LLMFactory.create_llm(provider, api_key)
        
        start_time = time.time()
        response = llm.chat([{"role": "user", "content": question}])
        elapsed = time.time() - start_time
        
        results[provider] = {
            "response": response,
            "time": elapsed
        }
    
    return results

results = compare_models(
    "解释量子计算",
    ["qwen", "deepseek", "glm"]
)

for provider, result in results.items():
    print(f"\n{provider}:")
    print(f"响应时间: {result['time']:.2f}秒")
    print(f"回答: {result['response'][:100]}...")

模型选择建议

按场景选择

python
场景配置 = {
    "代码生成": {
        "推荐": ["deepseek-coder", "glm-4"],
        "原因": "代码能力强,价格低"
    },
    "长文档处理": {
        "推荐": ["glm-4-long", "qwen-plus"],
        "原因": "支持长上下文"
    },
    "创意写作": {
        "推荐": ["qwen-max", "ERNIE-Bot-4"],
        "原因": "中文表达能力强"
    },
    "知识问答": {
        "推荐": ["ERNIE-Bot-4", "qwen-max"],
        "原因": "知识图谱能力强"
    },
    "成本敏感": {
        "推荐": ["deepseek-chat", "glm-4-flash"],
        "原因": "价格最低"
    }
}

性能对比

python
import time

def benchmark_models():
    models = {
        "qwen": QwenLLM(os.environ["QWEN_API_KEY"]),
        "deepseek": DeepSeekLLM(os.environ["DEEPSEEK_API_KEY"]),
        "glm": GLMLLM(os.environ["GLM_API_KEY"])
    }
    
    test_cases = [
        "写一个Python函数",
        "解释机器学习",
        "翻译成英文:你好世界"
    ]
    
    for case in test_cases:
        print(f"\n测试用例: {case}")
        for name, model in models.items():
            start = time.time()
            response = model.chat([{"role": "user", "content": case}])
            elapsed = time.time() - start
            
            print(f"{name}: {elapsed:.2f}秒, {len(response)}字符")

benchmark_models()

成本对比

模型输入价格(元/千Token)输出价格(元/千Token)
qwen-max0.120.12
qwen-plus0.0080.008
ERNIE-Bot-40.120.12
glm-40.10.1
deepseek-chat0.0010.002

成本计算

python
def calculate_cost(model: str, input_tokens: int, output_tokens: int) -> float:
    prices = {
        "qwen-plus": {"input": 0.008, "output": 0.008},
        "glm-4": {"input": 0.1, "output": 0.1},
        "deepseek-chat": {"input": 0.001, "output": 0.002}
    }
    
    price = prices.get(model, prices["qwen-plus"])
    cost = (input_tokens * price["input"] + output_tokens * price["output"]) / 1000
    
    return cost

cost = calculate_cost("deepseek-chat", 10000, 2000)
print(f"预估成本: ¥{cost:.4f}")

小结

本章介绍了主流国产大模型API的使用方法:

  1. 通义千问 - 阿里云,中文能力强,支持多模态
  2. 文心一言 - 百度,知识图谱能力强
  3. 智谱GLM - 清华背景,代码能力强,支持超长上下文
  4. DeepSeek - 性价比高,代码能力强
  5. 统一封装 - 工厂模式实现模型切换

国产大模型在中文理解和生成方面具有优势,价格相对较低,适合国内应用场景。下一章将介绍API调用的最佳实践。

参考资源