Skip to content

Commit

Permalink
Merge pull request #313 from moxin-org/dev
Browse files Browse the repository at this point in the history
Main update 20 nov
  • Loading branch information
noxware authored Nov 20, 2024
2 parents f33bf57 + b01857b commit a82d297
Show file tree
Hide file tree
Showing 40 changed files with 564 additions and 516 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,3 @@ jobs:
cd dist/
$file=Get-ChildItem -Filter *.exe
gh release upload ${{ github.event.inputs.release_tags }} $file.name --clobber
64 changes: 32 additions & 32 deletions Cargo.lock

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

68 changes: 49 additions & 19 deletions moly-backend/src/backend_impls/api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,12 @@ impl BackendModel for LLamaEdgeApiServer {
let (wasm_module, listen_addr) = if let Some(old_model) = &old_model {
let listen_addr = load_model_options.override_server_address.clone().map_or(
old_model.listen_addr,
|addr| {
match std::net::TcpListener::bind(&addr) {
Ok(listener) => listener.local_addr().unwrap(),
Err(_) => {
eprintln!("Failed to start the model on address {}", addr);
eprintln!("Using the previous one {}", old_model.listen_addr);
old_model.listen_addr
}
|addr| match std::net::TcpListener::bind(&addr) {
Ok(listener) => listener.local_addr().unwrap(),
Err(_) => {
eprintln!("Failed to start the model on address {}", addr);
eprintln!("Using the previous one {}", old_model.listen_addr);
old_model.listen_addr
}
},
);
Expand All @@ -205,10 +203,23 @@ impl BackendModel for LLamaEdgeApiServer {
(old_model.wasm_module.clone(), listen_addr)
} else {
let addr = std::env::var("MOLY_API_SERVER_ADDR").unwrap_or("localhost:0".to_string());
let new_addr = std::net::TcpListener::bind(&addr)
.unwrap()
.local_addr()
.unwrap();

let listen_addr = load_model_options
.override_server_address
.clone()
.map(|addr| match std::net::TcpListener::bind(&addr) {
Ok(listener) => Some(listener.local_addr().unwrap()),
Err(_) => None,
})
.flatten();

let new_addr = match listen_addr {
Some(addr) => addr,
None => {
let listener = std::net::TcpListener::bind(&addr).unwrap();
listener.local_addr().unwrap()
}
};

(Module::from_bytes(None, WASM).unwrap(), new_addr)
};
Expand Down Expand Up @@ -312,15 +323,24 @@ impl BackendModel for LLamaEdgeApiServer {

async_rt.spawn(async move {
let request_body = serde_json::to_string(&data).unwrap();
let resp = reqwest::ClientBuilder::new()
let request = reqwest::ClientBuilder::new()
.no_proxy()
.build()
.unwrap()
.post(url)
.body(request_body)
.send()
.await
.map_err(|e| anyhow!(e));
.body(request_body);

let resp = tokio::select! {
res = request.send() => Some(res.map_err(|e| anyhow!(e))),
_ = cancel.recv() => None,
};

let Some(resp) = resp else {
let _ = tx.send(Ok(ChatResponse::ChatResponseChunk(stop_chunk(
StopReason::Stop,
))));
return;
};

match resp {
Ok(resp) => {
Expand Down Expand Up @@ -351,8 +371,18 @@ impl BackendModel for LLamaEdgeApiServer {
StopReason::Stop,
))));
} else {
let resp: Result<ChatResponseData, anyhow::Error> =
resp.json().await.map_err(|e| anyhow!(e));
let resp = tokio::select! {
res = resp.json::<ChatResponseData>() => Some(res.map_err(|e| anyhow!(e))),
_ = cancel.recv() => None,
};

let Some(resp) = resp else {
let _ = tx.send(Ok(ChatResponse::ChatResponseChunk(stop_chunk(
StopReason::Stop,
))));
return;
};

let _ = tx.send(resp.map(ChatResponse::ChatFinalResponseData));
}
}
Expand Down
Loading

0 comments on commit a82d297

Please sign in to comment.