AI大模型教程
一起来学习

Springboot+vue调用文心一言接口

     最近接到一客户需求,希望在系统中加入AI机器人,用户提问然后系统根据要求流式输出内容。我根据市面上流行的大模型进行了技术调研,最后选用了百度文心一言

     官方网址:服务与支持,提供了技术文档、示例代码和测试等内容。

功能还是很全的,模型种类也很多,之前做得智能识别也是调用了百度的接口,所以使用起来还是很顺利的。

下面是完成的效果:

由于传不了视频,只能截图了,输出效果是流式的,就像打字一样一个一个输出的。

部分核心代码:

  methods: {
            sendMessage() {
                if (this.newMessage.trim()) {
                    this.messages.push({
                        id: Date.now(),
                        user: '你:',
                        content: this.newMessage
                    });
                    this.newMessage = '';

                    //AI回复
                    axios.post('/wenxin/query', this.messages).then(res => {
                        this.str = res.data.result
                        this.getAnswer()
                    });

                }
            },
            async getAnswer() {
                // ai回答
                this.messages.push({
                    id: Date.now(),
                    user: 'AI:',
                    content: ''
                });
                // 流式输出文字逐个展示
              .........
            },

java代码

@RestController
@RequestMapping(value = "/wenxin")
public class WenxinController {

    @Autowired
    private WenxinService wenxinService;

    @PostMapping("/query")
    public ResponseEntity queryWenxin(@RequestBody String question) throws IOException {
        String response = wenxinService.queryWenxin(question);
        return ResponseEntity.ok(response);
    }
}
package com.example.service;

import com.alibaba.fastjson.JSON;
import com.example.common.HttpRequest;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import okhttp3.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

@Service
public class WenxinService {

    public static final String API_KEY = "";
    public static final String SECRET_KEY = "";

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public String queryWenxin(String question) throws IOException {

        //访问数据
        String requestMethod = "POST";
        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-4.0-8k-latest?access_token="+getAccessToken();//post请求时格式

        HashMap msg = new HashMap();
        msg.put("role","user");
        msg.put("content", question);
        ArrayList messages = new ArrayList();
        messages.add(msg);
        HashMap requestBody = new HashMap();
        requestBody.put("messages", messages);
        String outputStr = JSON.toJSONString(requestBody);
        JSON json = HttpRequest.httpRequest(url,requestMethod,outputStr,"application/json");
        return json.toString();
    }

    /**
     * 从用户的AK,SK生成鉴权签名(Access Token)
     * @return 鉴权签名(Access Token)
     * @throws IOException IO异常
     */
    static String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        return new JSONObject(response.body().string()).getString("access_token");
    }

}

最后提醒一下,要提前申请好key

v:p_k1816

文章来源于互联网:Springboot+vue调用文心一言接口

相关推荐: 快速上手文心一言指令:解锁智能对话的新纪元

这是老K的第6篇原创 导语:         人工智能(AI)已经悄然改变了我们的生活。从智能家居到自动驾驶,再到如今的智能助手,AI技术无处不在,让我们的生活变得更加便捷和高效。然而,对于许多人来说,AI技术似乎仍然遥不可及,高深莫测。今天,就让我们一起揭开…

赞(0)
未经允许不得转载:5bei.cn大模型教程网 » Springboot+vue调用文心一言接口
分享到: 更多 (0)

AI大模型,我们的未来

小欢软考联系我们