aleph_alpha_client/semantic_embedding/
embedding_with_instruction.rs

1use crate::{
2    semantic_embedding::{
3        embedding::SemanticEmbeddingOutput, RequestBody, DEFAULT_EMBEDDING_MODEL_WITH_INSTRUCTION,
4    },
5    Job, Prompt, Task,
6};
7use serde::Serialize;
8
9const ENDPOINT: &str = "instructable_embed";
10
11/// Allows you to choose a semantic representation fitting for your use case.
12///
13/// By providing instructions, you can help the model better understand the nuances of your specific
14/// data, leading to embeddings that are more useful for your use case.
15#[derive(Serialize, Debug)]
16pub struct TaskSemanticEmbeddingWithInstruction<'a> {
17    /// To further improve performance by steering the model, you can use instructions.
18    ///
19    /// Instructions can help the model understand nuances of your specific data and ultimately lead
20    /// to embeddings that are more useful for your use-case. In this case, we aim to further
21    /// increase the absolute difference between the cosine similarities. Instruction can also be
22    /// the empty string.
23    pub instruction: &'a str,
24    /// The prompt (usually text) to be embedded.
25    #[serde(rename = "input")]
26    pub prompt: Prompt<'a>,
27    /// Return normalized embeddings. This can be used to save on additional compute when applying a
28    /// cosine similarity metric.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub normalize: Option<bool>,
31}
32
33impl Task for TaskSemanticEmbeddingWithInstruction<'_> {
34    type Output = SemanticEmbeddingOutput;
35    type ResponseBody = SemanticEmbeddingOutput;
36
37    fn build_request(
38        &self,
39        client: &reqwest::Client,
40        base: &str,
41        model: &str,
42    ) -> reqwest::RequestBuilder {
43        let body = RequestBody {
44            model,
45            semantic_embedding_task: self,
46        };
47        client.post(format!("{base}/{ENDPOINT}")).json(&body)
48    }
49
50    fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
51        response
52    }
53}
54
55impl Job for TaskSemanticEmbeddingWithInstruction<'_> {
56    type Output = SemanticEmbeddingOutput;
57    type ResponseBody = SemanticEmbeddingOutput;
58
59    fn build_request(&self, client: &reqwest::Client, base: &str) -> reqwest::RequestBuilder {
60        let body = RequestBody {
61            model: DEFAULT_EMBEDDING_MODEL_WITH_INSTRUCTION,
62            semantic_embedding_task: self,
63        };
64        client.post(format!("{base}/{ENDPOINT}")).json(&body)
65    }
66
67    fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
68        response
69    }
70}