-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding streaming function to mistralrs server. (#986)
* Adding streaming function to mistralrs server. * Adding simple_stream example
- Loading branch information
Showing
3 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use anyhow::Result; | ||
use mistralrs::{ | ||
IsqType, PagedAttentionMetaBuilder, RequestBuilder, Response, TextMessageRole, TextMessages, | ||
TextModelBuilder, | ||
}; | ||
use std::io::Write; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let model = TextModelBuilder::new("microsoft/Phi-3.5-mini-instruct") | ||
.with_isq(IsqType::Q8_0) | ||
.with_logging() | ||
.with_paged_attn(|| PagedAttentionMetaBuilder::default().build())? | ||
.build() | ||
.await?; | ||
|
||
let messages = TextMessages::new() | ||
.add_message( | ||
TextMessageRole::System, | ||
"You are an AI agent with a specialty in programming.", | ||
) | ||
.add_message( | ||
TextMessageRole::User, | ||
"Hello! How are you? Please write generic binary search function in Rust.", | ||
); | ||
|
||
let response = model.send_chat_request(messages).await?; | ||
|
||
println!("{}", response.choices[0].message.content.as_ref().unwrap()); | ||
dbg!( | ||
response.usage.avg_prompt_tok_per_sec, | ||
response.usage.avg_compl_tok_per_sec | ||
); | ||
|
||
// Next example: Return some logprobs with the `RequestBuilder`, which enables higher configurability. | ||
let request = RequestBuilder::new().return_logprobs(true).add_message( | ||
TextMessageRole::User, | ||
"Please write a mathematical equation where a few numbers are added.", | ||
); | ||
|
||
let mut stream = model.stream_chat_request(request).await?; | ||
|
||
let stdout = std::io::stdout(); | ||
let lock = stdout.lock(); | ||
let mut buf = std::io::BufWriter::new(lock); | ||
while let Some(chunk) = stream.next().await { | ||
if let Response::Chunk(chunk) = chunk { | ||
buf.write(chunk.choices[0].delta.content.as_bytes())?; | ||
} else { | ||
// Handle errors | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters