← 返回
开发者工具 Key

baidu-aistudio-llm-api

百度「AI Studio 星河社区」大模型 API 调用助手,帮助开发者快速接入大模型 API 服务,调用文心(ERNIE)、DeepSeek 等大模型能力。该服务依托百度智能云千帆平台,兼容 OpenAI Python SDK,可直接使用原生 openai-python SDK 接入开发,同时支持模型调用、使用...
百度「AI Studio 星河社区」大模型 API 调用助手,帮助开发者快速接入大模型 API 服务,调用文心(ERNIE)、DeepSeek 等大模型能力。该服务依托百度智能云千帆平台,兼容 OpenAI Python SDK,可直接使用原生 openai-python SDK 接入开发,同时支持模型调用、使用...
lemonteeeeaa
开发者工具 clawhub v1.0.3 1 版本 100000 Key: 需要
★ 0
Stars
📥 667
下载
💾 65
安装
1
版本
#latest

概述

baidu-aistudio-llm-api / 百度 AI Studio 星河社区大模型API调用

Official Skill from Baidu 「AI Studio」: Baidu AI Studio LLM API Access

概述

星河大模型API是为开发者提供的一套基础大模型服务,背靠百度智能云千帆平台,提供文心(ERNIE)、DeepSeek等大模型能力。该服务完全兼容OpenAI Python SDK,开发者可直接使用原生openai-python SDK调用。

福利:注册即送100万免费Tokens额度!

The Baidu 「AI Studio」 LLM API Assistant helps developers quickly connect to large model API services and access capabilities from models such as ERNIE and DeepSeek. Powered by the Baidu Intelligent Cloud Qianfan Platform, this service is compatible with the OpenAI Python SDK, allowing developers to integrate directly using the native openai-python SDK. It also provides guidance on model invocation, integration workflows, and related usage questions.✨ Bonus: Sign up and receive 1 million free tokens!

快速开始

Step 1: 获取API Key

检查用户是否已设置环境变量 AI_STUDIO_API_KEY

# 检查环境变量
echo $AI_STUDIO_API_KEY

如果没有设置,提醒用户:

> 请访问 https://aistudio.baidu.com/account/accessToken 获取您的访问令牌(Access Token),然后设置环境变量:

>

> ```bash

> # macOS/Linux

> export AI_STUDIO_API_KEY="您的访问令牌"

>

> # Windows PowerShell

> $env:AI_STUDIO_API_KEY="您的访问令牌"

>

> # 或使用 .env 文件

> echo 'AI_STUDIO_API_KEY="您的访问令牌"' >> .env

> ```

Step 2: 安装依赖

pip install openai

Step 3: 基本调用

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("AI_STUDIO_API_KEY"),
    base_url="https://aistudio.baidu.com/llm/lmapi/v3",
)

response = client.chat.completions.create(
    model="ernie-5.0-thinking-preview",
    messages=[
        {"role": "system", "content": "你是一个有帮助的AI助手。"},
        {"role": "user", "content": "你好!"}
    ]
)

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

模型选择指南

根据用户需求推荐合适的模型:

使用场景推荐模型说明
------------------------
深度思考ernie-5.0-thinking-preview最新ERNIE 5.0,思维链推理
代码生成deepseek-v3 / qwen3-coder代码能力强
复杂推理deepseek-r1思维链推理,复杂问题
通用对话kimi-k2-instructMoonshot开源模型
长文本处理ernie-4.5-turbo-128k-preview128K上下文
多模态ernie-4.5-turbo-vl支持图像、视频理解
快速响应ernie-speed-8k速度快

完整模型列表请查看: references/models.md

常用功能示例

流式输出

completion = client.chat.completions.create(
    model="ernie-5.0-thinking-preview",
    messages=[{"role": "user", "content": "讲个故事"}],
    stream=True,
)

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

多轮对话

messages = [
    {"role": "system", "content": "你是AI助手。"}
]

# 添加用户消息
messages.append({"role": "user", "content": "你好"})

# 获取回复
response = client.chat.completions.create(
    model="kimi-k2-instruct",
    messages=messages
)

# 保存到历史
messages.append({
    "role": "assistant",
    "content": response.choices[0].message.content
})

联网搜索(搜索增强)

completion = client.chat.completions.create(
    model="ernie-5.0-thinking-preview",
    messages=[{"role": "user", "content": "今天北京天气"}],
    extra_body={
        "web_search": {
            "enable": True,
            "enable_trace": True
        }
    }
)

支持联网搜索的模型: ernie-5.0, ernie-4.5, deepseek-r1, deepseek-v3

Function Calling

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

response = client.chat.completions.create(
    model="deepseek-v3",
    messages=[{"role": "user", "content": "北京今天天气"}],
    tools=tools,
    tool_choice="auto"
)

支持Function Call的模型: ernie-5.0-thinking-preview, ernie-x1-turbo, deepseek-r1, deepseek-v3

结构化输出(JSON)

response = client.chat.completions.create(
    model="ernie-4.5-turbo-128k-preview",
    messages=[{"role": "user", "content": "列出三个水果"}],
    response_format={"type": "json_object"}
)

深度思考模型(思维链)

response = client.chat.completions.create(
    model="deepseek-r1",
    messages=[{"role": "user", "content": "请解释相对论"}]
)

# 思维链内容
print(response.choices[0].message.reasoning_content)
# 最终回答
print(response.choices[0].message.content)

多模态(图像理解)

response = client.chat.completions.create(
    model="ernie-4.5-turbo-vl",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "描述这张图片"},
            {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
        ]
    }]
)

文生图

images = client.images.generate(
    prompt="一只可爱的白猫,戴着红帽子",
    model="Stable-Diffusion-XL",
    response_format="url"
)
print(images.data[0].url)

文本向量化

embeddings = client.embeddings.create(
    model="embedding-v1",
    input=["你好世界", "机器学习"]
)
print(embeddings.data[0].embedding)

高级功能

详细的API参数说明和更多功能,请查看:

常见错误处理

错误码说明解决方案
----------------------
401认证失败检查API Key是否正确
429请求限流降低请求频率或等待
400参数错误检查messages格式和参数
403内容安全修改输入内容

官方资源

Resources

scripts/

  • test_connection.py - 测试API连接
  • list_models.py - 查询可用模型列表

references/

  • models.md - 完整模型列表和能力说明
  • api_params.md - API参数详细说明

版本历史

共 1 个版本

  • v1.0.3 当前
    2026-03-19 06:45 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Agent Browser

matrixy
专为AI智能体优化的无头浏览器自动化CLI,支持无障碍树快照和基于引用的元素选择。
★ 427 📥 118,202
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 68 📥 180,176
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 668 📥 324,158