async_llm::client

Struct Client

Source
pub struct Client<P: Provider, H: HttpClient> { /* private fields */ }

Implementations§

Source§

impl<P: Provider> Client<P, DefaultHttpClient<P::Config>>

Source

pub fn with_provider(provider: P) -> Self

Source§

impl<P: Provider, H: HttpClient> Client<P, H>

Source

pub fn with_args(provider: P, http_client: H) -> Self

Source

pub fn provider(&self) -> &P

Source

pub fn http_client(&self) -> &H

Source§

impl Client<OpenAIProvider, DefaultHttpClient<OpenAIConfig>>

Source

pub fn new() -> Self

Source

pub fn with_auth( base_url: impl Into<String>, api_key: Option<SecretString>, ) -> Self

Source§

impl Client<RawProvider, DefaultHttpClient<OpenAIConfig>>

Source

pub fn raw() -> Self

Source

pub fn with_auth_raw( base_url: impl Into<String>, api_key: Option<SecretString>, ) -> Self

Examples found in repository?
examples/generate.rs (lines 205-208)
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
fn create_client(
    env_base_url: &str,
    env_api_key: &str,
) -> Option<Client<RawProvider, DefaultHttpClient<OpenAIConfig>>> {
    match std::env::var(env_api_key) {
        Err(e) => {
            tracing::error!("Error: {e} - {env_api_key}");
            None
        }
        Ok(api_key) => match std::env::var(env_base_url) {
            Ok(base_url) => Some(Client::with_auth_raw(
                base_url,
                Some(SecretString::new(api_key.into())),
            )),
            Err(e) => {
                tracing::error!("Error: {e} - {env_base_url}");
                None
            }
        },
    }
}
Source§

impl<P: Provider, H: HttpClient> Client<P, H>

Source

pub fn completions(&self) -> Completions<'_, P, H>

Source

pub fn chat(&self) -> Chat<'_, P, H>

Examples found in repository?
examples/generate.rs (line 139)
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
async fn generate<H: HttpClient>(
    client: &Option<Client<RawProvider, H>>,
    provider_name: impl Into<String>,
    model_name: impl Into<String>,
    test_name: impl Into<String>,
    prompt: impl Into<String>,
) -> Result<(), Error> {
    let test_name: String = test_name.into();
    let provider_name: String = provider_name.into();
    let model_name: String = model_name.into();
    match client {
        None => tracing::debug!(
            "Skip {}/{}/{} because client is None",
            provider_name,
            model_name,
            test_name
        ),
        Some(client) => {
            let request = ChatRequest::new(&model_name, vec![ChatMessage::user(prompt)]);
            let request = serde_json::to_value(request)?;
            tracing::debug!("Sending request: {:?}", request);
            let response = client.chat().create(request.clone()).await?;

            let provider_model_name =
                format!("{}_{}", provider_name, sanitize_folder_name(&model_name));

            let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
            d.push("data");
            d.push(&test_name);
            d.push(&provider_model_name);
            let output_path = d
                .to_str()
                .map(ToString::to_string)
                .ok_or(Error::InvalidArgument(format!(
                    "Failed to get path for data/{}/{}",
                    test_name, provider_model_name
                )))?;
            match fs::create_dir_all(&output_path) {
                Err(e) => tracing::error!("Failed to create folder: {:?}", e),
                Ok(_) => {
                    tracing::info!("Successfully created folder: {:?}", output_path);
                    // info.json
                    let mut info_path = PathBuf::from_str(&output_path).unwrap();
                    info_path.push("info.json");
                    let info = json!({
                        "provider_name": provider_name,
                        "test_name": test_name,
                        "model_name": model_name
                    });
                    match save_json_to_file(&info, &info_path) {
                        Ok(_) => tracing::info!("Succesfully created file: {:?}", info_path),
                        Err(e) => tracing::error!("Failed to create file: {:?}", e),
                    }

                    // request.json
                    let mut request_path = PathBuf::from_str(&output_path).unwrap();
                    request_path.push("request.json");
                    match save_json_to_file(&request, &request_path) {
                        Ok(_) => tracing::info!("Succesfully created file: {:?}", request_path),
                        Err(e) => tracing::error!("Failed to create file: {:?}", e),
                    }

                    // response.json
                    let mut response_path = PathBuf::from_str(&output_path).unwrap();
                    response_path.push("response.json");
                    match save_json_to_file(&response, &response_path) {
                        Ok(_) => tracing::info!("Succesfully created file: {:?}", response_path),
                        Err(e) => tracing::error!("Failed to create file: {:?}", e),
                    }
                }
            }
        }
    }

    Ok(())
}

Trait Implementations§

Source§

impl<P: Clone + Provider, H: Clone + HttpClient> Clone for Client<P, H>

Source§

fn clone(&self) -> Client<P, H>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug + Provider, H: Debug + HttpClient> Debug for Client<P, H>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, H> Freeze for Client<P, H>
where P: Freeze, H: Freeze,

§

impl<P, H> RefUnwindSafe for Client<P, H>

§

impl<P, H> Send for Client<P, H>

§

impl<P, H> Sync for Client<P, H>

§

impl<P, H> Unpin for Client<P, H>
where P: Unpin, H: Unpin,

§

impl<P, H> UnwindSafe for Client<P, H>
where P: UnwindSafe, H: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

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

Source§

impl<T> MaybeSendSync for T