Skip to content

Commit

Permalink
Support IA
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jun 27, 2024
1 parent aa3ab17 commit 343dc53
Show file tree
Hide file tree
Showing 15 changed files with 985 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@
<artifactId>helidon-microprofile-health</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.ia</groupId>
<artifactId>helidon-ia-api</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.ia</groupId>
<artifactId>helidon-ia-openid</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
<artifactId>helidon-microprofile-server</artifactId>
Expand Down
42 changes: 42 additions & 0 deletions ia/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.ia</groupId>
<artifactId>helidon-ia-project</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-ia-api</artifactId>
<name>Helidon IA API</name>

<description>
Integration with IA API
</description>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
</dependencies>

</project>
36 changes: 36 additions & 0 deletions ia/api/src/main/java/io/helidon/ia/api/ChatModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.ia.api;

/**
* Class to be extended by different LLM implementations that provides
* the methods to operate with it.
*
* @param <IN> the input of the LLM
* @param <OUT> the output of LLM
*/
public interface ChatModel<IN, OUT> {

/**
* Invokes the LLM.
* Throws a runtime exception if errors.
*
* @param in the input of the LLM
* @return the output of LLM
*/
OUT call(IN in);
}
26 changes: 26 additions & 0 deletions ia/api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* IA API module.
*
*/
module io.helidon.ia.api {

requires io.helidon.common;

exports io.helidon.ia.api;
}
62 changes: 62 additions & 0 deletions ia/openai/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.ia</groupId>
<artifactId>helidon-ia-project</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-ia-openai</artifactId>
<name>Helidon IA OpenId</name>

<description>
Integration with IA OpenId
</description>

<dependencies>
<dependency>
<groupId>io.helidon.ia</groupId>
<artifactId>helidon-ia-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.http.media</groupId>
<artifactId>helidon-http-media-jackson</artifactId>
<!--JSON is required, but we let the user to choose-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
135 changes: 135 additions & 0 deletions ia/openai/src/main/java/io/helidon/ia/openai/OpenAIChatModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.ia.openai;

import java.net.URI;
import java.util.Objects;

import io.helidon.http.HeaderNames;
import io.helidon.http.Status;
import io.helidon.ia.api.ChatModel;
import io.helidon.webclient.api.HttpClient;
import io.helidon.webclient.api.HttpClientResponse;

/**
* Class that handles OpenAI communications specified in
* <a href="https://platform.openai.com/docs/api-reference/chat/">Chat</a> .
*/
public class OpenAIChatModel implements ChatModel<OpenAIRequest, OpenAIResponse> {

private final HttpClient<?> httpClient;
private final URI uri;
private final String apiKey;

private OpenAIChatModel(HttpClient<?> httpClient, URI uri, String apiKey) {
this.httpClient = httpClient;
this.uri = uri;
this.apiKey = apiKey;
}

@Override
public OpenAIResponse call(OpenAIRequest in) {
HttpClientResponse response = httpClient.post(uri.toString())
.header(HeaderNames.CONTENT_TYPE, "application/json")
.header(HeaderNames.AUTHORIZATION, "Bearer " + apiKey)
.submit(in);
if (response.status() != Status.OK_200) {
throw new IllegalStateException("Invalid response " + response.status()
+ ": " + response.as(String.class));
} else {
return response.as(OpenAIResponse.class);
}
}

/**
* Create a builder.
*
* @return a new fluent API builder
*/
public static Builder builder() {
return new Builder();
}

/**
* Create a OpenAIChatModel for the given API key and client.
*
* @param apiKey the OpenAI API key
* @param httpClient the HTTP Client
* @return a OpenAIChatModel default instance
*/
public static OpenAIChatModel create(String apiKey, HttpClient<?> httpClient) {
return builder().apiKey(apiKey).client(httpClient).build();
}

/**
* Fluent API builder for {@link io.helidon.ia.openid.OpenAIChatModel}.
*/
public static final class Builder implements io.helidon.common.Builder<Builder, OpenAIChatModel> {

private static final String DEFAULT_OPENID_URL = "https://api.openai.com";
private HttpClient<?> httpClient;
private String url = DEFAULT_OPENID_URL;
private String apiKey;

private Builder() {
}

@Override
public OpenAIChatModel build() {
Objects.requireNonNull(apiKey, "OpenAI API key is mandatory.");
Objects.requireNonNull(httpClient, "HttpClient is mandatory to make HTTP requests to OpenAI.");
URI uri = URI.create(url + "/v1/chat/completions");
return new OpenAIChatModel(httpClient, uri, apiKey);
}

/**
* Configure http client for HTTP requests, so multiple instances of {@link OpenAIChatModel} can
* re-use the same client for performance reasons.
* The client will never be closed by {@link OpenAIChatModel}.
*
* @param httpClient the configured HTTP Client
* @return updated builder instance
*/
public Builder client(HttpClient<?> httpClient) {
this.httpClient = httpClient;
return this;
}

/**
* The URL of OpenAI. It defaults to {@link #DEFAULT_OPENID_URL}.
*
* @param url of OpenAI
* @return updated builder instance
*/
public Builder url(String url) {
this.url = url;
return this;
}

/**
* Configure the API key to authenticate with OpenAI.
*
* @param apiKey
* @return updated builder instance
*/
public Builder apiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}
}

}
Loading

0 comments on commit 343dc53

Please sign in to comment.