pub struct OpenAi {
config: OpenAiConfig,
client: Client,
model_name: &'static str,
model_info_offline: OfflineModelInfo,
model_info_online: Option<CachedModelInfo>,
rate_limit: Option<RateLimit>,
}
Expand description
OpenAI システムモジュール。
Fields§
§config: OpenAiConfig
§client: Client
§model_name: &'static str
§model_info_offline: OfflineModelInfo
§model_info_online: Option<CachedModelInfo>
§rate_limit: Option<RateLimit>
Implementations§
Source§impl OpenAi
impl OpenAi
pub async fn model_info(&mut self) -> Result<ModelInfo>
pub fn model_info_offline(&self) -> OfflineModelInfo
pub async fn model_info_online(&mut self) -> Result<OnlineModelInfo>
Sourcepub fn get_output_reserved_token(&self) -> usize
pub fn get_output_reserved_token(&self) -> usize
出力用に予約するトークン数を計算する。 基本的に max_output_tokens に余裕を持たせた値を使うが、 それが意味をなしていない旧モデルの場合は context_window のうち一定割合とする。
async fn get_online_model_info(&self) -> Result<Model>
Sourcefn check_enabled(&self) -> Result<()>
fn check_enabled(&self) -> Result<()>
設定で無効になっていたら警告をログに出しつつ Err を返す。
Sourcepub fn error_kind(err: &Error) -> OpenAiErrorKind
pub fn error_kind(err: &Error) -> OpenAiErrorKind
エラーチェインの中から特定のエラーを探す。
fn log_header(resp: &Response, key: &str)
Sourceasync fn post_json(
&mut self,
url: &str,
body: &(impl Serialize + Debug),
) -> Result<Response>
async fn post_json( &mut self, url: &str, body: &(impl Serialize + Debug), ) -> Result<Response>
JSON を POST して [Response] を返す。 成功しても HTTP ステータスコードは失敗かもしれない。
HTTP Header に付与されるメタ情報をログに記録する。 レートリミット情報は後で参照できるように保存する。
https://platform.openai.com/docs/api-reference/debugging-requests
Sourceasync fn post_json_text(
&mut self,
url: &str,
body: &(impl Serialize + Debug),
) -> Result<String>
async fn post_json_text( &mut self, url: &str, body: &(impl Serialize + Debug), ) -> Result<String>
Self::post_json の結果を文字列として返す。 HTTP エラーも含めてエラーにする。
Sourceasync fn post_json_bin(
&mut self,
url: &str,
body: &(impl Serialize + Debug),
) -> Result<Vec<u8>>
async fn post_json_bin( &mut self, url: &str, body: &(impl Serialize + Debug), ) -> Result<Vec<u8>>
Self::post_json の結果をバイナリとして返す。 HTTP エラーも含めてエラーにする。
pub fn get_expected_rate_limit(&self) -> Option<ExpectedRateLimit>
Sourcepub async fn chat(
&mut self,
instructions: Option<&str>,
input: Vec<InputItem>,
) -> Result<ResponseObject>
pub async fn chat( &mut self, instructions: Option<&str>, input: Vec<InputItem>, ) -> Result<ResponseObject>
OpenAI Reponse API を使用する。
Sourcepub async fn chat_with_tools(
&mut self,
instructions: Option<&str>,
input: Vec<InputItem>,
tools: &[Tool],
) -> Result<ResponseObject>
pub async fn chat_with_tools( &mut self, instructions: Option<&str>, input: Vec<InputItem>, tools: &[Tool], ) -> Result<ResponseObject>
OpenAI Reponse API を使用する。
Sourcepub fn to_image_input(bin: &[u8]) -> Result<InputContent>
pub fn to_image_input(bin: &[u8]) -> Result<InputContent>
OpenAI Image Input に適した形式に変換する。
https://platform.openai.com/docs/guides/images-vision?api-mode=responses#image-input-requirements
Supported file types
- PNG (.png)
- JPEG (.jpeg and .jpg)
- WEBP (.webp)
- Non-animated GIF (.gif)
Size limits
- Up to 20MB per image
- Low-resolution: 512px x 512px
- High-resolution: 768px (short side) x 2000px (long side)
image/png;base64 文字列として保持する。 detail=Low だと OpenAI 側で 512x512 まで縮小されるらしいが、 こちらのメモリ消費と送信時のネットワーク帯域が無駄なので ここで 512 まで縮小してからエンコードする。
Sourcepub async fn generate_image(
&mut self,
prompt: &str,
n: u8,
) -> Result<Vec<String>>
pub async fn generate_image( &mut self, prompt: &str, n: u8, ) -> Result<Vec<String>>
OpenAI Image Generation API を使用する。
Sourcepub async fn text_to_speech(
&mut self,
model: SpeechModel,
input: &str,
voice: SpeechVoice,
response_format: Option<SpeechFormat>,
speed: Option<f32>,
) -> Result<Vec<u8>>
pub async fn text_to_speech( &mut self, model: SpeechModel, input: &str, voice: SpeechVoice, response_format: Option<SpeechFormat>, speed: Option<f32>, ) -> Result<Vec<u8>>
OpenAI Create Speech API を使用する。
Trait Implementations§
Source§impl SystemModule for OpenAi
impl SystemModule for OpenAi
Source§fn on_start(&mut self, _ctrl: &Control)
fn on_start(&mut self, _ctrl: &Control)
Auto Trait Implementations§
impl Freeze for OpenAi
impl !RefUnwindSafe for OpenAi
impl Send for OpenAi
impl Sync for OpenAi
impl Unpin for OpenAi
impl !UnwindSafe for OpenAi
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more