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 ec80c90
Show file tree
Hide file tree
Showing 15 changed files with 945 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;
}
56 changes: 56 additions & 0 deletions ia/openai/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</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 jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import java.util.Objects;

import io.helidon.ia.api.ChatModel;

/**
* 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 Client client;
private final String url;
private final String apiKey;

private OpenAIChatModel(Client client, String url, String apiKey) {
this.client = client;
this.url = url;
this.apiKey = apiKey;
}

@Override
public OpenAIResponse call(OpenAIRequest in) {
Response response = client.target(url).path("/v1/chat/completions")
.request()
.header("Authorization", "Bearer " + apiKey)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(in, MediaType.APPLICATION_JSON_TYPE));
if (response.getStatus() != 200) {
throw new IllegalStateException("Invalid response " + response.getStatus()
+ ": " + response.readEntity(String.class));
} else {
return response.readEntity(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 client the client
* @return a OpenAIChatModel default instance
*/
public static OpenAIChatModel create(String apiKey, Client client) {
return builder().apiKey(apiKey).client(client).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 Client client;
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(client, "Client is mandatory to make HTTP requests to OpenAI.");
return new OpenAIChatModel(client, url, apiKey);
}

/**
* Configure 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 client the configured client
* @return updated builder instance
*/
public Builder client(Client client) {
this.client = client;
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 ec80c90

Please sign in to comment.