openai_api_rust/lib.rs
1//! Pull parser for [CommonMark](https://commonmark.org). This crate provides a [Parser](struct.Parser.html) struct
2//! which is an iterator over [Event](enum.Event.html)s. This iterator can be used
3//! directly, or to output HTML using the [HTML module](html/index.html).
4//!
5//! By default, only CommonMark features are enabled. To use extensions like tables,
6//! footnotes or task lists, enable them by setting the corresponding flags in the
7//! [Options](struct.Options.html) struct.
8//!
9//! # Example
10//! ```rust
11//! use openai_api_rust::*;
12//! use openai_api_rust::chat::*;
13//! use openai_api_rust::completions::*;
14//!
15//! fn main() {
16//! // Load API key from environment OPENAI_API_KEY.
17//! // You can also hadcode through `Auth::new(<your_api_key>)`, but it is not recommended.
18//! let auth = Auth::from_env().unwrap();
19//! let openai = OpenAI::new(auth, "https://api.openai.com/v1/");
20//! let body = ChatBody {
21//! model: "gpt-3.5-turbo".to_string(),
22//! max_tokens: Some(7),
23//! temperature: Some(0_f32),
24//! top_p: Some(0_f32),
25//! n: Some(2),
26//! stream: Some(false),
27//! stop: None,
28//! presence_penalty: None,
29//! frequency_penalty: None,
30//! logit_bias: None,
31//! user: None,
32//! messages: vec![Message { role: Role::User, content: "Hello!".to_string() }],
33//! };
34//! let rs = openai.chat_completion_create(&body);
35//! let choice = rs.unwrap().choices;
36//! let message = &choice[0].message.as_ref().unwrap();
37//! assert!(message.content.contains("Hello"));
38//! }
39//! ```
40//!
41//! ## Use proxy
42//!
43//! ```rust
44//! // Load proxy from env
45//! let openai = OpenAI::new(auth, "https://api.openai.com/v1/")
46//! .use_env_proxy();
47//!
48//! // Set the proxy manually
49//! let openai = OpenAI::new(auth, "https://api.openai.com/v1/")
50//! .set_proxy("http://127.0.0.1:1080");
51//! ```
52
53#![warn(unused_crate_dependencies)]
54
55pub mod apis;
56use std::fmt::{self, Display, Formatter};
57
58pub use apis::*;
59pub mod openai;
60pub use openai::*;
61mod mpart;
62mod requests;
63
64use log as _;
65
66pub type Json = serde_json::Value;
67pub type ApiResult<T> = Result<T, Error>;
68
69#[derive(Debug)]
70pub enum Error {
71 /// An Error returned by the API
72 ApiError(String),
73 /// An Error not related to the API
74 RequestError(String),
75}
76
77impl Display for Error {
78 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
79 match self {
80 Error::ApiError(msg) => write!(f, "API error: {}", msg),
81 Error::RequestError(msg) => write!(f, "Request error: {}", msg),
82 }
83 }
84}