enum variant matching for oai

This commit is contained in:
Benjamin Sherriff
2024-05-12 17:52:22 -04:00
parent 1de68f86ae
commit c83d398ce0
3 changed files with 32 additions and 11 deletions

View File

@@ -71,9 +71,26 @@ pub struct Choice {
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
enum ResponseEvent {
ChatCompletionResponse(ChatCompletionResponse),
ResponseError(ResponseError),
// ChatCompletionResponse {
// id: String,
// object: String,
// system_fingerprint: Option<String>,
// created: i64,
// model: String,
// usage: Usage,
// choices: Vec<Choice>,
// },
// ResponseError {
// error: Option<ErrorDetails>,
// message: Option<String>,
// param: Option<String>,
// #[serde(rename = "type")]
// error_type: Option<String>,
// },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -119,13 +136,18 @@ impl OAI {
match response {
Ok(response) => {
let value = response.json::<Value>().await?;
// let event: ResponseEvent = serde_json::from_value::<ResponseEvent>(value)?;
// match event {
// ResponseEvent::ChatCompletionResponse(response) => return Ok(response),
// ResponseEvent::ResponseError(error) => return Err(ServiceError { status: 500, message: format!("Error: {}", error.message.unwrap()) })
// }
let res = serde_json::from_value::<ChatCompletionResponse>(value)?;
return Ok(res);
let event: ResponseEvent = serde_json::from_value::<ResponseEvent>(value)?;
match event {
ResponseEvent::ChatCompletionResponse(response) => {
return Ok(response);
},
ResponseEvent::ResponseError(error) => {
return Err(ServiceError {
status: 500,
message: format!("Error: {}", error.message.unwrap()),
});
},
}
}
Err(err) => {
return Err(ServiceError {