Store messages into table, required docker container to create table
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
use diesel::{prelude::*, PgConnection, insert_into};
|
||||
use diesel::r2d2::{Pool, ConnectionManager};
|
||||
use log::{error, debug, trace};
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
@@ -9,10 +11,12 @@ use serde_json::Value;
|
||||
use serenity::model::channel::Message;
|
||||
use serenity::prelude::*;
|
||||
|
||||
use crate::database::models::NewMessageDB;
|
||||
|
||||
pub struct OAI {
|
||||
pub client: reqwest::Client,
|
||||
pub base_url: String,
|
||||
pub max_attempts: u64,
|
||||
pub max_attempts: i64,
|
||||
pub token: String
|
||||
}
|
||||
|
||||
@@ -29,7 +33,7 @@ struct ChatCompletionRequest {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
n: Option<f64>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
max_tokens: Option<u64>,
|
||||
max_tokens: Option<i64>,
|
||||
/// Value between -2.0 and 2.0
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
presence_penalty: Option<f64>,
|
||||
@@ -62,7 +66,7 @@ enum Role {
|
||||
struct ChatCompletionResponse {
|
||||
id: String,
|
||||
object: String,
|
||||
created: u64,
|
||||
created: i64,
|
||||
model: String,
|
||||
usage: Usage,
|
||||
choices: Vec<Choice>
|
||||
@@ -70,21 +74,21 @@ struct ChatCompletionResponse {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct Usage {
|
||||
prompt_tokens: u64,
|
||||
completion_tokens: u64,
|
||||
total_tokens: u64
|
||||
prompt_tokens: i64,
|
||||
completion_tokens: i64,
|
||||
total_tokens: i64
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct Choice {
|
||||
message: ChatCompletionMessage,
|
||||
finish_reason: String,
|
||||
index: u64
|
||||
index: i64
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct ResponseError {
|
||||
code: Option<u64>,
|
||||
code: Option<i64>,
|
||||
message: Option<String>,
|
||||
param: Option<String>,
|
||||
#[serde(rename = "type")]
|
||||
@@ -114,7 +118,7 @@ impl OAI {
|
||||
async fn get_request(&self, request: ChatCompletionRequest) -> Result<ChatCompletionResponse, OAIError> {
|
||||
let uri = format!("{}/chat/completions", self.base_url);
|
||||
let body = serde_json::to_string(&request).unwrap();
|
||||
trace!("Sending request to {}", uri);
|
||||
trace!("Sending request to {}: {}", uri, body);
|
||||
let value = match match self.client
|
||||
.post(&uri)
|
||||
.bearer_auth(&self.token)
|
||||
@@ -135,7 +139,7 @@ impl OAI {
|
||||
})
|
||||
};
|
||||
|
||||
debug!("Received response from OpenAI: {:?}", value);
|
||||
trace!("Received response from OpenAI: {:?}", value);
|
||||
|
||||
// let response = match serde_json::from_value::<OAIResponseEvent>(value) {
|
||||
// Ok(r) => {
|
||||
@@ -159,8 +163,8 @@ impl OAI {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
let bot_mention = format!("<@{}>", ctx.cache.current_user_id().0);
|
||||
pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI, pool: &Pool<ConnectionManager<PgConnection>>) {
|
||||
let bot_mention: String = format!("<@{}>", ctx.cache.current_user_id().0);
|
||||
let parsed_content = msg.content.replace(bot_mention.as_str(), "");
|
||||
debug!("Generating response for message: {}", msg.content);
|
||||
|
||||
@@ -171,12 +175,14 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
},
|
||||
ChatCompletionMessage {
|
||||
role: "user".to_string(),
|
||||
content: parsed_content
|
||||
content: parsed_content.to_string()
|
||||
},
|
||||
];
|
||||
|
||||
let model = "gpt-3.5-turbo".to_string();
|
||||
|
||||
let request = ChatCompletionRequest {
|
||||
model: "gpt-3.5-turbo".to_string(),
|
||||
model: model.to_string(),
|
||||
messages,
|
||||
temperature: None,
|
||||
top_p: None,
|
||||
@@ -190,7 +196,23 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
Ok(r) => {
|
||||
debug!("Received response from OpenAI");
|
||||
if !r.choices.is_empty() {
|
||||
r.choices[0].message.content.clone()
|
||||
let mut connection = pool.get().unwrap();
|
||||
let res = r.choices[0].message.content.clone();
|
||||
if let Err(err) = insert_into(crate::database::schema::messages::table).values(NewMessageDB {
|
||||
id: &r.id,
|
||||
guild_id: msg.guild_id.unwrap().0 as i64,
|
||||
channel_id: msg.channel_id.0 as i64,
|
||||
user_id: msg.author.id.0 as i64,
|
||||
created: r.created,
|
||||
model: &model,
|
||||
request: &parsed_content,
|
||||
response: &res,
|
||||
request_tags: vec![],
|
||||
response_tags: vec![],
|
||||
}).execute(&mut connection) {
|
||||
error!("Could not insert message into database: {}", err);
|
||||
}
|
||||
res
|
||||
} else {
|
||||
"No reply received".to_string()
|
||||
}
|
||||
@@ -200,6 +222,8 @@ pub async fn generate_response(ctx: &Context, msg: &Message, oai: &OAI) {
|
||||
err.message
|
||||
}
|
||||
};
|
||||
debug!("Sending response: \"{}\"", response);
|
||||
|
||||
if let Err(why) = msg.channel_id.say(&ctx.http, response).await {
|
||||
error!("Cannot send message: {}", why);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user