feat: add `config.organization_id` (#77)

pull/79/head
sigoden 1 year ago committed by GitHub
parent 66a73c7b84
commit acddece520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -153,12 +153,18 @@ impl ChatGptClient {
.and_then(|m| m.insert("stream".into(), json!(true))); .and_then(|m| m.insert("stream".into(), json!(true)));
} }
let builder = self let (api_key, organization_id) = self.config.read().get_api_key();
let mut builder = self
.build_client()? .build_client()?
.post(API_URL) .post(API_URL)
.bearer_auth(self.config.read().get_api_key()) .bearer_auth(api_key)
.json(&body); .json(&body);
if let Some(organization_id) = organization_id {
builder = builder.header("OpenAI-Organization", organization_id);
}
Ok(builder) Ok(builder)
} }
} }

@ -32,8 +32,7 @@ const CONFIG_FILE_NAME: &str = "config.yaml";
const ROLES_FILE_NAME: &str = "roles.yaml"; const ROLES_FILE_NAME: &str = "roles.yaml";
const HISTORY_FILE_NAME: &str = "history.txt"; const HISTORY_FILE_NAME: &str = "history.txt";
const MESSAGE_FILE_NAME: &str = "messages.md"; const MESSAGE_FILE_NAME: &str = "messages.md";
const SET_COMPLETIONS: [&str; 9] = [ const SET_COMPLETIONS: [&str; 8] = [
".set api_key",
".set temperature", ".set temperature",
".set save true", ".set save true",
".set save false", ".set save false",
@ -49,6 +48,8 @@ const SET_COMPLETIONS: [&str; 9] = [
pub struct Config { pub struct Config {
/// Openai api key /// Openai api key
pub api_key: Option<String>, pub api_key: Option<String>,
/// Openai organization id
pub organization_id: Option<String>,
/// Openai model /// Openai model
#[serde(rename(serialize = "model", deserialize = "model"))] #[serde(rename(serialize = "model", deserialize = "model"))]
pub model_name: Option<String>, pub model_name: Option<String>,
@ -85,6 +86,7 @@ impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
api_key: None, api_key: None,
organization_id: None,
model_name: None, model_name: None,
temperature: None, temperature: None,
save: false, save: false,
@ -201,8 +203,10 @@ impl Config {
Self::local_file(CONFIG_FILE_NAME) Self::local_file(CONFIG_FILE_NAME)
} }
pub fn get_api_key(&self) -> &String { pub fn get_api_key(&self) -> (String, Option<String>) {
self.api_key.as_ref().expect("api_key not set") let api_key = self.api_key.as_ref().expect("api_key not set");
let organization_id = self.organization_id.as_ref();
(api_key.into(), organization_id.cloned())
} }
pub fn roles_file() -> Result<PathBuf> { pub fn roles_file() -> Result<PathBuf> {
@ -324,11 +328,14 @@ impl Config {
.temperature .temperature
.map(|v| v.to_string()) .map(|v| v.to_string())
.unwrap_or("-".into()); .unwrap_or("-".into());
let (api_key, organization_id) = self.get_api_key();
let organization_id = organization_id.unwrap_or("-".into());
let items = vec![ let items = vec![
("config_file", file_info(&Config::config_file()?)), ("config_file", file_info(&Config::config_file()?)),
("roles_file", file_info(&Config::roles_file()?)), ("roles_file", file_info(&Config::roles_file()?)),
("messages_file", file_info(&Config::messages_file()?)), ("messages_file", file_info(&Config::messages_file()?)),
("api_key", self.get_api_key().to_string()), ("api_key", api_key),
("organization_id", organization_id),
("model", self.model.0.to_string()), ("model", self.model.0.to_string()),
("temperature", temperature), ("temperature", temperature),
("save", self.save.to_string()), ("save", self.save.to_string()),
@ -367,13 +374,6 @@ impl Config {
let value = parts[1]; let value = parts[1];
let unset = value == "null"; let unset = value == "null";
match key { match key {
"api_key" => {
if unset {
bail!("Error: Not allowed");
} else {
self.api_key = Some(value.to_string());
}
}
"temperature" => { "temperature" => {
if unset { if unset {
self.temperature = None; self.temperature = None;

Loading…
Cancel
Save