• 百度千帆大模型文心一言api调用


    demo工程(csdn上传总是报错461, 只好使用百度网盘了)
    链接:https://pan.baidu.com/s/1EXbQDBMMNh1pyMIKwCmnow?pwd=7891
    提取码:7891

    注册百度智能云账号并申请文心千帆大模型资格

    https://login.bce.baidu.com/
    https://cloud.baidu.com/product/wenxinworkshop

    创建应用用于获取access_token

    在这里插入图片描述
    在这里插入图片描述
    创建应用成功后,可以获取到API Key和Secret Key

    获取access_token

    curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】'
    
    • 1

    在这里插入图片描述

    开通付费服务

    在这里插入图片描述
    api调用是按token(字数)收费的,不开通收费无法使用。

    发送对话请求

    curl -XPOST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=[步骤一调用接口获取的access_token]' -d '{
       "messages": [
        {"role":"user","content":"介绍一下你自己"}
       ]
    }' 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    Java发送http方式调用

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.gson.Gson;
    
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    
    /**
     * api文档 https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11
     *
     */
    public class BaiduWenXinChat {
    
    	static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().connectTimeout(5, TimeUnit.SECONDS) // 连接超时设置为20秒
    			.writeTimeout(15, TimeUnit.SECONDS) // 写入超时设置为30秒
    			.readTimeout(20, TimeUnit.SECONDS) // 读取超时设置为30秒
    			.build();
    	static Gson gson = new Gson();
    
    	// 历史对话信息
    	Map<String, List<ChatMsg>> mapChatList = new HashMap<String, List<ChatMsg>>();
    
    	public static void main(String[] args) throws Exception {
    
    		/*  获取acessToken:
    		curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API Key】&client_secret=【Secret Key】'
    		*/
    		String accessToken = "24.621fe77e232121321213213213213213c6b";
    		String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token="
    				+ accessToken;
    		String msg = "介绍下你自己";
    
    		// 结合prompt增强的当前待发送信息
    		ChatMsg chatMsg = new ChatMsg();
    		chatMsg.setRole("user");
    		chatMsg.setContent(msg);
    
    		// 当前发送消息数组
    		List<ChatMsg> messages = new ArrayList<ChatMsg>();
    		messages.add(chatMsg);
    		String messagesJson = gson.toJson(messages);
    		String content = "{\"messages\":" + messagesJson + "}";
    
    		long start = System.currentTimeMillis();
    
    		MediaType mediaType = MediaType.parse("application/json");
    		RequestBody body = RequestBody.create(mediaType, content);
    
    		System.out.println(content);
    		Request request = new Request.Builder().url(url).method("POST", body)
    				.addHeader("Content-Type", "application/json").build();
    
    		Response response = HTTP_CLIENT.newCall(request).execute();
    		String responseText = response.body().string();
    		System.out.println("response返回: \n" + responseText);
    
    		long end = System.currentTimeMillis();
    		System.out.println("该回答花费时间为:" + (end - start) / 1000.0 + "秒");
    
    		ObjectMapper objectMapper = new ObjectMapper();
    		JsonNode rootNode = objectMapper.readTree(responseText);
    		String answer = rootNode.get("result").asText();
    		System.out.println(answer);
    	}
    
    }
    
    class ChatMsg {
    
    	private String role;
    	private String content;
    
    	public String getRole() {
    		return role;
    	}
    
    	public void setRole(String role) {
    		this.role = role;
    	}
    
    	public String getContent() {
    		return content;
    	}
    
    	public void setContent(String content) {
    		this.content = content;
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    demo工程(csdn上传总是报错461, 只好使用百度网盘了)
    链接:https://pan.baidu.com/s/1EXbQDBMMNh1pyMIKwCmnow?pwd=7891
    提取码:7891

    参考

    https://blog.csdn.net/qq_30299877/article/details/131917097
    https://cloud.baidu.com/doc/WENXINWORKSHOP/s/jlil56u11
    https://cloud.baidu.com/qianfandev/topic/267178
    https://blog.csdn.net/shy_snow/article/details/132759686

  • 相关阅读:
    基于java网上服装店设计与实现(mvc+jsp+servlet+jdbc+MySQL)
    springboot-自动装配原理
    RabbitMQ简介(一)
    园子周边第2季:黑色大鼠标垫已上架,大气简洁与五彩缤纷的融合
    CSS---关于font文本属性设置样式总结
    MybatisPlus高级特性
    nginx连接前后端分离项目 或 负载均衡映射多个服务器
    信息学奥赛一本通(c++):1839:【05NOIP提高组】谁拿了最多奖学金
    [附源码]计算机毕业设计ssm校园二手交易平台
    【前端】Vue+Element UI案例:通用后台管理系统-登陆页面Login
  • 原文地址:https://blog.csdn.net/shy_snow/article/details/132759686