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

@@ -68,9 +68,8 @@ The following packages must be installed for [serenity-rs/songbird](https://gith
</details>
### Running Locally
1. Build the [Docker](https://www.docker.com/) containers with `make build`
2. Start the utility containers with `make utils`
3. Start the application with `cargo run`
1. Start the backend containers with `make refresh`
2. Start the application with `make run`
The application can also be tested from within a Docker container:
```

View File

@@ -24,4 +24,4 @@ DATA_DIR_PATH= # OPTIONAL
DISCORD_TOKEN= # OPTIONAL
OPENAI_API_KEY= # OPTIONAL
OPENAI_API_MODEL=gpt-3.5-turbo-0125
OPENAI_API_MODEL=gpt-3.5-turbo

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 {