OpenAI
The OpenAI library provides convenient access to the OpenAI REST API from any Rust application. This library aims to follow the implementation of the official OpenAI SDKs for Python and Node.js as closely as possible. However, it is unofficial and not maintained by OpenAI.
Note: This project is still under development, should not be used in production, and many aspects of the structure may change.
Documentation
The REST API documentation can be found on platform.openai.com. The full API of this library can be found in docs.rs.
Installation
Add this to your Cargo.toml:
[]
= "0.1.5"
Usage
The full API of this library can be found in the documentation.
use ;
use ;
async
While you can provide an api_key directly, we recommend using environment variables to keep your API key secure.
Examples
Refer to the examples directory for usage examples.
OpenAI Assistant Beta
The Crab AI library includes support for the OpenAI Assistant API, which is currently in beta. This feature allows you to create and manage threads, messages, and runs with the Assistant. The Assistant API is designed to help build conversational agents that can interact with users in a more dynamic and context-aware manner.
Example: Creating a Thread and a Message
Below is an example demonstrating how to create a thread and a message within that thread using the Assistant API:
let thread = openai.beta.threads.create.await?;
let message = openai.beta.threads.messages.create.await?;
This example showcases how to initialize a conversation with a thread and add a message to it.
Creating a Run and Polling for Completion
Here's an example of how to create a run using the Assistant API and poll until it reaches a terminal state:
let run = openai.beta.threads.runs.create_and_poll.await?;
if run.status == Completed
By integrating these features, the open_ai library provides a robust interface for utilizing the latest capabilities of the OpenAI API, including the Assistant API currently in beta.