package com.guojian.student.home.service.impl; /** * Created on 2023/2/10. * * [url=home.php?mod=space&uid=686208]@AuThor[/url] GuoJian * -version 1.0.0 */ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.guojian.student.home.model.param.ChatParam; import com.guojian.student.home.model.vo.ChatVO; import com.guojian.student.home.service.ChatGptService; import com.guojian.student.home.util.RSAUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.*; /** * @ClassName:ChatGptServiceImpl * @Author: GuoJian * @Date: 2023/2/10 8:57 * @Description: chatGpt二次封装服务实现 */ @Service public class ChatGptServiceImpl implements ChatGptService { @Override public ChatVO request(ChatParam chatParam){ String url = "https://api.openai.com/v1/completions" ; Map<String,Object> jparam = new HashMap<>(); jparam.put( "prompt" , chatParam.getPrompt()); jparam.put( "max_tokens" , 2048 ); jparam.put( "model" , "text-davinci-003" ); Map<String,String> headParams = new HashMap<>(); headParams.put( "Content-Type" , "application/json" ); headParams.put( "Authorization" , "Bearer 个人aipkey替换这里" ); String resultJson = doPostJson(url, JSONObject.toJSONString(jparam),headParams); JSONObject jsonObject = JSON.parseObject(resultJson); ChatVO result = new ChatVO(); result.setContent(jsonObject.getJSONArray( "choices" ).getJSONObject( 0 ).getString( "text" )); result.setIsUser( false ); return result; } @Override public String decipherin(String ciphertext) throws NoSuchAlgorithmException, InvalidKeySpecException { String privateKey = "这里是私钥" ; String decodedData = RSAUtils.privateDecrypt(ciphertext, RSAUtils.getPrivateKey(privateKey)); //传入密文和私钥,得到明文 return decodedData; } public static String doPostJson(String url, String params, Map<String,String> headParams) { String result = null ; //1. 获取httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null ; try { //2. 创建post请求 HttpPost httpPost = new HttpPost(url); //3.设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout( 120000 ).setConnectTimeout( 120000 ).build(); httpPost.setConfig(requestConfig); //4.提交参数发送请求 httpPost.setEntity( new StringEntity(params, ContentType.create( "application/json" , "utf-8" ))); //设置请求头 for (String head : headParams.keySet()) { httpPost.addHeader(head,headParams.get(head)); } response = httpClient.execute(httpPost); System.out.println(response); //5.得到响应信息 int statusCode = response.getStatusLine().getStatusCode(); //6. 判断响应信息是否正确 if (HttpStatus.SC_OK != statusCode) { //结束请求并抛出异常 httpPost.abort(); throw new RuntimeException( "HttpClient,error status code :" + statusCode); } //7. 转换成实体类 HttpEntity entity = response.getEntity(); if ( null != entity) { result = EntityUtils.toString(entity, "UTF-8" ); } EntityUtils.consume(entity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //8. 关闭所有资源连接 if ( null != response) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } if ( null != httpClient) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } } |