Skip to content

Commit

Permalink
chore: remove unnecessary OpenAI trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevries3133 committed Dec 12, 2023
1 parent 7930830 commit ebc3d9b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
6 changes: 2 additions & 4 deletions src/count_chat/counter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The core calorie counting feature (models, components, and controllers
//! are colocated here).

use super::openai::{OpenAI, OpenAITr};
use super::openai::OpenAI;
use crate::{components::Component, errors::ServerError, routes::Route};
use axum::{extract::Form, response::IntoResponse};
use serde::Deserialize;
Expand Down Expand Up @@ -38,8 +38,6 @@ pub async fn handle_chat(
) -> Result<impl IntoResponse, ServerError> {
let mut msg = String::from("The meal I'd like a calorie estimate for is ");
msg.push_str(&chat);
let response = OpenAI::from_env()?
.send_message(SYSTEM_MSG.into(), msg)
.await?;
let response = OpenAI::from_env()?.send_message(SYSTEM_MSG.into(), msg)?;
Ok(response)
}
19 changes: 3 additions & 16 deletions src/count_chat/openai.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use async_trait::async_trait;
use openai_api_rs::v1::{
api::Client,
chat_completion::{
Expand All @@ -12,27 +11,15 @@ use std::env;
pub struct OpenAI {
client: Client,
}
#[async_trait]
pub trait OpenAITr {
fn from_env() -> Result<Self>
where
Self: Sized;
async fn send_message(
&self,
system_msg: String,
usr_msg: String,
) -> Result<String>;
}

#[async_trait]
impl OpenAITr for OpenAI {
fn from_env() -> Result<Self> {
impl OpenAI {
pub fn from_env() -> Result<Self> {
let api_key = env::var("OPENAI_API_KEY")?;
Ok(Self {
client: Client::new(api_key),
})
}
async fn send_message(
pub fn send_message(
&self,
system_msg: String,
usr_msg: String,
Expand Down

0 comments on commit ebc3d9b

Please sign in to comment.