Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Unwrap SocketTimeoutException and update tests accordingly #373

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/src/main/java/example/OpenAiApiFunctionsExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.theokanning.openai.service.FunctionExecutor;
import com.theokanning.openai.service.OpenAiService;

import java.net.SocketTimeoutException;
import java.util.*;

class OpenAiApiFunctionsExample {
Expand Down Expand Up @@ -38,7 +39,7 @@ public WeatherResponse(String location, WeatherUnit unit, int temperature, Strin
}
}

public static void main(String... args) {
public static void main(String... args) throws SocketTimeoutException {
String token = System.getenv("OPENAI_TOKEN");
OpenAiService service = new OpenAiService(token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;
Expand Down Expand Up @@ -134,8 +135,15 @@ public Flowable<CompletionChunk> streamCompletion(CompletionRequest request) {
return stream(api.createCompletionStream(request), CompletionChunk.class);
}

public ChatCompletionResult createChatCompletion(ChatCompletionRequest request) {
return execute(api.createChatCompletion(request));
public ChatCompletionResult createChatCompletion(ChatCompletionRequest request) throws SocketTimeoutException{
try {
return execute(api.createChatCompletion(request));
} catch (RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof SocketTimeoutException)
throw new SocketTimeoutException(e.getCause().getMessage());
else
throw e;
}
}

public Flowable<ChatCompletionChunk> streamChatCompletion(ChatCompletionRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.theokanning.openai.completion.chat.*;
import org.junit.jupiter.api.Test;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -47,7 +48,7 @@ public WeatherResponse(String location, WeatherUnit unit, int temperature, Strin
OpenAiService service = new OpenAiService(token);

@Test
void createChatCompletion() {
void createChatCompletion() throws SocketTimeoutException {
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a dog and will speak as such.");
messages.add(systemMessage);
Expand Down Expand Up @@ -88,7 +89,7 @@ void streamChatCompletion() {
}

@Test
void createChatCompletionWithFunctions() {
void createChatCompletionWithFunctions() throws SocketTimeoutException {
final List<ChatFunction> functions = Collections.singletonList(ChatFunction.builder()
.name("get_weather")
.description("Get the current weather in a given location")
Expand Down