Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spring-ai-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.9.0</version>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@

package org.springframework.ai.autoconfigure.openai;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.theokanning.openai.OpenAiApi;
import com.theokanning.openai.service.OpenAiService;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;

import org.springframework.ai.autoconfigure.NativeHints;
import org.springframework.ai.embedding.EmbeddingClient;
import org.springframework.ai.openai.embedding.OpenAiEmbeddingClient;
Expand Down Expand Up @@ -49,7 +56,19 @@ public OpenAiService theoOpenAiService(OpenAiProperties openAiProperties) {
throw new IllegalArgumentException(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to only force an API key if the URL was anything other than the default base URL (maybe that's too complex or nondeterministic)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen two cases of model hosting projects that expose the OpenAI API locally and do not require a key. I think we can remove the requirement for a key when the base url is not https://api.openai.com and see how it goes.

"You must provide an API key with the property name " + CONFIG_PREFIX + ".api-key");
}
return new OpenAiService(openAiProperties.getApiKey(), openAiProperties.getDuration());

ObjectMapper mapper = OpenAiService.defaultObjectMapper();
OkHttpClient client = OpenAiService.defaultClient(openAiProperties.getApiKey(), openAiProperties.getDuration());

Retrofit retrofit = new Retrofit.Builder().baseUrl(openAiProperties.getBaseUrl())
.client(client)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();

OpenAiApi api = retrofit.create(OpenAiApi.class);

return new OpenAiService(api);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class OpenAiProperties {

private String model = "gpt-3.5-turbo";

private String baseUrl = "https://api.openai.com";

public String getApiKey() {
return apiKey;
}
Expand Down Expand Up @@ -67,4 +69,12 @@ public void setDuration(Duration duration) {
this.duration = duration;
}

public String getBaseUrl() {
return baseUrl;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

}