Skip to content

Commit

Permalink
Stage 4
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Jun 19, 2024
1 parent 8f78bcf commit 0439701
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 163 deletions.
111 changes: 48 additions & 63 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[package]
authors = ["Xavier Lau <[email protected]>"]
build = "build.rs"
description = "AI with Rust."
edition = "2021"
homepage = "https://hack.ink/air"
Expand All @@ -23,9 +22,10 @@ inherits = "dev"
inherits = "release"
lto = true

[build-dependencies]
# crates.io
vergen = { version = "8.2", features = ["build", "cargo", "git", "gitcl"] }
[features]
default = []
dev = []
tokenizer = ["llm_utils"]

[dependencies]
# crates.io
Expand All @@ -40,15 +40,15 @@ enigo = { version = "0.2" }
futures = { version = "0.3" }
get-selected-text = { version = "0.1" }
global-hotkey = { version = "0.5" }
# TODO?: remove it since it requires libxml2.
llm_utils = { version = "0.0.6" }
llm_utils = { version = "0.0.6", optional = true }
reqwew = { version = "0.2" }
serde = { version = "1.0", features = ["derive"] }
thiserror = { version = "1.0" }
tokio = { version = "1.38", features = ["rt-multi-thread"] }
toml = { version = "0.8" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
tracing-appender = { version = "0.2" }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[target.'cfg(target_os = "macos")'.dependencies]
# accessibility = { version = "0.1" }
Expand Down
1 change: 1 addition & 0 deletions asset/retry.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 0 additions & 19 deletions build.rs

This file was deleted.

29 changes: 21 additions & 8 deletions src/component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// TODO?: refresh trait.

pub mod tokenizer;
use tokenizer::Tokenizer;
#[cfg(feature = "tokenizer")] pub mod tokenizer;
#[cfg(feature = "tokenizer")] use tokenizer::Tokenizer;

pub mod function;

Expand All @@ -21,22 +19,37 @@ pub mod util;

// std
use std::sync::Arc;
// crates.io
use tokio::sync::Mutex;
// self
use crate::prelude::*;

#[derive(Debug)]
pub struct Components {
pub setting: Setting,
#[cfg(feature = "tokenizer")]
pub tokenizer: Tokenizer,
pub openai: Arc<OpenAi>,
pub openai: Arc<Mutex<OpenAi>>,
}
impl Components {
pub fn init() -> Result<Self> {
let setting = Setting::load()?;
#[cfg(feature = "tokenizer")]
let tokenizer = Tokenizer::new(setting.ai.model.as_str());
// TODO: no clone.
let openai = Arc::new(OpenAi::new(setting.ai.clone()));
let openai = Arc::new(Mutex::new(OpenAi::new(setting.ai.clone())));

Ok(Self {
setting,
#[cfg(feature = "tokenizer")]
tokenizer,
openai,
})
}

// TODO?: move to somewhere else.
pub fn reload_openai(&self) {
tracing::info!("reloading openai component");

Ok(Self { setting, tokenizer, openai })
self.openai.blocking_lock().reload(self.setting.ai.clone());
}
}
15 changes: 6 additions & 9 deletions src/component/openai.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// std
use std::sync::Arc;
// crates.io
use async_openai::{
config::OpenAIConfig,
Expand All @@ -11,27 +9,26 @@ use async_openai::{
};
use eframe::egui::WidgetText;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock as TokioRwLock;
// self
use super::setting::Ai;
use crate::prelude::*;

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct OpenAi {
pub client: Arc<TokioRwLock<Client<OpenAIConfig>>>,
pub client: Client<OpenAIConfig>,
pub setting: Ai,
}
impl OpenAi {
pub fn new(setting: Ai) -> Self {
let client = Arc::new(TokioRwLock::new(Client::with_config(
let client = Client::with_config(
OpenAIConfig::new().with_api_base(&setting.api_base).with_api_key(&setting.api_key),
)));
);

Self { client, setting }
}

pub fn reload(&mut self, setting: Ai) {
*self.client.blocking_write() = Client::with_config(
self.client = Client::with_config(
OpenAIConfig::new().with_api_base(&setting.api_base).with_api_key(&setting.api_key),
);
self.setting = setting;
Expand All @@ -48,7 +45,7 @@ impl OpenAi {
.max_tokens(4_096_u16)
.messages(&msg)
.build()?;
let stream = self.client.read().await.chat().create_stream(req).await?;
let stream = self.client.chat().create_stream(req).await?;

Ok(stream)
}
Expand Down
9 changes: 3 additions & 6 deletions src/component/setting.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// std
use std::{fs, path::PathBuf};
// crates.io
use app_dirs2::{AppDataType, AppInfo};
use app_dirs2::AppDataType;
use async_openai::config::OPENAI_API_BASE;
use eframe::egui::WidgetText;
use serde::{Deserialize, Serialize};
// self
use super::openai::Model;
use crate::prelude::*;

const APP: AppInfo = AppInfo { name: "AiR", author: "[email protected]" };
use crate::{prelude::*, APP_INFO};

#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand All @@ -20,7 +18,7 @@ pub struct Setting {
}
impl Setting {
pub fn path() -> Result<PathBuf> {
Ok(app_dirs2::get_app_root(AppDataType::UserConfig, &APP).map(|p| p.join(".airrc"))?)
Ok(app_dirs2::get_app_root(AppDataType::UserConfig, &APP_INFO).map(|p| p.join(".airrc"))?)
}

pub fn load() -> Result<Self> {
Expand Down Expand Up @@ -57,7 +55,6 @@ impl Default for General {
}
}

// TODO: support Google Gemini.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Ai {
Expand Down
Loading

0 comments on commit 0439701

Please sign in to comment.