|
| 1 | +package ai.chat2db.server.web.api.controller.ai.rest.client; |
| 2 | + |
| 3 | +import ai.chat2db.server.tools.common.exception.ParamBusinessException; |
| 4 | +import ai.chat2db.server.web.api.controller.ai.fastchat.interceptor.FastChatHeaderAuthorizationInterceptor; |
| 5 | +import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatCompletionsOptions; |
| 6 | +import ai.chat2db.server.web.api.controller.ai.fastchat.model.FastChatMessage; |
| 7 | +import cn.hutool.http.ContentType; |
| 8 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import lombok.Getter; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import okhttp3.MediaType; |
| 13 | +import okhttp3.OkHttpClient; |
| 14 | +import okhttp3.Request; |
| 15 | +import okhttp3.RequestBody; |
| 16 | +import okhttp3.sse.EventSource; |
| 17 | +import okhttp3.sse.EventSourceListener; |
| 18 | +import okhttp3.sse.EventSources; |
| 19 | +import org.apache.commons.collections4.CollectionUtils; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +/** |
| 27 | + * Custom AI interface client |
| 28 | + * @author moji |
| 29 | + */ |
| 30 | +@Slf4j |
| 31 | +public class RestAIStreamClient { |
| 32 | + /** |
| 33 | + * apikey |
| 34 | + */ |
| 35 | + @Getter |
| 36 | + @NotNull |
| 37 | + private String apiKey; |
| 38 | + |
| 39 | + /** |
| 40 | + * apiHost |
| 41 | + */ |
| 42 | + @Getter |
| 43 | + @NotNull |
| 44 | + private String apiHost; |
| 45 | + |
| 46 | + /** |
| 47 | + * model |
| 48 | + */ |
| 49 | + @Getter |
| 50 | + private String model; |
| 51 | + /** |
| 52 | + * okHttpClient |
| 53 | + */ |
| 54 | + @Getter |
| 55 | + private OkHttpClient okHttpClient; |
| 56 | + |
| 57 | + /** |
| 58 | + * Construct instance object |
| 59 | + * |
| 60 | + * @param builder |
| 61 | + */ |
| 62 | + public RestAIStreamClient(Builder builder) { |
| 63 | + this.apiKey = builder.apiKey; |
| 64 | + this.apiHost = builder.apiHost; |
| 65 | + this.model = builder.model; |
| 66 | + this.okHttpClient = new OkHttpClient |
| 67 | + .Builder() |
| 68 | + .addInterceptor(new FastChatHeaderAuthorizationInterceptor(this.apiKey)) |
| 69 | + .connectTimeout(10, TimeUnit.SECONDS) |
| 70 | + .writeTimeout(50, TimeUnit.SECONDS) |
| 71 | + .readTimeout(50, TimeUnit.SECONDS) |
| 72 | + .build(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * structure |
| 77 | + * |
| 78 | + * @return |
| 79 | + */ |
| 80 | + public static RestAIStreamClient.Builder builder() { |
| 81 | + return new RestAIStreamClient.Builder(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * builder |
| 86 | + */ |
| 87 | + public static final class Builder { |
| 88 | + private String apiKey; |
| 89 | + |
| 90 | + private String apiHost; |
| 91 | + |
| 92 | + private String model; |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + * OkhttpClient |
| 97 | + */ |
| 98 | + private OkHttpClient okHttpClient; |
| 99 | + |
| 100 | + public Builder() { |
| 101 | + } |
| 102 | + |
| 103 | + public RestAIStreamClient.Builder apiKey(String apiKeyValue) { |
| 104 | + this.apiKey = apiKeyValue; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param apiHostValue |
| 110 | + * @return |
| 111 | + */ |
| 112 | + public RestAIStreamClient.Builder apiHost(String apiHostValue) { |
| 113 | + this.apiHost = apiHostValue; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param modelValue |
| 119 | + * @return |
| 120 | + */ |
| 121 | + public RestAIStreamClient.Builder model(String modelValue) { |
| 122 | + this.model = modelValue; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + public RestAIStreamClient.Builder okHttpClient(OkHttpClient val) { |
| 128 | + this.okHttpClient = val; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + public RestAIStreamClient build() { |
| 133 | + return new RestAIStreamClient(this); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + /** |
| 140 | + * Q&A interface stream form |
| 141 | + * |
| 142 | + * @param chatMessages |
| 143 | + * @param eventSourceListener |
| 144 | + */ |
| 145 | + public void streamCompletions(List<FastChatMessage> chatMessages, EventSourceListener eventSourceListener) { |
| 146 | + if (CollectionUtils.isEmpty(chatMessages)) { |
| 147 | + log.error("param error:Rest AI Prompt cannot be empty"); |
| 148 | + throw new ParamBusinessException("prompt"); |
| 149 | + } |
| 150 | + if (Objects.isNull(eventSourceListener)) { |
| 151 | + log.error("param error:RestAIEventSourceListener cannot be empty"); |
| 152 | + throw new ParamBusinessException(); |
| 153 | + } |
| 154 | + log.info("Rest AI, prompt:{}", chatMessages.get(chatMessages.size() - 1).getContent()); |
| 155 | + try { |
| 156 | + |
| 157 | + FastChatCompletionsOptions chatCompletionsOptions = new FastChatCompletionsOptions(chatMessages); |
| 158 | + chatCompletionsOptions.setStream(true); |
| 159 | + chatCompletionsOptions.setModel(this.model); |
| 160 | + |
| 161 | + EventSource.Factory factory = EventSources.createFactory(this.okHttpClient); |
| 162 | + ObjectMapper mapper = new ObjectMapper(); |
| 163 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 164 | + String requestBody = mapper.writeValueAsString(chatCompletionsOptions); |
| 165 | + Request request = new Request.Builder() |
| 166 | + .url(apiHost) |
| 167 | + .post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody)) |
| 168 | + .build(); |
| 169 | + //Create event |
| 170 | + EventSource eventSource = factory.newEventSource(request, eventSourceListener); |
| 171 | + log.info("finish invoking rest ai"); |
| 172 | + } catch (Exception e) { |
| 173 | + log.error("rest ai error", e); |
| 174 | + eventSourceListener.onFailure(null, e, null); |
| 175 | + throw new ParamBusinessException(); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | +} |
0 commit comments