Skip to content

Commit

Permalink
Rebrand to Persona
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisTM committed Oct 22, 2024
1 parent d9df77b commit 463aab2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/commands/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use serenity::builder::CreateCommand;
use serenity::model::application::ResolvedOption;
use serenity::prelude::RwLock;

use crate::god::God;
use crate::persona::Persona;

pub async fn run(ctx: &Context, command: &CommandInteraction, god: Arc<RwLock<God>>) {
pub async fn run(ctx: &Context, command: &CommandInteraction, god: Arc<RwLock<Persona>>) {
let author_name = if let Some(global_name) = &command.user.global_name {
global_name.clone()
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use serenity::all::{CommandInteraction, Context, CreateInteractionResponseMessag
use serenity::builder::CreateCommand;
use serenity::prelude::RwLock;

use crate::god::God;
use crate::persona::Persona;

pub async fn run(ctx: &Context, command: &CommandInteraction, god: Arc<RwLock<God>>) {
pub async fn run(ctx: &Context, command: &CommandInteraction, god: Arc<RwLock<Persona>>) {
god.write().await.clear();
if let Err(why) = command
.create_response(
Expand Down
22 changes: 11 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::Parser;

pub mod commands;
pub mod god;
pub mod ollama;
pub mod persona;

use god::{God, GodConfig, GodNursery};
use persona::{Nursery, Persona, PersonaConfig};

use serenity::all::Interaction;
use serenity::gateway::ActivityData;
Expand All @@ -25,20 +25,20 @@ fn get_name<T>(_: T) -> String {
std::any::type_name::<T>().to_string()
}

async fn get_or_create_bot(ctx: &Context, key: u64) -> Arc<RwLock<God>> {
async fn get_or_create_bot(ctx: &Context, key: u64) -> Arc<RwLock<Persona>> {
let data = ctx.data.read().await;
let nursery = data
.get::<GodNursery>()
.get::<Nursery>()
.expect("There should be a nursery here.");

let default_god = data
.get::<GodConfig>()
.get::<PersonaConfig>()
.expect("There should be a default config in the context.");

let has_bot = nursery.read().await.contains_key(&key);

if !has_bot {
let new_god = God::from_config(default_god.clone());
let new_god = Persona::from_config(default_god.clone());
let mut write_nursery = nursery.write().await;
write_nursery.insert(key, Arc::new(RwLock::new(new_god)));
}
Expand Down Expand Up @@ -74,7 +74,7 @@ impl EventHandler for Handler {

async fn message(&self, ctx: Context, msg: Message) {
// This is only used for private messages
if msg.guild_id == None {
if msg.guild_id.is_none() {
return;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ impl EventHandler for Handler {

let data = ctx.data.read().await;
let config = data
.get::<GodConfig>()
.get::<PersonaConfig>()
.expect("There should be god configuration.");

let guild_commands = ctx
Expand Down Expand Up @@ -155,7 +155,7 @@ async fn main() {

let god_data: String = fs::read_to_string(&args.god)
.unwrap_or_else(|_| panic!("The god {:?} file must be readable.", &args.god));
let config = match serde_json::from_str::<GodConfig>(&god_data) {
let config = match serde_json::from_str::<PersonaConfig>(&god_data) {
Ok(config) => Some(config),
Err(err) => {
println!("Parsing failed: {err}");
Expand All @@ -176,8 +176,8 @@ async fn main() {

{
let mut data = client.data.write().await;
data.insert::<GodConfig>(config);
data.insert::<GodNursery>(RwLock::new(HashMap::default()));
data.insert::<PersonaConfig>(config);
data.insert::<Nursery>(RwLock::new(HashMap::default()));
}

if let Err(why) = client.start().await {
Expand Down
34 changes: 17 additions & 17 deletions src/god.rs → src/persona.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ use std::sync::Arc;

const MAX_RECOLLECTIONS: usize = 20;

// The nursery allows to find the god we are interested in, in all those servers
pub struct GodNursery;
impl TypeMapKey for GodNursery {
type Value = RwLock<HashMap<u64, Arc<RwLock<God>>>>;
// The nursery allows to find the persona we are interested in, in all those servers
pub struct Nursery;
impl TypeMapKey for Nursery {
type Value = RwLock<HashMap<u64, Arc<RwLock<Persona>>>>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GodConfig {
pub struct PersonaConfig {
pub model: String,
pub botname: String,
pub options: GenerationOptions,
}

impl TypeMapKey for GodConfig {
type Value = GodConfig;
impl TypeMapKey for PersonaConfig {
type Value = PersonaConfig;
}

impl Default for GodConfig {
impl Default for PersonaConfig {
fn default() -> Self {
let options = GenerationOptions::default()
.num_ctx(4096)
Expand All @@ -50,21 +50,21 @@ impl Default for GodConfig {

// trait Bot, for God
#[derive(Debug)]
pub struct God {
pub struct Persona {
pub brain: OllamaAI,
pub config: GodConfig,
pub config: PersonaConfig,
// The actual live memory of the bot.
recollections: Vec<ChatMessage>,
}

impl Default for God {
impl Default for Persona {
fn default() -> Self {
let config = GodConfig::default();
let config = PersonaConfig::default();
Self::from_config(config)
}
}

impl God {
impl Persona {
pub fn get_prompt(&self, author: &str, prompt: &str) -> Vec<ChatMessage> {
let mut prompts = self.recollections.clone();
prompts.push(ChatMessage::user(format!("{author}: {prompt}").to_owned()));
Expand Down Expand Up @@ -97,15 +97,15 @@ impl God {
self.recollections.clear();
}

pub fn from_config(config: GodConfig) -> God {
God {
pub fn from_config(config: PersonaConfig) -> Persona {
Persona {
brain: OllamaAI::new(&config.model, config.options.clone()),
recollections: Vec::new(),
config,
}
}

pub fn update_from_config(&mut self, config: GodConfig) {
pub fn update_from_config(&mut self, config: PersonaConfig) {
self.brain = OllamaAI::new(&config.model, config.options.clone());
self.recollections = Vec::new();
self.config = config;
Expand All @@ -116,7 +116,7 @@ impl God {
}

pub fn import_json(val: &str) -> Option<Self> {
if let Ok(config) = serde_json::from_str::<GodConfig>(val) {
if let Ok(config) = serde_json::from_str::<PersonaConfig>(val) {
Some(Self::from_config(config))
} else {
None
Expand Down

0 comments on commit 463aab2

Please sign in to comment.