Bug description
When execute a request to openai with a new OpenAiApi created manually, I got a URI is not absolute error.
Environment java 17 org.springframework.ai 0.8.0-snapshot
Comment From: markpollack
Thanks, will investigate and resolve for the 0.8.0 release.
Comment From: StillTogether
Thanks, will investigate and resolve for the 0.8.0 release.
Thanks very much~
Comment From: pankajtandon
I tested this and reproduced the error. It seems that restTemplate can be initialized with a DefaultUriBuilderFactory that takes in the baseUrl.
This seems at odds with the OpenAiApi constructor that ALSO takes in the baseUrl, thereby needing to specify the baseUrl two times! Anyhow, here's the working code:
private static final String BASE_URL = "https://api.openai.com";
@Autowired
private Environment environment;
@Test
void manuallyAccessOpenAiApi() {
String key = environment.getProperty("OPENAI_API_KEY", String.class);
RestTemplate restTemplate = new RestTemplate();
DefaultUriBuilderFactory uriFactory = new DefaultUriBuilderFactory(BASE_URL);
//uriFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.TEMPLATE_AND_VALUES);
restTemplate.setUriTemplateHandler(uriFactory);
OpenAiApi openAiApi = new OpenAiApi(BASE_URL, key, RestClient.builder(restTemplate));
Assert.notNull(openAiApi, "openAiApi cannot be null");
ChatClient openAiChatClient = new OpenAiChatClient(openAiApi);
String joke = openAiChatClient.call("Tell me a joke");
System.out.println("Joek: " + joke);
Assert.notNull(joke, "C'mon , no joke is not an option!");
}
I have a PR that I can also with this test that I can submit to serve as a sample. @markpollack, should I?
Comment From: tzolov
@StillTogether , thanks for catching this.
It works fine with the default RestClient.builder()
but fails with the RestClient.builder(new RestTemplate())
builder.
Apparently it is a RestClient issue: https://github.com/spring-projects/spring-framework/issues/32180
In the mean time if you just want to pass your custom request-factory you can do something like this:
var clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
clientHttpRequestFactory.setProxy(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("localhost", 80)));
RestClient.Builder builder = RestClient.builder().requestFactory(clientHttpRequestFactory);
OpenAiApi openAiApi = new OpenAiApi(BASE_URL, key, builder);
and if you have to use a RestTemplate, then perhaps you can apply the workaround suggested by @pankajtandon (thanks pankajtandon):
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUrl));
Hope this helps
Comment From: StillTogether
@StillTogether , thanks for catching this.
It works fine with the default
RestClient.builder()
but fails with theRestClient.builder(new RestTemplate())
builder. Apparently it is a RestClient issue: spring-projects/spring-framework#32180In the mean time if you just want to pass your custom request-factory you can do something like this:
```java var clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); clientHttpRequestFactory.setProxy(new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("localhost", 80)));
RestClient.Builder builder = RestClient.builder().requestFactory(clientHttpRequestFactory);
OpenAiApi openAiApi = new OpenAiApi(BASE_URL, key, builder); ```
and if you have to use a RestTemplate, then perhaps you can apply the workaround suggested by @pankajtandon (thanks pankajtandon):
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUrl));
Hope this helps
Thanks very much @tzolov and @pankajtandon , it's very useful to me.
Comment From: markpollack
With the next release of spring-framework, this issue will be resolved. Until then, follow the workaround.
https://github.com/spring-projects/spring-framework/issues/32180
Comment From: tzolov
FYI, Spring 0.8.1-SN with Boot 3.2.3 has this problem resolved. Verified by RestClientBuilderTests#test2