Struct OpenAi

Source
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

Source

pub fn new() -> Result<Self>

コンストラクタ。

Source

pub async fn model_info(&mut self) -> Result<ModelInfo>

Source

pub fn model_info_offline(&self) -> OfflineModelInfo

Source

pub async fn model_info_online(&mut self) -> Result<OnlineModelInfo>

Source

pub fn get_output_reserved_token(&self) -> usize

出力用に予約するトークン数を計算する。 基本的に max_output_tokens に余裕を持たせた値を使うが、 それが意味をなしていない旧モデルの場合は context_window のうち一定割合とする。

Source

async fn get_online_model_info(&self) -> Result<Model>

Source

fn check_enabled(&self) -> Result<()>

設定で無効になっていたら警告をログに出しつつ Err を返す。

Source

pub fn error_kind(err: &Error) -> OpenAiErrorKind

エラーチェインの中から特定のエラーを探す。

Source

fn log_header(resp: &Response, key: &str)

Source

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

Source

async fn post_json_text( &mut self, url: &str, body: &(impl Serialize + Debug), ) -> Result<String>

Self::post_json の結果を文字列として返す。 HTTP エラーも含めてエラーにする。

Source

async fn post_json_bin( &mut self, url: &str, body: &(impl Serialize + Debug), ) -> Result<Vec<u8>>

Self::post_json の結果をバイナリとして返す。 HTTP エラーも含めてエラーにする。

Source

pub fn get_expected_rate_limit(&self) -> Option<ExpectedRateLimit>

Source

pub async fn chat( &mut self, instructions: Option<&str>, input: Vec<InputItem>, ) -> Result<ResponseObject>

OpenAI Reponse API を使用する。

Source

pub async fn chat_with_tools( &mut self, instructions: Option<&str>, input: Vec<InputItem>, tools: &[Tool], ) -> Result<ResponseObject>

OpenAI Reponse API を使用する。

Source

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 まで縮小してからエンコードする。

Source

pub async fn generate_image( &mut self, prompt: &str, n: u8, ) -> Result<Vec<String>>

OpenAI Image Generation API を使用する。

Source

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

Source§

fn on_start(&mut self, _ctrl: &Control)

SystemModule の初期化時には [crate::sys::taskserver::TaskServer] がまだ存在しないので タスクの登録はこのタイミングまで遅延させる。

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,