⬆️ 升级依赖

This commit is contained in:
ronger 2023-08-01 10:49:37 +08:00
parent e1584729fb
commit c06a157e66
4 changed files with 75 additions and 17 deletions

10
pom.xml
View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version> <version>2.7.13</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.rymcu</groupId> <groupId>com.rymcu</groupId>
@ -104,7 +104,7 @@
<dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId> <artifactId>tomcat-embed-core</artifactId>
<version>9.0.68</version> <version>9.0.73</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -328,7 +328,7 @@
<dependency> <dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId> <groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>client</artifactId> <artifactId>client</artifactId>
<version>0.12.0</version> <version>0.14.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.squareup.retrofit2</groupId> <groupId>com.squareup.retrofit2</groupId>
@ -366,6 +366,10 @@
<groupId>io.micrometer</groupId> <groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId> <artifactId>micrometer-registry-prometheus</artifactId>
</dependency> </dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -17,4 +17,6 @@ public class NotificationConstant {
public static String UpdateArticle = "4"; public static String UpdateArticle = "4";
public static String UpdateArticleStatus = "5";
} }

View File

@ -9,11 +9,8 @@ import com.rymcu.forest.openai.service.OpenAiService;
import com.rymcu.forest.openai.service.SseService; import com.rymcu.forest.openai.service.SseService;
import com.rymcu.forest.util.Html2TextUtil; import com.rymcu.forest.util.Html2TextUtil;
import com.rymcu.forest.util.UserUtils; import com.rymcu.forest.util.UserUtils;
import com.theokanning.openai.completion.chat.ChatCompletionChoice;
import com.theokanning.openai.completion.chat.ChatCompletionChunk;
import com.theokanning.openai.completion.chat.ChatCompletionRequest; import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage; import com.theokanning.openai.completion.chat.ChatMessage;
import io.reactivex.Flowable;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;

View File

@ -4,12 +4,14 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.rymcu.forest.util.SpringContextHolder;
import com.rymcu.forest.util.Utils;
import com.theokanning.openai.DeleteResult; import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.OpenAiApi;
import com.theokanning.openai.OpenAiError; import com.theokanning.openai.OpenAiError;
import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.OpenAiHttpException;
import com.theokanning.openai.audio.CreateTranscriptionRequest;
import com.theokanning.openai.audio.CreateTranslationRequest;
import com.theokanning.openai.audio.TranscriptionResult;
import com.theokanning.openai.audio.TranslationResult;
import com.theokanning.openai.client.OpenAiApi;
import com.theokanning.openai.completion.CompletionChunk; import com.theokanning.openai.completion.CompletionChunk;
import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult; import com.theokanning.openai.completion.CompletionResult;
@ -31,26 +33,26 @@ import com.theokanning.openai.image.ImageResult;
import com.theokanning.openai.model.Model; import com.theokanning.openai.model.Model;
import com.theokanning.openai.moderation.ModerationRequest; import com.theokanning.openai.moderation.ModerationRequest;
import com.theokanning.openai.moderation.ModerationResult; import com.theokanning.openai.moderation.ModerationResult;
import io.reactivex.BackpressureStrategy; import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.Single; import io.reactivex.Single;
import okhttp3.*; import okhttp3.*;
import org.springframework.core.env.Environment;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import retrofit2.Call;
import retrofit2.HttpException; import retrofit2.HttpException;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.Call;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory; import retrofit2.converter.jackson.JacksonConverterFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.rymcu.forest.util.SpringContextHolder;
import org.springframework.core.env.Environment;
public class OpenAiService { public class OpenAiService {
@ -60,6 +62,7 @@ public class OpenAiService {
private final OpenAiApi api; private final OpenAiApi api;
private final ExecutorService executorService; private final ExecutorService executorService;
private static final Environment env = SpringContextHolder.getBean(Environment.class); private static final Environment env = SpringContextHolder.getBean(Environment.class);
/** /**
@ -254,6 +257,61 @@ public class OpenAiService {
return execute(api.createImageVariation(builder.build())); return execute(api.createImageVariation(builder.build()));
} }
public TranscriptionResult createTranscription(CreateTranscriptionRequest request, String audioPath) {
java.io.File audio = new java.io.File(audioPath);
return createTranscription(request, audio);
}
public TranscriptionResult createTranscription(CreateTranscriptionRequest request, java.io.File audio) {
RequestBody audioBody = RequestBody.create(MediaType.parse("audio"), audio);
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MediaType.get("multipart/form-data"))
.addFormDataPart("model", request.getModel())
.addFormDataPart("file", audio.getName(), audioBody);
if (request.getPrompt() != null) {
builder.addFormDataPart("prompt", request.getPrompt());
}
if (request.getResponseFormat() != null) {
builder.addFormDataPart("response_format", request.getResponseFormat());
}
if (request.getTemperature() != null) {
builder.addFormDataPart("temperature", request.getTemperature().toString());
}
if (request.getLanguage() != null) {
builder.addFormDataPart("language", request.getLanguage());
}
return execute(api.createTranscription(builder.build()));
}
public TranslationResult createTranslation(CreateTranslationRequest request, String audioPath) {
java.io.File audio = new java.io.File(audioPath);
return createTranslation(request, audio);
}
public TranslationResult createTranslation(CreateTranslationRequest request, java.io.File audio) {
RequestBody audioBody = RequestBody.create(MediaType.parse("audio"), audio);
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MediaType.get("multipart/form-data"))
.addFormDataPart("model", request.getModel())
.addFormDataPart("file", audio.getName(), audioBody);
if (request.getPrompt() != null) {
builder.addFormDataPart("prompt", request.getPrompt());
}
if (request.getResponseFormat() != null) {
builder.addFormDataPart("response_format", request.getResponseFormat());
}
if (request.getTemperature() != null) {
builder.addFormDataPart("temperature", request.getTemperature().toString());
}
return execute(api.createTranslation(builder.build()));
}
public ModerationResult createModeration(ModerationRequest request) { public ModerationResult createModeration(ModerationRequest request) {
return execute(api.createModeration(request)); return execute(api.createModeration(request));
} }
@ -339,11 +397,8 @@ public class OpenAiService {
} }
public static OkHttpClient defaultClient(String token, Duration timeout) { public static OkHttpClient defaultClient(String token, Duration timeout) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = Utils.getIpAddress(request);
return new OkHttpClient.Builder() return new OkHttpClient.Builder()
.addInterceptor(new AuthenticationInterceptor(token)) .addInterceptor(new AuthenticationInterceptor(token))
.addInterceptor(new IpAddressInterceptor(ip))
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS)) .connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
.readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS) .readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
.build(); .build();