From c83d398ce0c52df3968b65c5e5a63e5e85e20602 Mon Sep 17 00:00:00 2001 From: Benjamin Sherriff Date: Sun, 12 May 2024 17:52:22 -0400 Subject: [PATCH] enum variant matching for oai --- README.md | 5 ++--- service/.env | 2 +- service/src/bot/oai/model.rs | 36 +++++++++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f5e35f6..906f2e7 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,8 @@ The following packages must be installed for [serenity-rs/songbird](https://gith ### 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: ``` diff --git a/service/.env b/service/.env index 43cb6b1..f815d8a 100644 --- a/service/.env +++ b/service/.env @@ -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 diff --git a/service/src/bot/oai/model.rs b/service/src/bot/oai/model.rs index f9181c7..91fab69 100644 --- a/service/src/bot/oai/model.rs +++ b/service/src/bot/oai/model.rs @@ -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, + // created: i64, + // model: String, + // usage: Usage, + // choices: Vec, + // }, + // ResponseError { + // error: Option, + // message: Option, + // param: Option, + // #[serde(rename = "type")] + // error_type: Option, + // }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -119,13 +136,18 @@ impl OAI { match response { Ok(response) => { let value = response.json::().await?; - // let event: ResponseEvent = serde_json::from_value::(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::(value)?; - return Ok(res); + let event: ResponseEvent = serde_json::from_value::(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 {