Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

Client for the LlamaEdge API.

Implementations§

Source§

impl Client

Source

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?
examples/embeddings.rs (line 7)
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
Hide additional examples
examples/rag_embeddings.rs (line 7)
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}
examples/rag_chunk_file.rs (line 7)
4async fn main() {
5    const SERVER_BASE_URL: &str = "http://localhost:10086";
6
7    let client = Client::new(SERVER_BASE_URL).unwrap();
8
9    let file_path = "tests/assets/paris.txt";
10    match client.rag_chunk_file(file_path, 1024).await {
11        Ok(chunks_response) => println!("{:#?}", chunks_response),
12        Err(e) => println!("Error: {}", e),
13    }
14}
examples/create_image.rs (line 7)
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}
examples/translate_audio.rs (line 7)
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}
examples/transcribe_audio.rs (line 7)
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}
Source

pub fn server_base_url(&self) -> &Url

Get the server base URL.

§Returns

A reference to the server base URL.

Source

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?
examples/chat.rs (line 28)
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}
Source

pub async fn chat_stream( &self, chat_history: &[ChatCompletionRequestMessage], params: &ChatParams, ) -> Result<impl TryStream<Item = Result<String, LlamaEdgeError>, Error = LlamaEdgeError>, LlamaEdgeError>

Send a chat completion request with streaming.

§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 stream or an error.

Source

pub async fn upload_file( &self, file: impl AsRef<Path>, ) -> Result<FileObject, LlamaEdgeError>

Upload a file to the server.

§Arguments
  • file - The file to upload.
§Returns

A Result containing the file object or an error.

Source

pub async fn models(&self) -> Result<Vec<Model>, LlamaEdgeError>

List all available models.

§Returns

A Result containing the list of models or an error.

Source

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?
examples/embeddings.rs (line 10)
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
Hide additional examples
examples/rag_embeddings.rs (line 10)
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}
Source

pub 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.

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?
examples/transcribe_audio.rs (lines 10-14)
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}
Source

pub 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.

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?
examples/translate_audio.rs (lines 10-14)
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}
Source

pub async fn create_image( &self, prompt: impl AsRef<str>, params: ImageCreateParams, ) -> Result<Vec<ImageObject>, LlamaEdgeError>

Available on crate feature 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?
examples/create_image.rs (line 10)
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}
Source

pub 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.

Edit the given image with the given prompt.

§Arguments
  • image - The image to edit.

  • prompt - The prompt for the image edit.

  • params - The parameters for the image edit.

§Returns

A Result containing the list of images or an error.

Source

pub async fn rag_retrieve_context( &self, chat_history: &[ChatCompletionRequestMessage], params: RagChatParams, ) -> Result<Vec<RetrieveObject>, LlamaEdgeError>

Available on crate feature 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?
examples/rag_retrieve_context.rs (line 31)
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}
Source

pub async fn rag_chunk_file( &self, file_path: impl AsRef<Path>, chunk_capacity: usize, ) -> Result<ChunksResponse, LlamaEdgeError>

Available on crate feature rag only.

Chunk a text file into chunks.

§Arguments
  • file_path - The path to the file to chunk. Note that the file should be a txt or md file.

  • chunk_capacity - The capacity of each chunk.

§Returns

A Result containing the chunks or an error.

Examples found in repository?
examples/rag_chunk_file.rs (line 10)
4async fn main() {
5    const SERVER_BASE_URL: &str = "http://localhost:10086";
6
7    let client = Client::new(SERVER_BASE_URL).unwrap();
8
9    let file_path = "tests/assets/paris.txt";
10    match client.rag_chunk_file(file_path, 1024).await {
11        Ok(chunks_response) => println!("{:#?}", chunks_response),
12        Err(e) => println!("Error: {}", e),
13    }
14}

Auto Trait Implementations§

§

impl Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,