Skip to content

Commit

Permalink
Merge pull request #27 from guchengxi1994/master
Browse files Browse the repository at this point in the history
chore
  • Loading branch information
guchengxi1994 authored May 5, 2024
2 parents 30a8ce8 + 9bce063 commit 348b804
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
.env
.env
env
2 changes: 1 addition & 1 deletion lib/common/dev_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
2 changes: 1 addition & 1 deletion lib/src/rust/api/llm_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}) =>
Expand Down
6 changes: 3 additions & 3 deletions lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<LLMMessage> llmMessageStream({dynamic hint});

Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
3 changes: 2 additions & 1 deletion rust/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
*.png
.env
.env
env
7 changes: 0 additions & 7 deletions rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion rust/src/api/llm_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};

#[frb(sync)]
pub fn init_llm(p: Option<String>) {
pub fn init_llm(p: String) {
crate::llm::init(p)
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn wire_init_llm_impl(
};
let mut deserializer =
flutter_rust_bridge::for_generated::SseDeserializer::new(message);
let api_p = <Option<String>>::sse_decode(&mut deserializer);
let api_p = <String>::sse_decode(&mut deserializer);
deserializer.end();
transform_result_sse((move || {
Result::<_, ()>::Ok(crate::api::llm_api::init_llm(api_p))
Expand Down
55 changes: 41 additions & 14 deletions rust/src/llm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -45,20 +47,25 @@ pub struct EnvParams {
pub sk: Option<String>,
}

pub fn init(p: Option<String>) {
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();
Expand Down Expand Up @@ -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<HashMap<String, String>> {
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::<Vec<_>>();
if sep.len() == 2 {
res.insert(
sep.first().unwrap().trim().to_owned(),
sep.last().unwrap().trim().to_owned(),
);
}
}

anyhow::Ok(res)
}
48 changes: 26 additions & 22 deletions rust/src/llm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests {
use std::io::Write;

use dotenv::dotenv;
use futures::StreamExt;
use langchain_rust::{
chain::{builder::ConversationalChainBuilder, Chain, LLMChainBuilder},
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions windows/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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
)

0 comments on commit 348b804

Please sign in to comment.