Skip to content

Commit

Permalink
Add username and password for proxy HamaWhiteGG#30
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaWhiteGG committed Jul 14, 2023
1 parent efe2f6c commit ec058f1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public class BaseOpenAI extends BaseLLM {
*/
protected String openaiProxy;

/**
* the username for proxy authentication (optional)
*/
protected String proxyUsername;

/**
* the password for proxy authentication (optional)
*/
protected String proxyPassword;

/**
* Batch size to use when passing multiple documents to generate.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public OpenAI init() {
.openaiApiKey(openaiApiKey)
.openaiOrganization(openaiOrganization)
.openaiProxy(openaiProxy)
.proxyUsername(proxyUsername)
.proxyPassword(proxyPassword)
.requestTimeout(requestTimeout)
.build()
.init();
Expand Down
12 changes: 11 additions & 1 deletion openai-client/src/main/java/com/hw/openai/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public class OpenAiClient {

private String openaiProxy;

/**
* the username for proxy authentication (optional)
*/
private String proxyUsername;

/**
* the password for proxy authentication (optional)
*/
private String proxyPassword;

/**
* Timeout for requests to OpenAI completion API. Default is 16 seconds.
*/
Expand Down Expand Up @@ -109,7 +119,7 @@ public OpenAiClient init() {
httpClientBuilder.addInterceptor(loggingInterceptor);

if (StringUtils.isNotEmpty(openaiProxy)) {
httpClientBuilder.proxy(ProxyUtils.http(openaiProxy));
httpClientBuilder.proxy(ProxyUtils.http(openaiProxy, proxyUsername, proxyPassword));
}
httpClient = httpClientBuilder.build();

Expand Down
35 changes: 28 additions & 7 deletions openai-client/src/main/java/com/hw/openai/utils/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import lombok.experimental.UtilityClass;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.*;

import static java.util.Objects.requireNonNull;

/**
* A utility class for creating HTTP proxies.
Expand All @@ -34,15 +34,36 @@ public class ProxyUtils {
/**
* Creates an HTTP proxy object based on the provided address.
*
* @param address the address of the proxy server in the format 'http://host:port'
* @return the Proxy object representing the HTTP proxy server
* @param address the address of the proxy server in the format <a href="http://host:port">http://host:port</a>
* @param username the username for proxy authentication (optional)
* @param password the password for proxy authentication (optional)
* @return the {@link Proxy} object representing the HTTP proxy server
*/
public Proxy http(String address) {
public static Proxy http(String address, String username, String password) {
requireNonNull(address, "Proxy address cannot be null");

// Parse the proxy server address
URI uri = URI.create(address);
URI uri;
try {
uri = new URI(address);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid proxy address: " + address, e);
}

String hostname = uri.getHost();
int port = uri.getPort();

if (username != null && password != null) {
// Set the proxy authentication credentials
Authenticator.setDefault(new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}

// Create the Proxy object
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void testChatCompletion() {
.messages(List.of(message))
.build();

assertThat(client.chatCompletion(chatCompletion)).isEqualTo("Hello there! How can I assist you today?");
assertThat(client.chatCompletion(chatCompletion)).isEqualTo("Hello! How can I assist you today?");
}

@Test
Expand Down

0 comments on commit ec058f1

Please sign in to comment.