A wrapper for OpenAI API written in Rust
First of all, you need to initialize a client.
```rust use openai_rs::client::Client;
let client = Client::new("Your API key goes here"); ```
Then you can use the methods to interact with the API:
```rust use openai_rs::args::CompletionArgs;
let args = CompletionArgs::new("Say this is a test", Some(32), // Max tokens Some(2), // Amount of completions (default: 1) None, // Suffix (default: "") Some(1.0) // Temperature );
let completion = client.create_completion(&args) .await .unwrap(); ``` This method returns the response returned from the API.
You can do the following to get the content from the response:
```rust use openai_rs::response::Content;
// Get the text data from the response let text = completion.get_content(0) .await .unwrap();
// Print the completion
println!("Completion: {}", text);
``
In this example we use
.unwrap()because
get_contentmethod returns an
Option
If you want to get the actual Response
you can directly get access the resp
field:
rust
// Moves the Response value to the new variable
let response = completion.resp;
rust
// Borrows the Response
let response = &completion.resp;
To see examples of other APIs you can look at the examples here.