【个人学习||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());

    }
}
    

最终结果

相关推荐
xian_wwq4 小时前
【学习笔记】模型权重管理:Safetensors与私有化Hub(23/35)
笔记·学习
其实防守也摸鱼4 小时前
运维--学习阶段问题解答(1)(自测)
linux·运维·服务器·数据库·学习·自动化·命令模式
ctrl_v助手4 小时前
Halcon学习笔记2
人工智能·笔记·学习
YUS云生5 小时前
Python学习笔记·第31天:FastAPI入门——路由、路径参数、查询参数与请求体
笔记·python·学习
铅笔侠_小龙虾5 小时前
Rust 学习目录
开发语言·学习·rust
渣渣灰飞7 小时前
MySQL 系统学习 第五阶段:企业级 MySQL 实战开发 第二章:RBAC 权限系统设计
android·学习·mysql
六bring个六7 小时前
open Harmony中分布式软总线的学习任务清单
分布式·学习·c/c++·open harmony
精神底层8 小时前
AI 学习笔记:研究方法的演变
人工智能·笔记·学习
依然范特东8 小时前
RAG学习总结3--检索技术
笔记·学习
白帽小阳9 小时前
[特殊字符]【2026最新】零基础入门学网络安全(详细路线图),看这篇就够了!
学习·安全·web安全·网络安全·安全运营·学习路线·web渗透