AI大模型教程
一起来学习

springboot 项目整合 AI (文心一言)

百度智能云网址:https://cloud.baidu.com/?from=console

注册——个人认证——登录成功

第一步:点击千帆大模型平台

第二步:点击应用接入——创建应用

第三步:点击接口文档——API列表——可以点击指定模型进行查看调用示例https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Nlks5zkzu

第四步:调用调试——百度智能云登录成功——点击个人中心——安全认证——点击显示输入验证码获取Access Key和Secret Key

第五步:引入依赖


        com.baidubceqianfan0.0.9

第六步:调用示例——单轮——多轮——流式

单轮:一个人提问题

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = HerbigApplicationTests.class)
class HerbigApplicationTests {

    @Test
    void singleWheel() {//单轮
        // 使用安全认证AK/SK鉴权参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
        Qianfan qianfan = new Qianfan("your_iam_ak" , "your_iam_sk" );
        //指定模型
        ChatResponse resp = qianfan.chatCompletion()
                .model("ERNIE-4.0-8K")
                .addMessage( "user","你好,你是谁?")
                .execute();
        System.out.println(resp.getResult());
    }
}

结果:

多轮:根据之前回复的历史数据结合最后一个问题 然后回答

@Test
    void multiWheel() {//多轮
        // 使用安全认证AK/SK鉴权参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
        Qianfan qianfan = new Qianfan("your_iam_ak" , "your_iam_sk" );
        // 多轮对话
        ChatResponse resp = qianfan.chatCompletion()
                .model("ERNIE-4.0-8K")
                .addMessage("user", "你好")
                .addMessage("assistant", "你好!请问有什么我可以帮助你的吗?")
                .addMessage("user", "我在济南,周末可以去哪里玩?")
                .addMessage("assistant", "济南是一个充满活力和文化氛围的城市,有很多适合周末游玩的地方让您深入了解中国和世界的文化历史。")
                .addMessage("user", "简单介绍下济南的美食")
                .execute();
        System.out.println(resp.getResult());
    }

结果:

流式:结果分段返回

@Test
    void flowType() {//流式
        // 使用安全认证AK/SK鉴权参数,安全认证Access Key替换your_iam_ak,Secret Key替换your_iam_sk
        Qianfan qianfan = new Qianfan("your_iam_ak" , "your_iam_sk" );

        Gson gson = new Gson();
        qianfan.chatCompletion()
                .model("ERNIE-4.0-8K")
                .addMessage("user", "简单介绍下趵突泉")
                // 启用流式返回
                .executeStream()
                .forEachRemaining(chunk -> System.out.print(gson.toJson(chunk)));
    }

文章来源于互联网:springboot 项目整合 AI (文心一言)

相关推荐: Stable Diffusion中的常用术语解析

Stable Diffusion中的常用术语解析 对于很多初学者来说,会对Stable Diffusion中的很多术语感到困惑,当然你不是唯一的那个。 在这篇文章中,我将会讲解几乎所有你在Stable Diffusion中需要了解的关键术语。搞懂了这些术语,使…

赞(0)
未经允许不得转载:5bei.cn大模型教程网 » springboot 项目整合 AI (文心一言)
分享到: 更多 (0)

AI大模型,我们的未来

小欢软考联系我们