Skip to content

Http client wrapped around the OkHttp3 library, providing an easy way to access OpenAI API interface

License

Notifications You must be signed in to change notification settings

renjfk/openai-client

 
 

Repository files navigation

OpenAI Client

Java http client wrapped around the OkHttp3 library, providing an easy way to access OpenAI api interface.

Contributions welcome Maven

Installation

To use the package, you need to use following Maven dependency:

<dependency>
    <groupId>ee.carlrobert</groupId>
    <artifactId>openai-client</artifactId>
    <version>1.0.14</version>
</dependency>

Gradle dependency:

dependencies {
  implementation("ee.carlrobert:openai-client:1.0.14")
}

Usage

Client Builder

OpenAIClient.Builder builder = new OpenAIClient.Builder(System.getenv("MY_SECRET_KEY"))
    .setConnectTimeout(60L, TimeUnit.SECONDS)
    .setReadTimeout(30L, TimeUnit.SECONDS)
    .setProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1234)))
    .setProxyAuthenticator(new ProxyAuthenticator("proxyUsername", "proxyPassword"));

Dashboard client

DashboardClient dashboardClient = clientBuilder.buildDashboardClient();
dashboardClient.getSubscriptionAsync(subscription -> {
  System.out.println(subscription.getAccountName());
});

Chat Completion SSE

ChatCompletionClient completionClient = clientBuilder.buildChatCompletionClient();
ChatCompletionRequest request = new ChatCompletionRequest.Builder(List.of(new ChatCompletionMessage("user", "Hi there!")))
    .setModel(ChatCompletionModel.GPT_4)
    .build();
EventSource call = completionClient.stream(request, new CompletionEventListener() {
  @Override
  public void onMessage(String message) {
    System.out.println(message);
  }
});
call.cancel();

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This is an unofficial OpenAI library and is not associated with or endorsed by OpenAI.

MIT © Carl-Robert Linnupuu

About

Http client wrapped around the OkHttp3 library, providing an easy way to access OpenAI API interface

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 95.1%
  • Kotlin 4.9%