Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl ChatGptClient {
"messages": messages,
});

if let Some(v) = self.config.borrow().temperature {
if let Some(v) = self.config.borrow().get_temperature() {
body.as_object_mut()
.and_then(|m| m.insert("temperature".into(), json!(v)));
}
Expand Down
30 changes: 26 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use std::{

use anyhow::{anyhow, Context, Result};
use inquire::{Confirm, Text};
use serde::Deserialize;
use serde::{Deserialize, Serialize};

use crate::utils::now;
use crate::utils::{emphasis, now};

const CONFIG_FILE_NAME: &str = "config.yaml";
const ROLES_FILE_NAME: &str = "roles.yaml";
Expand Down Expand Up @@ -153,7 +153,19 @@ impl Config {
pub fn change_role(&mut self, name: &str) -> String {
match self.find_role(name) {
Some(role) => {
let output = format!("{}>> {}", role.name, role.prompt.trim());
let temperature = match role.temperature {
Some(v) => format!("{v}"),
None => "null".into(),
};
let output = format!(
"{}: {}\n{}: {}\n{}: {}",
emphasis("name"),
role.name,
emphasis("prompt"),
role.prompt.trim(),
emphasis("temperature"),
temperature
);
self.role = Some(role);
output
}
Expand All @@ -165,6 +177,7 @@ impl Config {
self.role = Some(Role {
name: TEMP_ROLE_NAME.into(),
prompt: prompt.into(),
temperature: self.temperature,
});
}

Expand All @@ -178,6 +191,13 @@ impl Config {
})
}

pub fn get_temperature(&self) -> Option<f64> {
self.role
.as_ref()
.and_then(|v| v.temperature)
.or(self.temperature)
}

pub fn merge_prompt(&self, content: &str) -> String {
match self.get_prompt() {
Some(prompt) => format!("{}\n{content}", prompt.trim()),
Expand Down Expand Up @@ -305,12 +325,14 @@ impl Config {
}
}

#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Role {
/// Role name
pub name: String,
/// Prompt text send to ai for setting up a role
pub prompt: String,
/// What sampling temperature to use, between 0 and 2
pub temperature: Option<f64>,
}

fn create_config_file(config_path: &Path) -> Result<()> {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::prelude::*;
use crossterm::style::{Color, Stylize};
use std::io::{stdout, Write};

#[macro_export]
Expand All @@ -17,3 +18,7 @@ pub fn now() -> String {
let now = Local::now();
now.to_rfc3339_opts(SecondsFormat::Secs, false)
}

pub fn emphasis(text: &str) -> String {
text.stylize().with(Color::White).to_string()
}