Java集成openAi的ChatGPT实战

效果图:

Java集成openAi的ChatGPT实战_第1张图片
Java集成openAi的ChatGPT实战_第2张图片

免费体验地址:AI智能助手

具体实现

public class OpenAiUtils {
    private static final Log LOG = LogFactory.getLog(OpenAiUtils.class);
    private static OpenAiProxyService openAiProxyService;

    public OpenAiUtils(OpenAiProxyService openAiProxyService) {
        OpenAiUtils.openAiProxyService = openAiProxyService;
    }

    public static void createStreamChatCompletion(String content) {
        createStreamChatCompletion(content, "DEFAULT USER", System.out);
    }

    public static void createStreamChatCompletion(String content, OutputStream os) {
        createStreamChatCompletion(content, "DEFAULT USER", os);
    }

    public static void createStreamChatCompletion(String content, String user, OutputStream os) {
        openAiProxyService.createStreamChatCompletion(content, user, os);
    }

    public static void createStreamChatCompletion(String content, String user, String model, OutputStream os) {
        createStreamChatCompletion(RoleEnum.USER.getRoleName(), content, user, model, 1.0D, 1.0D, os);
    }

    public static void createStreamChatCompletion(String role, String content, String user, String model, Double temperature, Double topP, OutputStream os) {
        createStreamChatCompletion(ChatCompletionRequest.builder().model(model).messages(Collections.singletonList(new ChatMessage(role, content))).user(user).temperature(temperature).topP(topP).stream(true).build(), os);
    }

    public static void createStreamChatCompletion(ChatCompletionRequest chatCompletionRequest, OutputStream os) {
        openAiProxyService.createStreamChatCompletion(chatCompletionRequest, os);
    }

    public static List createChatCompletion(String content) {
        return createChatCompletion(content, "DEFAULT USER");
    }

    public static List createChatCompletion(String content, String user) {
        return openAiProxyService.chatCompletion(content, user);
    }

    public static List createChatCompletion(String content, String user, String model) {
        return createChatCompletion(RoleEnum.USER.getRoleName(), content, user, model, 1.0D, 1.0D);
    }

    public static List createChatCompletion(String role, String content, String user, String model, Double temperature, Double topP) {
        return createChatCompletion(ChatCompletionRequest.builder().model(model).messages(Collections.singletonList(new ChatMessage(role, content))).user(user).temperature(temperature).topP(topP).build());
    }

    public static List createChatCompletion(ChatCompletionRequest chatCompletionRequest) {
        return openAiProxyService.chatCompletion(chatCompletionRequest);
    }

    /** @deprecated */
    @Deprecated
    public static List createCompletion(String prompt) {
        return createCompletion(prompt, "DEFAULT USER");
    }

    /** @deprecated */
    @Deprecated
    public static List createCompletion(String prompt, String user) {
        return openAiProxyService.completion(prompt, user);
    }

    /** @deprecated */
    @Deprecated
    public static List createCompletion(String prompt, String user, String model) {
        return createCompletion(prompt, user, model, 0.0D, 1.0D);
    }

    /** @deprecated */
    @Deprecated
    public static List createCompletion(String prompt, String user, String model, Double temperature, Double topP) {
        return createCompletion(CompletionRequest.builder().model(model).prompt(prompt).user(user).temperature(temperature).topP(topP).maxTokens(ModelEnum.getMaxTokens(model)).build());
    }

    /** @deprecated */
    @Deprecated
    public static List createCompletion(CompletionRequest completionRequest) {
        return openAiProxyService.completion(completionRequest);
    }

    public static List createImage(String prompt) {
        return createImage(prompt, "DEFAULT USER");
    }

    public static List createImage(String prompt, String user) {
        return createImage(CreateImageRequest.builder().prompt(prompt).user(user).build());
    }

    public static List createImage(CreateImageRequest createImageRequest) {
        return openAiProxyService.createImages(createImageRequest);
    }

    public static void downloadImage(String prompt, HttpServletResponse response) {
        downloadImage(prompt, ImageSizeEnum.S1024x1024.getSize(), response);
    }

    public static void downloadImage(String prompt, Integer n, HttpServletResponse response) {
        downloadImage(prompt, n, ImageSizeEnum.S1024x1024.getSize(), response);
    }

    public static void downloadImage(String prompt, String size, HttpServletResponse response) {
        downloadImage(prompt, 1, size, response);
    }

    public static void downloadImage(String prompt, Integer n, String size, HttpServletResponse response) {
        downloadImage(CreateImageRequest.builder().prompt(prompt).n(n).size(size).user("DEFAULT USER").build(), response);
    }

    public static void downloadImage(CreateImageRequest createImageRequest, HttpServletResponse response) {
        openAiProxyService.downloadImage(createImageRequest, response);
    }

    public static String billingUsage() {
        return openAiProxyService.billingUsage();
    }

    public static String billingUsage(String startDate, String endDate) {
        return openAiProxyService.billingUsage(startDate, endDate);
    }

    public static void forceClearCache(String cacheName) {
        openAiProxyService.forceClearCache(cacheName);
    }
}

你可能感兴趣的:(openAi,java,chatgpt,开发语言)