当前位置: 首页 > article >正文

百度智能云—千帆 ModelBuilder API的简单调用(Java)

百度简介

百度(Baidu)是拥有强大互联网基础的领先AI公司。百度愿景是:成为最懂用户,并能帮助人们成长的全球顶级高科技公司。

“百度”二字,来自于八百年前南宋词人辛弃疾的一句词:众里寻他千百度。这句话描述了词人对理想的执着追求。1999年底,身在美国硅谷的李彦宏看到了中国互联网及中文搜索引擎服务的巨大发展潜力,抱着技术改变世界的梦想,他毅然辞掉硅谷的高薪工作,携搜索引擎专利技术,于2000年1月1日在中关村创建了百度公司。

百度拥有数万名研发工程师,这是中国乃至全球都顶尖的技术团队。这支队伍掌握着世界上领先的搜索引擎技术,使百度成为掌握世界尖端科学核心技术的中国高科技企业,也使中国成为美国、俄罗斯和韩国之外,全球仅有的4个拥有搜索引擎核心技术的国家之一。

该简介引自百度百科:百度(百度公司)_百度百科

简单实现千帆 ModelBuilder API的调用

1、注册百度云

进入百度智能云-云智一体深入产业官网完成注册

注意注册完成后一定要完成个人认证,不然无法使用

2、获取API接口

  • 完成注册后点击立即体验即可进入千帆大模型平台
  • 进入应用接入页面后点击创建应用
  • 输入该应用名称和描述后,点击确定即可完成创建
  • 可以看到这里就会有我刚创建完毕的应用,我们就可以在我们的程序中通过API key和Secret Key来调用该应用
  • 具体使用方式可见推理服务API介绍 - ModelBuilder官方API使用文档

3、简单的案例实现

  • 在pom.xml文件中导入下列依赖
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.1</version> <!-- 请检查最新版本 -->
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.8</version> <!-- 请检查最新版本 -->
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20231013</version>
        </dependency>
  •  创建一个简单的案例
import okhttp3.*;
import java.io.IOException;
import java.util.Scanner;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;

public class BaiduAIChat {

    private static final OkHttpClient client = new OkHttpClient();

    public static String getAccessToken() throws IOException {
        //格式为https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=API Key&client_secret=Secret Key
        //注意这里更换为自己的API Key和Secret Key
        String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=DJufZMEjworMMvTcCgukat4z&client_secret=LlEz19TXicOg5xxuwnjHS46IShdHtZ2W";

        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(null, new byte[0]))
                .build();

        try (Response response = client.newCall(request).execute()) {
            String responseBody = response.body().string();
            JsonObject jsonObject = new Gson().fromJson(responseBody, JsonObject.class);
            return jsonObject.get("access_token").getAsString();
        }
    }

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入您的问题:");
        String userInput = scanner.nextLine();
        scanner.close();

        String accessToken = getAccessToken();
        //百度云api https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/自己需要使用的服务
        //服务名称可在应用详情页面查看
        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k?access_token=" + accessToken;

        JsonObject payload = new JsonObject();
        JsonArray messages = new JsonArray();
        JsonObject message = new JsonObject();
        message.addProperty("role", "user");
        message.addProperty("content", userInput);
        messages.add(message);
        payload.add("messages", messages);
        payload.addProperty("stream", false);
        payload.addProperty("temperature", 0.9);
        payload.addProperty("top_p", 0.7);
        payload.addProperty("penalty_score", 1);
        payload.addProperty("system", "高等数学老师");
        payload.addProperty("max_output_tokens", 4096);
        payload.addProperty("frequency_penalty", 0.1);
        payload.addProperty("presence_penalty", 0.0);

        RequestBody body = RequestBody.create(payload.toString(), MediaType.get("application/json; charset=utf-8"));

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            String responseBody = response.body().string();
            JsonObject responseJson = new Gson().fromJson(responseBody, JsonObject.class);
            if (responseJson.has("result")) {
                System.out.println(responseJson.get("result").getAsString());
            } else {
                System.out.println("没有返回结果或结果格式不正确。");
            }
        }
    }
}

具体如何选择API url中的服务可见API列表 - ModelBuilder官方介绍文档,根据其中功能介绍选择适合自己程序的api

4、使用结果示例

这里只是一个简单的demo,更加详细完善的功能可以自己去实现


http://www.kler.cn/a/552298.html

相关文章:

  • leetcode232-用栈实现队列
  • 百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
  • 【网络安全 | 漏洞挖掘】我如何通过Cookie Manipulation发现主域上的关键PII?
  • [OD E 100] 服务器广播需要广播的服务器数量
  • Hive Orc表数据导出和导入
  • Param ‘serviceName‘ is illegal, serviceName is blank
  • Rabbitmq的三个端口区分
  • python 的框架 dash 开发TodoList Web 应用
  • Spring Boot自动装配:约定大于配置的魔法解密
  • 组合模式详解(Java)
  • React 与 Vue 对比指南 - 上
  • 备战蓝桥杯 Day4 差分
  • 学习机器视觉halcon3D需要什么基础知识
  • 【工具类】 Hutool 中用于生成随机数的工具类
  • 【USRP】N210,X310,X410 如何优雅得接入 Vmware 虚拟机
  • Vue学习记录19
  • CPP集群聊天服务器开发实践(六):Redis发布订阅消息队列及服务器集群通信
  • JAVA过滤器(学习自用)
  • Vue.js 框架
  • 基于TCP与UDP协议的性能测试研究