Skip to content

Commit 9a867f6

Browse files
committed
feat: add examples
Signed-off-by: Xin Liu <[email protected]>
1 parent c88da67 commit 9a867f6

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

Cargo.toml

+23
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,26 @@ image = []
2929
[package.metadata.docs.rs]
3030
all-features = true
3131
rustdoc-args = ["--cfg", "docsrs"]
32+
33+
[[example]]
34+
name = "chat"
35+
path = "examples/chat.rs"
36+
crate-type = ["bin"]
37+
38+
[[example]]
39+
name = "transcribe_audio"
40+
path = "examples/transcribe_audio.rs"
41+
crate-type = ["bin"]
42+
required-features = ["audio"]
43+
44+
[[example]]
45+
name = "translate_audio"
46+
path = "examples/translate_audio.rs"
47+
crate-type = ["bin"]
48+
required-features = ["audio"]
49+
50+
[[example]]
51+
name = "create_image"
52+
path = "examples/create_image.rs"
53+
crate-type = ["bin"]
54+
required-features = ["image"]

examples/chat.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use endpoints::chat::{
2+
ChatCompletionRequestMessage, ChatCompletionSystemMessage, ChatCompletionUserMessage,
3+
ChatCompletionUserMessageContent,
4+
};
5+
use llamaedge::{params::ChatParams, Client};
6+
7+
#[tokio::main]
8+
async fn main() {
9+
const SERVER_BASE_URL: &str = "http://localhost:8080";
10+
11+
// Create a client
12+
let client = Client::new(SERVER_BASE_URL).unwrap();
13+
14+
// create messages
15+
let mut messages = Vec::new();
16+
let system_message = ChatCompletionRequestMessage::System(ChatCompletionSystemMessage::new(
17+
"You are a helpful assistant. Answer questions as concisely and accurately as possible.",
18+
None,
19+
));
20+
messages.push(system_message);
21+
let user_message = ChatCompletionRequestMessage::User(ChatCompletionUserMessage::new(
22+
ChatCompletionUserMessageContent::Text("What is the capital of France?".to_string()),
23+
None,
24+
));
25+
messages.push(user_message);
26+
27+
// send chat completion request
28+
if let Ok(generation) = client.chat(&messages[..], &ChatParams::default()).await {
29+
println!("AI response: {}", generation);
30+
}
31+
}

examples/create_image.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use llamaedge::{params::ImageCreateParams, Client};
2+
3+
#[tokio::main]
4+
async fn main() {
5+
const SERVER_BASE_URL: &str = "http://localhost:8080";
6+
7+
let client = Client::new(SERVER_BASE_URL).unwrap();
8+
9+
let image_object_vec = match client
10+
.create_image("A lovely dog", ImageCreateParams::default())
11+
.await
12+
{
13+
Ok(image_object_vec) => image_object_vec,
14+
Err(e) => {
15+
println!("Error: {}", e);
16+
return;
17+
}
18+
};
19+
20+
println!("{:?}", image_object_vec[0]);
21+
}

examples/transcribe_audio.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use llamaedge::{params::TranscriptionParams, Client};
2+
3+
#[tokio::main]
4+
async fn main() {
5+
const SERVER_BASE_URL: &str = "http://localhost:8080";
6+
7+
let client = Client::new(SERVER_BASE_URL).unwrap();
8+
9+
let transcription_object = match client
10+
.transcribe(
11+
"tests/assets/test.wav",
12+
"en",
13+
TranscriptionParams::default(),
14+
)
15+
.await
16+
{
17+
Ok(to) => to,
18+
Err(e) => {
19+
println!("Error: {}", e);
20+
return;
21+
}
22+
};
23+
24+
println!("{}", transcription_object.text);
25+
}

examples/translate_audio.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use llamaedge::{params::TranslationParams, Client};
2+
3+
#[tokio::main]
4+
async fn main() {
5+
const SERVER_BASE_URL: &str = "http://localhost:8080";
6+
7+
let client = Client::new(SERVER_BASE_URL).unwrap();
8+
9+
let translation_object = match client
10+
.translate(
11+
"tests/assets/test_zh.wav",
12+
"zh",
13+
TranslationParams::default(),
14+
)
15+
.await
16+
{
17+
Ok(to) => to,
18+
Err(e) => {
19+
println!("Error: {}", e);
20+
return;
21+
}
22+
};
23+
24+
println!("{}", translation_object.text);
25+
}

0 commit comments

Comments
 (0)