【个人学习||agent底层】01创建基础的发送和模型建立联系

创建基础的发送

agent的模型一般大都由服务商提供,而agent功能是让本机和服务商的模型建立联系,因此第一步是简单的建立联系

本文以deepseek为例
https://api-docs.deepseek.com/zh-cn/

也就是说我们发送的http应该和他要求的一样

Maven依赖

jackson

maven 复制代码
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.18.2</version>
</dependency>

代码编写

思路

java代码需要转为http,发给服务商,再把服务商返回的转回java代码

需要ObjectMapper mapper,实现java和json的转换

需要HttpClient httpClient,实现http的收发

deepseek为例,官方文档要求定义key,mode,url

java 复制代码
public class DPAgent {

    public static final String API_KEY = "sk-";
    public static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    public static final String MODEL = "deepseek-chat";

    //jackson的类,将java代码转为json和把jso解析为java
    private static final ObjectMapper mapper = new ObjectMapper();
    //http发送器
    private static final HttpClient httpClient = HttpClient.newHttpClient();
    //字符串构建
    static final StringBuilder stringBu = new StringBuilder();
}

定义主函数,用来接受终端的用户输入

java 复制代码
    public static void main(String[] args) throws Exception {
        while (true) {
            Scanner input = new Scanner(System.in);
            System.out.print("我: ");
            String Prompt = input.nextLine();
            DPAgent.chat(Prompt);
        }
    }
java 复制代码
    //发送函数
    public static void chat(String Prompt) throws Exception {
        String input = """
                用户问题:%s
                规则:对话形式回答简洁明了步捏找数据
                """.formatted(Prompt);

        //放入字符串构建器
        stringBu.append(input);

        //用Map创建http请求
        Map<String, Object> body = new HashMap<>();
        body.put("model", MODEL);
        body.put("messages",
                new Object[]{
                        Map.of(
                                "role", "user",
                                "content", stringBu.toString())
                }
        );
        body.put("temperature", 0.1);


        //用 ObjectMapper  把Map 转成json格式的字符串
        String json = mapper.writeValueAsString(body);

        //创建Http请求对象
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(API_URL))
                .header("Authorization", "Bearer " + API_KEY)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(json))
                .build();

        // 用HttpClient 发送请求对象,并且得到响应对象
        HttpResponse<String> resp = httpClient.send(request,
                HttpResponse.BodyHandlers.ofString());

        //输出结果
        System.out.println(resp.body());

    }

总代码

java 复制代码
public class DPAgent {
    public static final String API_KEY = "sk-";
    public static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
    public static final String MODEL = "deepseek-chat";

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final HttpClient httpClient = HttpClient.newHttpClient();
    static final StringBuilder stringBu = new StringBuilder();

    public static void main(String[] args) throws Exception {
        while (true) {
            Scanner input = new Scanner(System.in);
            System.out.print("我: ");
            String Prompt = input.nextLine();
            DPAgent.chat(Prompt);
        }
    }

    public static void chat(String Prompt) throws Exception {
        String input = """
                用户问题:%s
                规则:对话形式回答简洁明了步捏找数据
                """.formatted(Prompt);

        stringBu.append(input);

        //创建http请求
        Map<String, Object> body = new HashMap<>();
        body.put("model", MODEL);
        body.put("messages",
                new Object[]{
                        Map.of(
                                "role", "user",
                                "content", stringBu.toString())
                }
        );
        body.put("temperature", 0.1);
        //用 ObjectMapper  把Map 转成json格式的字符串
        String json = mapper.writeValueAsString(body);
        //创建Http请求对象(和文档匹配)
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(API_URL))
                .header("Authorization", "Bearer " + API_KEY)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(json))
                .build();

        // 用HttpClient 发送请求对象,并且得到响应对象
        HttpResponse<String> resp = httpClient.send(request,
                HttpResponse.BodyHandlers.ofString());
        System.out.println(resp.body());

    }
}
    

最终结果

相关推荐
爱看大明王朝15661 小时前
磁件学习-磁性元器件的极限计算
笔记·学习
东风破1371 小时前
DM8达梦共享存储集群DSC搭建步骤
数据库·学习·dm达梦数据库
星幻元宇VR2 小时前
VR科普大空间:沉浸式公共教育新模式
科技·学习·安全·vr·虚拟现实
笨鸟先飞的橘猫4 小时前
MMO游戏中的“跨服团队副本”匹配与状态同步系统
分布式·学习·游戏·lua·skynet
雨落在了我的手上5 小时前
如何学习java?
java·开发语言·学习
吃好睡好便好6 小时前
汽车基本组成
学习·汽车
拾忆丶夜7 小时前
unity 热力图学习
学习·unity·游戏引擎
red_redemption8 小时前
自由学习记录(183)
学习·ue项目改名字的学问
lizhihai_998 小时前
股市学习心得-智能体顶层设计文件收益供应链
大数据·人工智能·学习
中草药z8 小时前
【测试基础】Python 核心语法,一篇搞定测试脚本开发基础
开发语言·笔记·python·学习·测试·语法