Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ainoya/openai-kiota-client-java
https://github.com/ainoya/openai-kiota-client-java
Last synced: about 21 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/ainoya/openai-kiota-client-java
- Owner: ainoya
- License: mit
- Created: 2024-02-07T12:58:42.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-07T13:00:43.000Z (9 months ago)
- Last Synced: 2024-10-17T23:03:03.462Z (20 days ago)
- Language: Java
- Size: 234 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
OpenAI API Client generated by kiota
=======================This is a Java client library for the OpenAI API. It is generated using the [Kiota](https://github.com/microsoft/kiota) code generator.
## Generate script
You can generate the client library using the `generate_client.sh` script. it runs the `kiota` code generator to generate the client library with docker.
```shell
./generate_client.sh
```## API spec modification
The OpenAI API spec is defined in the [openai.yaml](./openai.yaml) file which is a modified version of the original OpenAI API spec. The original OpenAI API spec is available at [openai/openai-api](https://github.com/openai/openai-openapi). If you want to look at the differences between the original and the modified spec, you can use the `git diff` command.
```shell
git diff openai.yaml openai.orig.yaml
```## Example code
You can also find the example code in the [ExampleApp.java](./src/example/java/dev/ainoya/kiota/openai/example/ExampleApp.java).
This example code is a simple Java application that uses the generated client library to interact with the OpenAI API.
```java
package dev.ainoya.kiota.openai.example;import com.microsoft.kiota.ApiException;
import com.microsoft.kiota.authentication.AccessTokenProvider;
import com.microsoft.kiota.authentication.AllowedHostsValidator;
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
import com.microsoft.kiota.http.OkHttpRequestAdapter;
import com.microsoft.kiota.serialization.*;
import dev.ainoya.kiota.openai.generated.ApiClient;
import dev.ainoya.kiota.openai.generated.models.*;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;import java.net.URI;
import java.util.List;
import java.util.Map;class ExampleBearerTokenProvider implements AccessTokenProvider {
// https://learn.microsoft.com/en-us/openapi/kiota/authentication?tabs=java@NotNull
@Override
public String getAuthorizationToken(@NotNull URI uri, @Nullable Map additionalAuthenticationContext) {
// get token from env variable "OPENAI_API_KEY"
return System.getenv("OPENAI_API_KEY");
}@NotNull
@Override
public AllowedHostsValidator getAllowedHostsValidator() {
return new AllowedHostsValidator(
"openai.com"
);
}
}public class ExampleApp {
public static void main(String[] args) {
final BaseBearerTokenAuthenticationProvider authProvider = new BaseBearerTokenAuthenticationProvider(new ExampleBearerTokenProvider());HttpLoggingInterceptor logging = new HttpLoggingInterceptor().setLevel(
HttpLoggingInterceptor.Level.BASIC
// if set level to BODY, kiota client will not work because of the response body is consumed by the interceptor
);Call.Factory httpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(logging)
.build();ParseNodeFactory parseNodeFactory = ParseNodeFactoryRegistry.defaultInstance;
SerializationWriterFactory serializationWriterFactory = SerializationWriterFactoryRegistry.defaultInstance;
final OkHttpRequestAdapter requestAdapter = new OkHttpRequestAdapter(authProvider,
null,
null,
httpClient
);ApiClient client = new ApiClient(requestAdapter);
final CreateChatCompletionRequest request = getCreateChatCompletionRequest();
try {
CreateChatCompletionResponse post = client
.chat().completions().post(request);// debug response
if (post != null) {
var choices = post.getChoices();
if (choices != null) {
for (var choice : choices) {
if (choice.getMessage() != null) {
System.out.println(choice.getMessage().getContent());
}
}
} else {
System.out.println("choices is null");
}
} else {
System.out.println("post is null");
}
} catch (ApiException e) {
// handle as ApiException
System.out.println(e.getLocalizedMessage());
}}
@NotNull
private static CreateChatCompletionRequest getCreateChatCompletionRequest() {
final CreateChatCompletionRequest request = new CreateChatCompletionRequest();final CreateChatCompletionRequest.CreateChatCompletionRequestModel model = new CreateChatCompletionRequest.CreateChatCompletionRequestModel();
model.setString("gpt-4-turbo-preview");request.setModel(
model
);request.setMaxTokens(100);
ChatCompletionRequestMessage message = new ChatCompletionRequestMessage();
ChatCompletionRequestUserMessage userMessage = new ChatCompletionRequestUserMessage();ChatCompletionRequestMessageContentPart contentPart = new ChatCompletionRequestMessageContentPart();
ChatCompletionRequestMessageContentPartText partText = new ChatCompletionRequestMessageContentPartText();
partText.setText("What is the meaning of life?");
partText.setType(ChatCompletionRequestMessageContentPartTextType.Text);contentPart.setChatCompletionRequestMessageContentPartText(
partText
);userMessage.setContent(List.of(
contentPart
));userMessage.setRole(
ChatCompletionRequestUserMessageRole.User
);message.setChatCompletionRequestUserMessage(userMessage);
List messages = List.of(
message
);request.setMessages(
messages
);
return request;
}
}
```## References
- [OpenAI API](https://beta.openai.com/docs/)
- [Kiota](https://github.com/microsoft/kiota)
- [ainoya/openai\-java\-generated\-client](https://github.com/ainoya/openai-java-generated-client): In Comparison with this kiota Client, `openai-java-generated-client` is generated by openapi-generator.