diff --git a/.gitignore b/.gitignore index d1ebabc..ac4a115 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ app.*.map.json /android/app/debug /android/app/profile /android/app/release -.env \ No newline at end of file +.env +env \ No newline at end of file diff --git a/lib/common/dev_utils.dart b/lib/common/dev_utils.dart index 9853faf..810ab14 100644 --- a/lib/common/dev_utils.dart +++ b/lib/common/dev_utils.dart @@ -6,5 +6,5 @@ class DevUtils { static Directory get executableDir => File(Platform.resolvedExecutable).parent; - static String env = "${DevUtils.executableDir.path}/.env"; + static String env = "${DevUtils.executableDir.path}/env"; } diff --git a/lib/src/rust/api/llm_api.dart b/lib/src/rust/api/llm_api.dart index 9df133a..06089d5 100644 --- a/lib/src/rust/api/llm_api.dart +++ b/lib/src/rust/api/llm_api.dart @@ -7,7 +7,7 @@ import '../frb_generated.dart'; import '../llm.dart'; import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; -void initLlm({String? p, dynamic hint}) => +void initLlm({required String p, dynamic hint}) => RustLib.instance.api.initLlm(p: p, hint: hint); EnvParams? getLlmConfig({dynamic hint}) => diff --git a/lib/src/rust/frb_generated.dart b/lib/src/rust/frb_generated.dart index e280a09..cd5a349 100644 --- a/lib/src/rust/frb_generated.dart +++ b/lib/src/rust/frb_generated.dart @@ -81,7 +81,7 @@ abstract class RustLibApi extends BaseApi { EnvParams? getLlmConfig({dynamic hint}); - void initLlm({String? p, dynamic hint}); + void initLlm({required String p, dynamic hint}); Stream llmMessageStream({dynamic hint}); @@ -192,11 +192,11 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { ); @override - void initLlm({String? p, dynamic hint}) { + void initLlm({required String p, dynamic hint}) { return handler.executeSync(SyncTask( callFfi: () { final serializer = SseSerializer(generalizedFrbRustBinding); - sse_encode_opt_String(p, serializer); + sse_encode_String(p, serializer); return pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 1)!; }, codec: SseCodec( diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index a67a9c9..7f57917 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -149,14 +149,14 @@ message(STATUS "P1 ==> " ${P1}) string(REPLACE "/build/linux/x64" "" ROOT_DIR ${P1}) message(STATUS "ROOT_DIR ==> " ${ROOT_DIR}) -set(ENV_PATH ${ROOT_DIR}/.env) +set(ENV_PATH ${ROOT_DIR}/env) if(NOT EXISTS ${ENV_PATH}) set(MULTILINE_TEXT - "LLM_BASE =\" your api here \" -LLM_MODEL_NAME =\" your model name here \" -LLM_SK =\" your api key here \" -CHAT_CHAT_BASE = \" your chatchat api here \" + "LLM_BASE = your api here +LLM_MODEL_NAME = your model name here +LLM_SK = your api key here +CHAT_CHAT_BASE = your chatchat api here " ) file(WRITE ${ENV_PATH} ${MULTILINE_TEXT}) diff --git a/rust/.gitignore b/rust/.gitignore index f3f2071..227eb1d 100644 --- a/rust/.gitignore +++ b/rust/.gitignore @@ -1,3 +1,4 @@ /target *.png -.env \ No newline at end of file +.env +env \ No newline at end of file diff --git a/rust/Cargo.lock b/rust/Cargo.lock index efd23c2..7b63ef4 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1867,12 +1867,6 @@ dependencies = [ "syn 2.0.60", ] -[[package]] -name = "dotenv" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" - [[package]] name = "downcast-rs" version = "1.2.0" @@ -5749,7 +5743,6 @@ version = "0.1.0" dependencies = [ "anyhow", "cron-job", - "dotenv", "flutter_rust_bridge", "futures", "langchain-rust", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 8def54e..fc757f6 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["cdylib", "staticlib"] [dependencies] anyhow = "1" cron-job = "=0.1.4" -dotenv = "0.15.0" +# dotenv = "0.15.0" flutter_rust_bridge = "=2.0.0-dev.31" futures = "0.3.30" langchain-rust = "4.1.0" diff --git a/rust/src/api/llm_api.rs b/rust/src/api/llm_api.rs index 54b21a4..f2be243 100644 --- a/rust/src/api/llm_api.rs +++ b/rust/src/api/llm_api.rs @@ -6,7 +6,7 @@ use crate::{ }; #[frb(sync)] -pub fn init_llm(p: Option) { +pub fn init_llm(p: String) { crate::llm::init(p) } diff --git a/rust/src/frb_generated.rs b/rust/src/frb_generated.rs index 2dec015..f6fbc5a 100644 --- a/rust/src/frb_generated.rs +++ b/rust/src/frb_generated.rs @@ -128,7 +128,7 @@ fn wire_init_llm_impl( }; let mut deserializer = flutter_rust_bridge::for_generated::SseDeserializer::new(message); - let api_p = >::sse_decode(&mut deserializer); + let api_p = ::sse_decode(&mut deserializer); deserializer.end(); transform_result_sse((move || { Result::<_, ()>::Ok(crate::api::llm_api::init_llm(api_p)) diff --git a/rust/src/llm/mod.rs b/rust/src/llm/mod.rs index 322a127..28b40d2 100644 --- a/rust/src/llm/mod.rs +++ b/rust/src/llm/mod.rs @@ -2,7 +2,6 @@ pub mod models; pub mod sequential_chain_builder; mod tests; -use dotenv::dotenv; use futures::StreamExt; use langchain_rust::chain::Chain; use langchain_rust::language_models::llm::LLM; @@ -12,6 +11,9 @@ use langchain_rust::{ schemas::Message, }; use once_cell::sync::Lazy; +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader}; use std::sync::RwLock; use crate::frb_generated::StreamSink; @@ -45,20 +47,25 @@ pub struct EnvParams { pub sk: Option, } -pub fn init(p: Option) { - match p { - Some(_p) => { - let _ = dotenv::from_path(_p); - } - None => { - dotenv().ok(); - } - } +pub fn init(p: String) { + // match p { + // Some(_p) => { + // let _ = dotenv::from_path(_p); + // } + // None => { + // dotenv().ok(); + // } + // } + + let map = crate::llm::env_parse(p).unwrap(); - let base = std::env::var("LLM_BASE").unwrap_or("".to_owned()); - let name = std::env::var("LLM_MODEL_NAME").unwrap_or("".to_owned()); - let chat_base = std::env::var("CHAT_CHAT_BASE").unwrap_or("".to_owned()); - let sk = std::env::var("LLM_SK").unwrap_or("".to_owned()); + let base = map.get("LLM_BASE").unwrap().to_string(); + let name = map.get("LLM_MODEL_NAME").unwrap().to_string(); + let sk = map.get("LLM_SK").unwrap_or(&"".to_string()).to_string(); + let chat_base = map + .get("CHAT_CHAT_BASE") + .unwrap_or(&"".to_string()) + .to_string(); { let mut r = ENV_PARAMS.write().unwrap(); @@ -247,3 +254,23 @@ pub async fn sequential_chain_chat(json_str: String, query: String) -> anyhow::R anyhow::Ok(()) } + +pub fn env_parse(path: String) -> anyhow::Result> { + let mut res = HashMap::new(); + let file = File::open(path)?; + let reader = BufReader::new(file); + let lines = reader.lines(); + + for i in lines { + let _line = i?; + let sep = _line.split("=").collect::>(); + if sep.len() == 2 { + res.insert( + sep.first().unwrap().trim().to_owned(), + sep.last().unwrap().trim().to_owned(), + ); + } + } + + anyhow::Ok(res) +} diff --git a/rust/src/llm/tests.rs b/rust/src/llm/tests.rs index 77baa60..f826263 100644 --- a/rust/src/llm/tests.rs +++ b/rust/src/llm/tests.rs @@ -2,7 +2,6 @@ mod tests { use std::io::Write; - use dotenv::dotenv; use futures::StreamExt; use langchain_rust::{ chain::{builder::ConversationalChainBuilder, Chain, LLMChainBuilder}, @@ -27,13 +26,14 @@ mod tests { #[tokio::test] async fn test() { - dotenv().ok(); + let map = crate::llm::env_parse("env".to_owned()).unwrap(); - let base = std::env::var("LLM_BASE").unwrap(); + let base = map.get("LLM_BASE").unwrap(); println!("base {:?}", base); - let name = std::env::var("LLM_MODEL_NAME").unwrap(); + let name = map.get("LLM_MODEL_NAME").unwrap(); println!("name {:?}", name); - let sk = std::env::var("LLM_SK").unwrap_or("".to_owned()); + let binding = "".to_string(); + let sk = map.get("LLM_SK").unwrap_or(&binding); println!("sk {:?}", sk); let open_ai = OpenAI::default() @@ -46,13 +46,14 @@ mod tests { #[tokio::test] async fn test_stream() { - dotenv().ok(); + let map = crate::llm::env_parse("env".to_owned()).unwrap(); - let base = std::env::var("LLM_BASE").unwrap(); + let base = map.get("LLM_BASE").unwrap(); println!("base {:?}", base); - let name = std::env::var("LLM_MODEL_NAME").unwrap(); + let name = map.get("LLM_MODEL_NAME").unwrap(); + let binding = "".to_string(); println!("name {:?}", name); - let sk = std::env::var("LLM_SK").unwrap_or("".to_owned()); + let sk = map.get("LLM_SK").unwrap_or(&binding); println!("sk {:?}", sk); let open_ai = OpenAI::default() @@ -77,13 +78,14 @@ mod tests { #[tokio::test] async fn test_chain() { - dotenv().ok(); + let map = crate::llm::env_parse("env".to_owned()).unwrap(); - let base = std::env::var("LLM_BASE").unwrap(); + let base = map.get("LLM_BASE").unwrap(); println!("base {:?}", base); - let name = std::env::var("LLM_MODEL_NAME").unwrap(); + let name = map.get("LLM_MODEL_NAME").unwrap(); println!("name {:?}", name); - let sk = std::env::var("LLM_SK").unwrap_or("".to_owned()); + let binding = "".to_string(); + let sk = map.get("LLM_SK").unwrap_or(&binding); println!("sk {:?}", sk); let open_ai = OpenAI::default() @@ -144,13 +146,14 @@ mod tests { #[tokio::test] async fn sequential_chain_test() { - dotenv().ok(); - - let base = std::env::var("LLM_BASE").unwrap(); + let map = crate::llm::env_parse("env".to_owned()).unwrap(); + + let base = map.get("LLM_BASE").unwrap(); println!("base {:?}", base); - let name = std::env::var("LLM_MODEL_NAME").unwrap(); + let name = map.get("LLM_MODEL_NAME").unwrap(); println!("name {:?}", name); - let sk = std::env::var("LLM_SK").unwrap_or("".to_owned()); + let binding = "".to_string(); + let sk = map.get("LLM_SK").unwrap_or(&binding); println!("sk {:?}", sk); let llm = OpenAI::default() @@ -218,13 +221,14 @@ mod tests { } "#; - dotenv().ok(); + let map = crate::llm::env_parse("env".to_owned()).unwrap(); - let base = std::env::var("LLM_BASE").unwrap(); + let base = map.get("LLM_BASE").unwrap(); println!("base {:?}", base); - let name = std::env::var("LLM_MODEL_NAME").unwrap(); + let name = map.get("LLM_MODEL_NAME").unwrap(); println!("name {:?}", name); - let sk = std::env::var("LLM_SK").unwrap_or("".to_owned()); + let binding = "".to_string(); + let sk = map.get("LLM_SK").unwrap_or(&binding); println!("sk {:?}", sk); let llm = OpenAI::default() diff --git a/windows/runner/CMakeLists.txt b/windows/runner/CMakeLists.txt index 2401b8e..00f7593 100644 --- a/windows/runner/CMakeLists.txt +++ b/windows/runner/CMakeLists.txt @@ -48,14 +48,14 @@ string(REPLACE "/build/windows/x64" "" ROOT_DIR ${P1}) message(STATUS "ROOT_DIR ==> " ${ROOT_DIR}) # create .env if .env not exists -set(ENV_PATH ${ROOT_DIR}/.env) +set(ENV_PATH ${ROOT_DIR}/env) if(NOT EXISTS ${ENV_PATH}) set(MULTILINE_TEXT - "LLM_BASE =\" your api here \" -LLM_MODEL_NAME =\" your model name here \" -LLM_SK =\" your api key here \" -CHAT_CHAT_BASE = \" your chatchat api here \" + "LLM_BASE = your api here +LLM_MODEL_NAME = your model name here +LLM_SK = your api key here +CHAT_CHAT_BASE = your chatchat api here " ) file(WRITE ${ENV_PATH} ${MULTILINE_TEXT}) @@ -65,7 +65,7 @@ else() endif() add_custom_target(copy-runtime-files ALL - COMMAND ${CMAKE_COMMAND} -E copy ${ENV_PATH} ${P1}/runner/Debug/.env - COMMAND ${CMAKE_COMMAND} -E copy ${ENV_PATH} ${P1}/runner/Release/.env - COMMAND ${CMAKE_COMMAND} -E copy ${ENV_PATH} ${P1}/runner/Profile/.env + COMMAND ${CMAKE_COMMAND} -E copy ${ENV_PATH} ${P1}/runner/Debug/env + COMMAND ${CMAKE_COMMAND} -E copy ${ENV_PATH} ${P1}/runner/Release/env + COMMAND ${CMAKE_COMMAND} -E copy ${ENV_PATH} ${P1}/runner/Profile/env )