pub struct Client { /* private fields */ }
Expand description
Client for the LlamaEdge API.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(server_base_url: impl AsRef<str>) -> Result<Self, LlamaEdgeError>
pub fn new(server_base_url: impl AsRef<str>) -> Result<Self, LlamaEdgeError>
Create a new client.
§Arguments
server_base_url
- The base URL of the LlamaEdge API server.
§Returns
A Result
containing the client or an error.
Examples found in repository?
4async fn main() {
5 const SERVER_BASE_URL: &str = "http://localhost:8080";
6
7 let client = Client::new(SERVER_BASE_URL).unwrap();
8
9 match client
10 .embeddings("Hello, world!".into(), EmbeddingsParams::default())
11 .await
12 {
13 Ok(embeddings) => println!("{:#?}", embeddings),
14 Err(e) => println!("Error: {}", e),
15 }
16}
More examples
4async fn main() {
5 const SERVER_BASE_URL: &str = "http://localhost:10086";
6
7 let client = Client::new(SERVER_BASE_URL).unwrap();
8
9 match client
10 .embeddings("Hello, world!".into(), EmbeddingsParams::default())
11 .await
12 {
13 Ok(embeddings) => println!("{:#?}", embeddings),
14 Err(e) => println!("Error: {}", e),
15 }
16}
4async 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}
4async 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}
4async 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}
Sourcepub fn server_base_url(&self) -> &Url
pub fn server_base_url(&self) -> &Url
Sourcepub async fn chat(
&self,
chat_history: &[ChatCompletionRequestMessage],
params: &ChatParams,
) -> Result<String, LlamaEdgeError>
pub async fn chat( &self, chat_history: &[ChatCompletionRequestMessage], params: &ChatParams, ) -> Result<String, LlamaEdgeError>
Send a chat completion request.
§Arguments
-
chat_history
- The chat history including the latest user message. -
params
- The parameters for the chat completion.
§Returns
A Result
containing the chat completion or an error.
Examples found in repository?
8async 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}
Sourcepub async fn chat_stream(
&self,
chat_history: &[ChatCompletionRequestMessage],
params: &ChatParams,
) -> Result<impl TryStream<Item = Result<String, LlamaEdgeError>, Error = LlamaEdgeError>, LlamaEdgeError>
pub async fn chat_stream( &self, chat_history: &[ChatCompletionRequestMessage], params: &ChatParams, ) -> Result<impl TryStream<Item = Result<String, LlamaEdgeError>, Error = LlamaEdgeError>, LlamaEdgeError>
Sourcepub async fn upload_file(
&self,
file: impl AsRef<Path>,
) -> Result<FileObject, LlamaEdgeError>
pub async fn upload_file( &self, file: impl AsRef<Path>, ) -> Result<FileObject, LlamaEdgeError>
Sourcepub async fn embeddings(
&self,
input: InputText,
params: EmbeddingsParams,
) -> Result<EmbeddingsResponse, LlamaEdgeError>
pub async fn embeddings( &self, input: InputText, params: EmbeddingsParams, ) -> Result<EmbeddingsResponse, LlamaEdgeError>
Compute embeddings for a given input.
§Arguments
-
input
- The input to compute embeddings for. -
params
- The parameters for the embeddings.
§Returns
A Result
containing the embeddings or an error.
Examples found in repository?
4async fn main() {
5 const SERVER_BASE_URL: &str = "http://localhost:8080";
6
7 let client = Client::new(SERVER_BASE_URL).unwrap();
8
9 match client
10 .embeddings("Hello, world!".into(), EmbeddingsParams::default())
11 .await
12 {
13 Ok(embeddings) => println!("{:#?}", embeddings),
14 Err(e) => println!("Error: {}", e),
15 }
16}
More examples
4async fn main() {
5 const SERVER_BASE_URL: &str = "http://localhost:10086";
6
7 let client = Client::new(SERVER_BASE_URL).unwrap();
8
9 match client
10 .embeddings("Hello, world!".into(), EmbeddingsParams::default())
11 .await
12 {
13 Ok(embeddings) => println!("{:#?}", embeddings),
14 Err(e) => println!("Error: {}", e),
15 }
16}
Sourcepub async fn transcribe(
&self,
audio_file: impl AsRef<Path>,
spoken_language: impl AsRef<str>,
params: TranscriptionParams,
) -> Result<TranscriptionObject, LlamaEdgeError>
Available on crate feature audio
only.
pub async fn transcribe( &self, audio_file: impl AsRef<Path>, spoken_language: impl AsRef<str>, params: TranscriptionParams, ) -> Result<TranscriptionObject, LlamaEdgeError>
audio
only.Transcribe an audio file.
§Arguments
-
audio_file
- The audio file to transcribe. -
spoken_language
- The language of the audio file. The language should be in ISO-639-1 format. For example, “en” for English, “zh” for Chinese, “ja” for Japanese, etc. -
params
- The parameters for the transcription.
§Returns
A Result
containing the transcription object or an error.
Examples found in repository?
4async 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}
Sourcepub async fn translate(
&self,
audio_file: impl AsRef<Path>,
spoken_language: impl AsRef<str>,
params: TranslationParams,
) -> Result<TranslationObject, LlamaEdgeError>
Available on crate feature audio
only.
pub async fn translate( &self, audio_file: impl AsRef<Path>, spoken_language: impl AsRef<str>, params: TranslationParams, ) -> Result<TranslationObject, LlamaEdgeError>
audio
only.Translate an audio file.
§Arguments
-
audio_file
- The audio file to translate. -
spoken_language
- The language of the audio file. The language should be in ISO-639-1 format. For example, “en” for English, “zh” for Chinese, “ja” for Japanese, etc. -
params
- The parameters for the translation.
§Returns
A Result
containing the translation object or an error.
Examples found in repository?
4async 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}
Sourcepub async fn create_image(
&self,
prompt: impl AsRef<str>,
params: ImageCreateParams,
) -> Result<Vec<ImageObject>, LlamaEdgeError>
Available on crate feature image
only.
pub async fn create_image( &self, prompt: impl AsRef<str>, params: ImageCreateParams, ) -> Result<Vec<ImageObject>, LlamaEdgeError>
image
only.Create an image with the given prompt.
§Arguments
-
prompt
- The prompt for the image. -
params
- The parameters for the image creation.
§Returns
A Result
containing the list of images or an error.
Examples found in repository?
4async 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}
Sourcepub async fn edit_image(
&self,
image: impl AsRef<Path>,
prompt: impl AsRef<str>,
params: ImageEditParams,
) -> Result<Vec<ImageObject>, LlamaEdgeError>
Available on crate feature image
only.
pub async fn edit_image( &self, image: impl AsRef<Path>, prompt: impl AsRef<str>, params: ImageEditParams, ) -> Result<Vec<ImageObject>, LlamaEdgeError>
image
only.Sourcepub async fn rag_retrieve_context(
&self,
chat_history: &[ChatCompletionRequestMessage],
params: RagChatParams,
) -> Result<Vec<RetrieveObject>, LlamaEdgeError>
Available on crate feature rag
only.
pub async fn rag_retrieve_context( &self, chat_history: &[ChatCompletionRequestMessage], params: RagChatParams, ) -> Result<Vec<RetrieveObject>, LlamaEdgeError>
rag
only.Retrieve the context from the VectorDB server.
§Arguments
-
chat_history
- The chat history. -
params
- The parameters for the retrieval.
§Returns
A Result
containing the retrieved context or an error.
Examples found in repository?
8async 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(
23 "What is the location of Paris, France along with the Seine River?".to_string(),
24 ),
25 None,
26 ));
27 messages.push(user_message);
28
29 // send chat completion request
30 if let Ok(res) = client
31 .rag_retrieve_context(&messages[..], RagChatParams::default())
32 .await
33 {
34 println!("Retrieved context:\n{:#?}", res);
35 }
36}
Sourcepub async fn rag_chunk_file(
&self,
file_path: impl AsRef<Path>,
chunk_capacity: usize,
) -> Result<ChunksResponse, LlamaEdgeError>
Available on crate feature rag
only.
pub async fn rag_chunk_file( &self, file_path: impl AsRef<Path>, chunk_capacity: usize, ) -> Result<ChunksResponse, LlamaEdgeError>
rag
only.