一步API中文文档
  1. Java
一步API中文文档
  • 01_人工客服
  • 02_一步API福利群
  • 03_购买地址
  • 04_base_url说明
  • 05_API key的获取和使用
  • 06_支持的模型与在线查询
  • 07_API Key余额查询
  • 08_常用工具配置教程
    • 01 Chatbox 配置一步API
    • 02 PyCharm 配置一步API
    • 03 Cursor 配置一步API
    • 04 Trae AI 配置一步API
    • 05 CherryStudio 配置一步API
    • 06 Dify 配置一步API
    • 07 AingDesk 配置一步API
    • 08 VS Code 配置一步API
    • 09 IntelliJ IDEA 配置一步API
    • 10 immersivetranslate 沉浸式翻译配置一步API
    • 11 Zed 配置一步API
    • 12 DeepChat 配置一步API
    • 13 Void 配置一步API
    • 14 LibreChat 配置一步API
    • 15 Sider 配置一步API
    • 16 NextChat 配置一步API
    • 17 ChatWise 配置一步API
    • 18 Glarity 配置一步API
    • 19 Tavo 配置一步API
    • 20 OMate Chat 配置一步API
    • 21 Claude Code 配置一步API
    • 22 91写作配置
  • 09_示例代码
    • python
      • 01 OpenAI-Python示例代码
      • 02 Claude-Python示例代码
      • 03 OpenAI-image-Python示例代码
      • 04 Gemini-Python示例代码
      • 05 Gemini多场景-Python代码示例
      • 06 Rerank-python示例代码
      • 07 Python分析文件代码示例
      • 08 whisper-1-Python实力代码
      • 09 dalle-3-Python示例代码
      • 10 doubao-Python示例代码
      • 11 gemini-image-Python示例代码
      • 12 gpt-image-1-Python示例代码
      • 13 sora-2-Python代码示例
      • 14_Pro/BAAI/bge-reranker-v2-m3-Python示例代码
      • 15 Python其他示例
    • Java
      • 01 OpenAI-Java示例代码
      • 02 Claude-Java示例代码
      • 03 Gemini多场景-Java代码示例
  • 聊天接口(Chat)
    • 图片识别接口
    • 聊天接口(通用)
  • 向量生成(Embeddings)
    • 创建嵌入
  • 文生图片(Images)
    • DALL·E
  • 音频(Audio)
    • TTS文本转语音
    • ASR语音转文本
  • 更新中
    • API的介绍及使用教程点击内涵链接
    • 常用教程合集
    • Anthropic Claude接口
    • Claude账号登录教程
  • 接口
    • Anthropic 对话格式(Messages)
    • Cohere 重排序格式(Rerank)
    • Deepseek reasoning 对话格式(类Chat Completions)
    • Jina AI 重排序格式(Rerank)
    • Midjourney 图像格式(Midjourney Proxy/Midjourney Proxy Plus)
    • OpenAI 音频格式
    • OpenAI 对话格式(Chat Completions)
    • OpenAI 嵌入格式(Embeddings)
    • OpenAI 图像格式(Image)
    • OpenAI 实时对话接口
    • OpenAI 响应格式(Responses)
    • Suno 音乐格式(Music)
    • Xinference 重排序格式(Rerank)
  1. Java

03 Gemini多场景-Java代码示例

1. 更多教程及资源请访问一步AI官方社区站#

社区站:https://www.yiboot.com
一键直达一步AI官方社区
长按识别下方二维码立即访问

2. 资源准备#

API Key:此项配置填写在一步API官网创建API令牌,一键直达API令牌创建页面
创建API令牌步骤请参考API Key的获取和使用
API Host:此项配置填写https://yibuapi.com/v1
查看支持的模型请参在这里复制模型在线查询
如使用 yibuapi.com 中转:将 base_url 改为 https://yibuapi.com,并使用你在 yibuapi 控制台创建的 API Key。

OpenAI 兼容性:用 OpenAI SDK 访问 Gemini(Markdown 版)#

本文示例展示如何直接使用 OpenAI 官方 SDK(Python/JS/REST 兼容风格)调用 Gemini 模型,只需改动三行配置,即可在不改业务代码的前提下完成迁移与共存。

三行改动(Only 3 lines)#

1.
API Key
apiKey="sk-***"(JS)或 api_key="sk-***"(Python)
将 "sk-***" 替换为你在 Google AI Studio 创建的实际密钥。
2.
Base URL
baseURL="https://yibuapi.com"
3.
Model
model="gemini-2.0-flash"(或其他兼容 Gemini 模型)

快速上手(JavaScript / TypeScript)#

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: "sk-***",
  baseURL: "https://yibuapi.com"
});

const response = await openai.chat.completions.create({
  model: "gemini-2.0-flash",
  messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Explain to me how AI works" }
  ],
});

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

思考(Reasoning)与思考预算(Thinking Budget)#

OpenAI SDK 兼容的 Gemini 2.5 系列支持推理控制:
reasoning_effort: "low" | "medium" | "high" | "none"
extra_body.google.thinking_config.thinking_budget: 指定精确思考 token 预算
二者不能同时使用。

流式输出(Streaming)#

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: "sk-***",
  baseURL: "https://yibuapi.com"
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: "gemini-2.0-flash",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: "Hello!" }
    ],
    stream: true,
  });

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

函数调用(Function Calling)#

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: "sk-***",
  baseURL: "https://yibuapi.com"
});

async function main() {
  const messages = [{ role: "user", content: "What's the weather like in Chicago today?" }];

  const tools = [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "Get the weather in a given location",
        parameters: {
          type: "object",
          properties: {
            location: { type: "string" },
            unit: { type: "string", enum: ["celsius", "fahrenheit"] }
          },
          required: ["location"]
        }
      }
    }
  ];

  const resp = await openai.chat.completions.create({
    model: "gemini-2.0-flash",
    messages,
    tools,
    tool_choice: "auto",
  });

  console.log(resp);
}
main();

图片理解(Vision)#

import OpenAI from "openai";
import fs from "fs/promises";

const openai = new OpenAI({
  apiKey: "sk-***",
  baseURL: "https://yibuapi.com"
});

async function encodeImage(imagePath: string) {
  const buf = await fs.readFile(imagePath);
  return buf.toString("base64");
}

async function main() {
  const base64Image = await encodeImage("Path/to/agi/image.jpeg");

  const messages = [
    {
      role: "user",
      content: [
        { type: "text", text: "What is in this image?" },
        { type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64Image}` } }
      ]
    }
  ];

  const resp = await openai.chat.completions.create({
    model: "gemini-2.0-flash",
    messages
  });

  console.log(resp.choices[0]);
}
main();

Embeddings(文本向量)#

import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: "sk-***",
  baseURL: "https://yibuapi.com"
});

async function main() {
  const embedding = await openai.embeddings.create({
    model: "gemini-embedding-001",
    input: "Your text string goes here"
  });

  console.log(embedding);
}
main();

模型管理#

const openai = new OpenAI({
  apiKey: "sk-***",
  baseURL: "https://yibuapi.com"
});

const list = await openai.models.list();
for await (const model of list) console.log(model);

本文档适用于 yibuapi.com 平台,用于展示 Gemini 与 OpenAI SDK 的完全兼容调用方式。

联系客服#

如果显示添加频繁请手动搜索微信号进行添加:
点击复制微信号:xuexiv5876
修改于 2025-10-20 01:19:56
上一页
02 Claude-Java示例代码
下一页
图片识别接口
Built with