pub struct Chat<'c, P: Provider, H: HttpClient> { /* private fields */ }
Implementations§
Source§impl<'c, P, H> Chat<'c, P, H>where
P: Provider,
H: HttpClient,
impl<'c, P, H> Chat<'c, P, H>where
P: Provider,
H: HttpClient,
pub fn new(client: &'c Client<P, H>) -> Self
Sourcepub async fn create<T>(&self, request: T) -> Result<P::ChatResponse, Error>
pub async fn create<T>(&self, request: T) -> Result<P::ChatResponse, Error>
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(())
}
pub async fn create_stream<T>( &self, request: T, ) -> Result<Pin<Box<dyn Stream<Item = Result<P::ChatResponseStream, Error>> + Send>>, Error>
Trait Implementations§
Auto Trait Implementations§
impl<'c, P, H> Freeze for Chat<'c, P, H>
impl<'c, P, H> RefUnwindSafe for Chat<'c, P, H>where
P: RefUnwindSafe,
H: RefUnwindSafe,
impl<'c, P, H> Send for Chat<'c, P, H>
impl<'c, P, H> Sync for Chat<'c, P, H>
impl<'c, P, H> Unpin for Chat<'c, P, H>
impl<'c, P, H> UnwindSafe for Chat<'c, P, H>where
P: RefUnwindSafe,
H: RefUnwindSafe,
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
Mutably borrows from an owned value. Read more