OpenAI Client
Java http client wrapped around the OkHttp3 library, providing an easy way to access OpenAI api interface.
Installation
To use the package, you need to use following Maven dependency:
<dependency>
<groupId>ee.carlrobert</groupId>
<artifactId>openai-client</artifactId>
<version>1.0.10</version>
</dependency>
Gradle dependency:
dependencies {
implementation("ee.carlrobert:openai-client:1.0.10")
}
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".
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
This is an unofficial OpenAI library and is not associated with or endorsed by OpenAI.
MIT © Carl-Robert Linnupuu