Springboot custom configuration class

Recently, the ChatGpt interface is written in java, and the proxy will be used when debugging locally, but the proxy will not be used after going online. Use springboot to make a configuration class. When the relevant properties of the proxy are configured in the configuration file, inject the proxy bean into the container and set it to the Proxy property of ChatGpt's OkClient

related code

package com.qaai.admin.chatgpt.infra.config;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
 * @author maqingbo
 * @date 2023/3/18 12:02
 * @email [email protected]
 */
@Data
@Component
@ConfigurationProperties(prefix = "openai.proxy")
public class ProxyConfig {
    
    
    private String proxyHost;
    private Integer proxyPort;
    private Proxy.Type type;

    @Bean
    @ConditionalOnProperty(name = {
    
    "openai.enable-proxy","openai.enableProxy"} ,havingValue = "true")
    public Proxy proxy() {
    
    
        Assert.notNull(proxyHost,"配置文件中未找到opai.proxy.proxy-host相关配置,请配置opai.proxy.proxy-port,或者设置enable-proxy为false来取消使用代理");
        Assert.notNull(proxyPort,"配置文件中未找到opai.proxy.proxy-port相关配置,请配置opai.proxy.proxy-port,或者设置enable-proxy为false来取消使用代理");
        Assert.notNull(type,"配置文件中未找到opai.proxy.type相关配置,请配置opai.proxy.proxy-port,或者设置enable-proxy为false来取消使用代理");
        return new Proxy(type, new InetSocketAddress(proxyHost, proxyPort));
    }
}

ChatGpt

package com.qaai.admin.chatgpt.infra.config;

import com.qaai.admin.chatgpt.infra.ChatGPT;
import lombok.Data;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.net.Proxy;

/**
 * @author maqingbo
 * @date 2023/3/18 13:02
 * @email [email protected]
 */
@Data
@Component
@ConfigurationProperties(prefix = "openai")
public class ChatGptConfig {
    
    
    /**
     * your openai key
     */
    private String key;
    /**
     * Chat completion API URL
     * <p>
     * see: <a href=https://platform.openai.com/docs/api-reference/chat/create>https://platform.openai.com/docs/api-reference/chat/create</a>
     */
    private String url;

    private boolean  enableProxy;

    @Bean
    public ChatGPT chatGPT(@Autowired(required = false) @Nullable Proxy proxy){
    
    
        ChatGPT chatGPT = new ChatGPT();
        chatGPT.setApiUrl(url);
        chatGPT.setApiKey(key);
        if (proxy!=null){
    
    
            OkHttpClient okHttpClient = new OkHttpClient.Builder().
                    proxy(proxy)
                    .build();
            chatGPT.setClient(okHttpClient);
        }
        return chatGPT;
    }


}

configuration file

openai:
  key: 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
  url: https://api.openai.com/v1/chat/completions
  enableProxy: false
  proxy:
    proxy-host: 127.0.0.1
    proxy-port: 7890
    type: http

explain

When the configuration file is configured with openai.enableProxy or openai.enable-proxy, and their value is true (corresponding to ProxyConfig @ConditionalOnProperty(name = {"openai.enable-proxy","openai.enableProxy"} ,havingValue = "true")), then the Proxy bean is automatically configured and put into the container.

When the configuration file is configured with openai.enableProxy or openai.enable-proxy, and their value is false, it means that the ChatGPT Bean can send requests to OpenAI without a proxy (depending on where your server is)

Guess you like

Origin blog.csdn.net/weixin_47287832/article/details/129636504