Introduction to liboai
liboai
is an unofficial C++17 library designed to simplify the process of interacting with the OpenAI API. It provides developers with a straightforward way to access OpenAI's various endpoints through a collection of easy-to-use methods and classes. This library is essentially a C++ adaptation of OpenAI's official Python library, openai
, sharing similar structures with a few exceptions.
Features
liboai
offers a range of features that cover many of OpenAI's capabilities:
- ChatGPT: Enables the integration of conversational agents.
- Audio: Provides functionalities for handling audio data.
- Azure: Supports integration with Azure services.
- Functions: Allows users to create and manage functions through OpenAI's infrastructure.
- Image DALL·E: Facilitates the generation of images using DALL·E.
- Models: Offers tools for managing and using different AI models.
- Completions: Supports generating text completions based on prompts.
- Edit: Allows for editing tasks on text content.
- Embeddings: Provides tools for creating and using text embeddings.
- Files: Manages file uploads and operations.
- Fine-tunes: Supports customization and tuning of models.
- Moderation: Includes features for content moderation.
- Asynchronous Support: Provides non-blocking operations to improve performance.
Usage
To demonstrate how liboai
parallels its Python counterpart, consider generating an image with DALL-E. Below are examples in both Python and C++:
Python Example:
import openai
import os
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Image.create(
prompt="A snake in the grass!",
n=1,
size="256x256"
)
print(response["data"][0]["url"])
C++ Example:
#include "liboai.h"
using namespace liboai;
int main() {
OpenAI oai;
oai.auth.SetKeyEnv("OPENAI_API_KEY");
Response res = oai.Image->create(
"A snake in the grass!",
1,
"256x256"
);
std::cout << res["data"][0]["url"] << std::endl;
}
Both snippets illustrate how simple it is to generate an image URL using a text prompt with liboai
. Users should consult the library's documentation for more comprehensive examples that handle exceptions appropriately.
Dependencies
liboai
relies on two major dependencies:
- nlohmann-json for handling JSON data.
- cURL for network requests.
When building the library using the provided solution, it is recommended to install these dependencies via vcpkg to streamline the setup process.
Documentation
For detailed documentation and further code examples, users are encouraged to visit the library’s documentation page.
Contributing
Artificial intelligence is a rapidly evolving field. Those interested in contributing to liboai
can do so by submitting new code and features via a Pull Request. Any issues or feature suggestions can be communicated directly through the GitHub profile or by opening an Issue.
All contributions should target the dev branch for testing before merging into the main branch.