openai-java  by openai

Java SDK for OpenAI API access

created 9 months ago
973 stars

Top 38.7% on sourcepulse

GitHubView on GitHub
Project Summary

This Java library provides a convenient interface to the OpenAI REST API, targeting Java developers building applications that leverage AI models. It simplifies interaction with OpenAI's services, enabling features like text generation, file uploads, and streaming responses.

How It Works

The SDK utilizes an OkHttp-based client for making HTTP requests to the OpenAI API. It maps API endpoints to Java methods, deserializing JSON responses into immutable Java objects with associated builders for easy construction. Both synchronous and asynchronous operations are supported, with asynchronous calls returning CompletableFuture instances. Streaming responses are handled via StreamResponse and AsyncStreamResponse for chunked data processing.

Quick Start & Requirements

  • Installation: Add the com.openai:openai-java dependency to your Gradle or Maven project.
  • Requirements: Java 8 or later.
  • Configuration: API keys and other credentials can be configured via environment variables (OPENAI_API_KEY, OPENAI_ORG_ID, OPENAI_PROJECT_ID, OPENAI_BASE_URL) or directly in code.
  • Examples: See the openai-java-example directory for runnable examples.

Highlighted Details

  • Supports both standard and chat completions APIs.
  • Provides synchronous, asynchronous, and streaming API access.
  • Includes utilities for file uploads and handling binary/raw responses.
  • Offers robust error handling with specific exception types for API errors.
  • Features auto-pagination for handling large result sets.
  • Supports Azure OpenAI integration with Azure-specific credential configuration.

Maintenance & Community

The library is officially maintained by OpenAI. Further community engagement details are not explicitly listed in the README.

Licensing & Compatibility

The library is released under the MIT License, permitting commercial use and integration with closed-source projects.

Limitations & Caveats

The README notes that while the library generally follows Semantic Versioning, some minor versions might contain backward-incompatible changes to internal library components not intended for public use. Users relying on these internals are advised to open a GitHub issue. Disabling Jackson version compatibility checks is not guaranteed to work correctly.

Health Check
Last commit

2 days ago

Responsiveness

1 day

Pull Requests (30d)
28
Issues (30d)
33
Star History
223 stars in the last 90 days

Explore Similar Projects

Feedback? Help us improve.