-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_claude_chat_stream.rs
89 lines (76 loc) · 2.69 KB
/
demo_claude_chat_stream.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use futures::TryStreamExt;
use crate::bedrock::model_info::{ModelInfo, ModelName};
use crate::bedrock::models::claude::claude_client::{ClaudeClient, ClaudeOptions};
use crate::bedrock::models::claude::claude_request_message::ConversationRequest;
use crate::bedrock::models::claude::claude_request_message::Message;
use crate::bedrock::models::claude::claude_request_message::{ChatOptions, StreamResultData};
pub async fn demo_chat_claude_with_stream() {
let claude_options = ClaudeOptions::new()
.profile_name("bedrock")
.region("us-west-2");
let client = ClaudeClient::new(claude_options).await;
let mut conversation_request = ConversationRequest::default();
conversation_request
.messages
.push(Message::new_user_message(
"What is the capital of France ?".to_owned(),
));
println!(
"{}",
serde_json::to_string_pretty(&conversation_request).unwrap()
);
let chat_options = ChatOptions::default()
.with_model_id(ModelInfo::from_model_name(
ModelName::AnthropicClaudeOpus1x,
))
.with_temperature(0.5);
let response_stream = client
.chat_with_stream(&conversation_request, &chat_options)
.await;
let response_stream = match response_stream {
Ok(response_stream) => response_stream,
Err(e) => {
println!("Error: {:?}", e);
return;
}
};
// consumme the stream and print the response
response_stream
.try_for_each(|chunk| async move {
display_streamresult_data(chunk);
Ok(())
})
.await
.unwrap();
}
fn display_streamresult_data(data: StreamResultData) {
match data {
StreamResultData::ContentBlockStart(content_block_start) => {
println!("ContentBlockStart: {:?}", content_block_start);
}
StreamResultData::ContentBlockStop(content_block_end) => {
println!("ContentBlockEnd: {:?}", content_block_end);
}
StreamResultData::MessageStart(message_start) => {
println!("MessageStart: {:?}", message_start);
}
StreamResultData::MessageStop(message_end) => {
println!("MessageStop: {:?}", message_end);
}
StreamResultData::MessageDelta(message_delta) => {
println!("MessageDelta: {:?}", message_delta);
}
StreamResultData::ContentBlockDelta(content_block_delta) => {
println!("ContentBlockDelta: {:?}", content_block_delta);
}
}
}
// Test
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_demo_chat_claude_with_stream() {
demo_chat_claude_with_stream().await;
}
}