feat: Add create_embeddings function to Jina client
- Added a `create_embeddings` function to the Jina client to generate embeddings for given input texts. - Improved the `create_embeddings` function input parameters for better flexibility and error handling. - Updated `TextEmbeddingInput` struct to handle optional parameters for embedding type, truncation type, and late chunking. This improves the flexibility of the embedding generation process.
This commit is contained in:
@@ -2,5 +2,12 @@
|
||||
|
||||
import freeflowuniverse.herolib.clients.jina
|
||||
|
||||
jina_client := jina.get()!
|
||||
println('jina: ${jina_client}')
|
||||
mut jina_client := jina.get()!
|
||||
|
||||
embeddings := jina_client.create_embeddings(
|
||||
input: ['Hello', 'World']
|
||||
model: .embeddings_v3
|
||||
task: 'separation'
|
||||
) or { panic('Error while creating embeddings: ${err}') }
|
||||
|
||||
println('Created embeddings: ${embeddings}')
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
module jina
|
||||
|
||||
import freeflowuniverse.herolib.core.httpconnection
|
||||
import json
|
||||
import os
|
||||
import net.http
|
||||
|
||||
@[params]
|
||||
pub struct CreateEmbeddingParams {
|
||||
pub mut:
|
||||
input []string @[required] // Input texts
|
||||
model JinaModelEnumerator @[required] // Model name
|
||||
task string @[required] // Task type
|
||||
}
|
||||
|
||||
// Create embeddings for input texts
|
||||
pub fn (mut j Jina) create_embeddings(input []string, model string, task string) !ModelEmbeddingOutput {
|
||||
model_ := jina_model_from_string(model)!
|
||||
task_ := task_type_from_string(task)!
|
||||
pub fn (mut j Jina) create_embeddings(params CreateEmbeddingParams) !ModelEmbeddingOutput {
|
||||
task := task_type_from_string(params.task)!
|
||||
|
||||
mut embedding_input := TextEmbeddingInput{
|
||||
input: input
|
||||
model: model_
|
||||
task: task_
|
||||
late_chunking: false
|
||||
input: params.input
|
||||
model: params.model.to_string()
|
||||
task: task
|
||||
}
|
||||
|
||||
req := httpconnection.Request{
|
||||
|
||||
@@ -144,25 +144,31 @@ mut:
|
||||
// TextEmbeddingInput represents the input for text embedding requests with enum types
|
||||
pub struct TextEmbeddingInput {
|
||||
pub mut:
|
||||
model JinaModelEnumerator = JinaModelEnumerator.embeddings_v2_base_en
|
||||
model string = 'jina-embeddings-v2-base-en'
|
||||
input []string @[required]
|
||||
task TaskType // task type
|
||||
type_ EmbeddingType // embedding type
|
||||
truncate TruncateType // truncation type
|
||||
late_chunking bool // Flag to determine if late chunking is applied
|
||||
task TaskType // task type
|
||||
type_ ?EmbeddingType // embedding type
|
||||
truncate ?TruncateType // truncation type
|
||||
late_chunking ?bool // Flag to determine if late chunking is applied
|
||||
}
|
||||
|
||||
// dumps converts TextEmbeddingInput to JSON string
|
||||
pub fn (t TextEmbeddingInput) dumps() !string {
|
||||
mut raw := TextEmbeddingInputRaw{
|
||||
model: t.model.to_string()
|
||||
model: t.model
|
||||
input: t.input
|
||||
late_chunking: t.late_chunking
|
||||
late_chunking: if v := t.late_chunking { true } else { false }
|
||||
}
|
||||
|
||||
raw.task = t.task.to_string()
|
||||
raw.type_ = t.type_.to_string()
|
||||
raw.truncate = t.truncate.to_string()
|
||||
if v := t.type_ {
|
||||
raw.type_ = v.to_string()
|
||||
}
|
||||
|
||||
if v := t.truncate {
|
||||
raw.truncate = v.to_string()
|
||||
}
|
||||
|
||||
return json.encode(raw)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user