前言
好久没用regex了,之前用的贼溜的东西都忘完了,这次遇到一个东西恰好我觉得用正则表达式会方便一点,所以把这次的开发过程记录一下
- 这遍文章包括Java如何使用正则表达式去匹配
- 解决正确的表达式却匹配不到数据的问题
- 使用正则表达式却出现栈溢出的问题
背景需求
- 首先我会根据提示词调用chatGPT的接口,然后获得返回的内容,代码大致如下
/**
* 请求聊天 GPT
* @param prompt 提示
*/
static String requestChatGPT(String prompt) {
CloseableHttpClient httpClient = HttpClients.createDefault();
ObjectMapper objectMapper = new ObjectMapper();
try {
// 创建HTTP POST请求 的head
HttpPost httpPost = new HttpPost(CommonConstant.CHATGPT_HOST);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", "Bearer " + CommonConstant.CHATGPT_AUTHORIZATION);
httpPost.setHeader("OpenAI-Organization", CommonConstant.CHATGPT_ORGANIZATION);
httpPost.setHeader("OpenAI-Project", CommonConstant.CHATGPT_PROJECT);
// 创建JSON请求体
ObjectNode requestBody = objectMapper.createObjectNode();
requestBody.put("model", CommonConstant.CHATGPT_MODEL);
requestBody.putArray("messages")
.addObject()
.put("role", "user")
.put("content", prompt);
requestBody.put("temperature", 0.7);
httpPost.setEntity(new StringEntity(requestBody.toString()));
// 发送请求并获取响应
CloseableHttpResponse response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
response.close();