在Java项目中集成ChatGLM3-6B模型

1.使用Java来测试连接本地ChatGLM3-6B模型

public class test {
    public static void main(String[] args) {

        try {
            // 创建URL对象
            URL url = new URL("http://本地模型的ip地址:8000/v1/chat/completions");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置连接超时为30秒
            connection.setConnectTimeout(300000);

            // 设置读取超时为60秒
            connection.setReadTimeout(600000);

            // 设置请求方法为POST
            connection.setRequestMethod("POST");

            // 设置请求头的Content-Type为application/json
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            // 设置允许输出
            connection.setDoOutput(true);

            // JSON参数
            String jsonParams = "{\n" +
                    "    \"model\": \"chatglm3-6b\",\n" +
                    "    \"messages\": [\n" +
                    "        {\n" +
                    "            \"role\": \"system\",\n" +
                    "            \"content\": \"You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user’s instructions carefully. Respond using markdown.\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"role\": \"user\",\n" +
                    "            \"content\": \"你好,给我讲一个故事,大概100字\"\n" +
                    "        }\n" +
                    "    ],\n" +
                    "    \"stream\": false,\n" +
                    "    \"max_tokens\": 100,\n" +
                    "    \"temperature\": 0.8,\n" +
                    "    \"top_p\": 0.8\n" +
                    "}";

            // 将JSON参数写入输出流
            OutputStream os = connection.getOutputStream();
            os.write(jsonParams.getBytes("UTF-8"));
            os.close();

            // 获取响应状态码
            int responseCode = connection.getResponseCode();
            System.out.println("响应状态码:" + responseCode);

            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 输出响应内容
            System.out.println("响应内容:" + response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

2.根据调用ChatGLM3-6B模型API所需要的参数进行设计

所需要的参数:如何本地调用ChatGLM3-6B的API-CSDN博客

参数设置:需要指定model的名称和发送到本地模型的请求地址

/**
 * chatGLM3-6b参数设置
 */
public interface ChatConstant {
    String model="chatglm3-6b";

    boolean stream = false;

    int max_tokens=100;

    double temperature = 0.5;

    double top_p=0.5;

    String roleSystem="system";

    String roleUser="user";

    String requestUrl="http://本地模型的ip地址:8000/v1/chat/completions";
}

3.设计模型所需要的请求与响应内容

Choice

@Data
public class Choice {
    private int index;
    private BigModel message;
    private String finish_reason;
}

Usage

@Data
public class Usage {
    private int prompt_tokens;

    private int total_tokens;

    private int completion_tokens;
}

BigModel

@Data
@NoArgsConstructor
@AllArgsConstructor
public class BigModel implements Serializable {
    @JsonProperty("role")
    private String role;// user system
    @JsonProperty("content")
    private String content;
}

ModelRequestJSON

@Data
public class ModelRequestJSON {

    public final String model= ChatConstant.model;

    private List<BigModel> messages;

    public final boolean stream=ChatConstant.stream;

    public final int max_tokens=ChatConstant.max_tokens;

    public final double temperature = ChatConstant.temperature;

    public final double top_p= ChatConstant.top_p;
}

ModelResponseJSON

@Data
public class ModelResponseJSON {
    private String model;
    private String id;
    private String object;
    private List<Choice> choices;
    private Long created;
    private Usage usage;
}

4.ChatGLM3-6b模型控制层

/**
 * ChatGLM3-6b访问控制层
 */
@RestController
@RequestMapping("/system/chat")
public class ChatGLMController extends BaseController {
    @Resource
    private IChatGLMControllerService iChatGLMControllerService;

    /**
     * 访问本地的ChatGLM3-6B模型
     */
    @PostMapping("/sendContent")
    public AjaxResult sendContent(@RequestBody MessagesJson messagesJson){
        String chatCall = iChatGLMControllerService.sendContent(messagesJson.getMessages());
        return AjaxResult.success("响应成功:"+chatCall);
    }
}

5.ChatGLM3-6B模型Service接口

/**
 * ChatGLM3-6B模型Service接口
 */
public interface IChatGLMControllerService {

    String sendContent(List<BigModel> messages);
}

6.实现类的实现

try中的代码建议抽取出来成工具类

@Service
public class ChatGLMControllerServiceImpl implements IChatGLMControllerService {
    /**
     * 访问本地的ChatGLM3-6B模型
     * @param messages
     * @return
     */
    @Override
    public String sendContent(List<BigModel> messages) {
        try {
            // 创建URL对象
            URL url = new URL(ChatConstant.requestUrl);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置连接超时为30秒
            connection.setConnectTimeout(30000);
            // 设置读取超时为60秒
            connection.setReadTimeout(60000);
            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 设置请求头的Content-Type为application/json
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            // 设置允许输出
            connection.setDoOutput(true);
            ModelRequestJSON requestJSON = new ModelRequestJSON();
            if (!messages.isEmpty()){
                requestJSON.setMessages(messages);
                String jsonParams = JSONObject.toJSONString(requestJSON);
                // 将JSON参数写入输出流
                OutputStream os = connection.getOutputStream();
                os.write(jsonParams.getBytes("UTF-8"));
                os.close();
                // 获取响应状态码
                int responseCode = connection.getResponseCode();
                System.out.println("响应状态码:" + responseCode);

                // 读取响应内容
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                System.out.println("response = " + response);
                ModelResponseJSON modelResponseJSON = (ModelResponseJSON) JSON.parseObject(response.toString(), ModelResponseJSON.class);
                return modelResponseJSON.getChoices().get(0).getMessage().getContent();
            }
        } catch (ProtocolException e) {
            throw new RuntimeException(e);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;

    }
}

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值