-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish the implementation of stats module
- Loading branch information
Showing
6 changed files
with
213 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use anyhow::Error; | ||
use async_openai::types::{ | ||
ChatCompletionRequestMessage, ChatCompletionResponseMessage, CreateChatCompletionRequestArgs, | ||
}; | ||
use async_openai::Client as OpenAIClient; | ||
|
||
pub(crate) struct ChatModelResult { | ||
pub message: ChatCompletionResponseMessage, | ||
pub token_usage: u32, | ||
} | ||
|
||
pub(crate) fn new_client() -> OpenAIClient { | ||
OpenAIClient::new() | ||
} | ||
|
||
pub(crate) async fn request_chat_model( | ||
client: &OpenAIClient, | ||
msgs: Vec<ChatCompletionRequestMessage>, | ||
) -> Result<ChatModelResult, Error> { | ||
let req = CreateChatCompletionRequestArgs::default() | ||
.model("gpt-3.5-turbo") | ||
.temperature(0.6) | ||
.messages(msgs) | ||
.build()?; | ||
|
||
let resp = client.chat().create(req).await?; | ||
let mut choices = resp.choices; | ||
|
||
if choices.is_empty() { | ||
return Err(anyhow!("Server responds with empty data")); | ||
} | ||
|
||
Ok(ChatModelResult { | ||
message: choices.remove(0).message, | ||
token_usage: resp.usage.map(|u| u.total_tokens).unwrap_or(0), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.