| 
					
				 | 
			
			
				@@ -0,0 +1,85 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.tzld.commons.llm.gpt; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.commons.llm.common.Constant; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.commons.llm.common.ConversationManager; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.commons.llm.gpt.param.OpenAiGptParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.commons.llm.gpt.util.AigcUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.commons.lang3.time.StopWatch; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpEntity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpMethod; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.stereotype.Component; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.util.CollectionUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.client.RestTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.annotation.Resource; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Objects; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.ThreadLocalRandom; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author sunxy 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Component(value = Constant.OFFICIAL_GPT_CONVERSATION_MANAGER) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class GptConversationManager implements ConversationManager { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Resource 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private RestTemplate restTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${official.openai.conversation.url:}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String conversationUrl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${official.openai.auths:") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String auths; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public JSONObject conversation(OpenAiGptParam openAiGptParam, String auth) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (openAiGptParam == null || StringUtils.isBlank(openAiGptParam.getModel()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                || CollectionUtils.isEmpty(openAiGptParam.getMessages())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (StringUtils.isBlank(auth)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        StopWatch st = StopWatch.createStarted(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String requestParams = JSONObject.toJSONString(openAiGptParam); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        HttpEntity<String> entity = new HttpEntity<>(requestParams, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                AigcUtils.getAigcHeaders(auth)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        JSONObject result = doRequestOpenai(requestParams, entity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("request auth {} param {} result {}  cost {}", auth, JSONObject.toJSONString(openAiGptParam), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                Objects.isNull(result) ? null : result.toJSONString(), st.getTime()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private JSONObject doRequestOpenai(String requestParams, HttpEntity<String> entity) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("openai chat request param {}", requestParams); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        JSONObject jsonObject = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int i = 0; i < 3; i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                jsonObject = restTemplate.exchange(conversationUrl, HttpMethod.POST, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        entity, JSONObject.class).getBody(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (jsonObject == null || Objects.nonNull(jsonObject.get("choices"))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.error("request openAi error ", e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return jsonObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String chooseAuth() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (StringUtils.isBlank(auths)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String[] authArray = auths.split(","); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (authArray.length == 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return authArray[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return authArray[ThreadLocalRandom.current().nextInt(authArray.length)]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |