🎨 代码调整

🎨 代码调整
This commit is contained in:
ronger 2023-08-24 09:46:44 +08:00 committed by GitHub
commit b06866b924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 12 deletions

10
pom.xml
View File

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

View File

@ -17,4 +17,6 @@ public class NotificationConstant {
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.util.Html2TextUtil;
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.ChatMessage;
import io.reactivex.Flowable;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;

View File

@ -4,12 +4,15 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
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.OpenAiApi;
import com.theokanning.openai.OpenAiError;
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.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
@ -31,26 +34,30 @@ import com.theokanning.openai.image.ImageResult;
import com.theokanning.openai.model.Model;
import com.theokanning.openai.moderation.ModerationRequest;
import com.theokanning.openai.moderation.ModerationResult;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.Single;
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.Retrofit;
import retrofit2.Call;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import com.rymcu.forest.util.SpringContextHolder;
import org.springframework.core.env.Environment;
import javax.servlet.http.HttpServletRequest;
public class OpenAiService {
@ -60,6 +67,7 @@ public class OpenAiService {
private final OpenAiApi api;
private final ExecutorService executorService;
private static final Environment env = SpringContextHolder.getBean(Environment.class);
/**
@ -254,6 +262,61 @@ public class OpenAiService {
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) {
return execute(api.createModeration(request));
}
@ -337,7 +400,6 @@ public class OpenAiService {
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return mapper;
}
public static OkHttpClient defaultClient(String token, Duration timeout) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = Utils.getIpAddress(request);