Merge branch 'development_action007_mahmoud' into development
* development_action007_mahmoud: feat: Add upsert points functionality to Qdrant client feat: Remove unnecessary delete_collection call in example feat: Add Qdrant client's retrieve_points functionality feat: Improve Qdrant client example ... feat: Add Jina server health check feat: Add multi-vector API support feat: Add classifier deletion functionality qdrant feat: Add classifier listing functionality feat: Enhance Jina client with improved classification API feat: Add train functionality to Jina client feat: Add Jina client training and classification features feat: Add reranking functionality to Jina client feat: Enhance Jina client with additional embedding parameters feat: Add create_embeddings function to Jina client fix: Ensure the code compiles and add a test example ... jina specs
This commit is contained in:
69
examples/aiexamples/openai_chat_completion.vsh
Executable file
69
examples/aiexamples/openai_chat_completion.vsh
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.clients.jina
|
||||
import freeflowuniverse.herolib.osal
|
||||
import os
|
||||
|
||||
// Example of using the Jina client
|
||||
|
||||
fn main() {
|
||||
// Set environment variable for testing
|
||||
// In production, you would set this in your environment
|
||||
// osal.env_set(key: 'JINAKEY', value: 'your-api-key')
|
||||
|
||||
// Check if JINAKEY environment variable exists
|
||||
if !osal.env_exists('JINAKEY') {
|
||||
println('JINAKEY environment variable not set. Please set it before running this example.')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
// Create a Jina client instance
|
||||
mut client := jina.get(name: 'default')!
|
||||
|
||||
println('Jina client initialized successfully.')
|
||||
|
||||
// Example: Create embeddings
|
||||
model := 'jina-embeddings-v3'
|
||||
texts := ['Hello, world!', 'How are you doing?']
|
||||
|
||||
println('Creating embeddings for texts: ${texts}')
|
||||
result := client.create_embeddings(texts, model, 'retrieval.query')!
|
||||
|
||||
println('Embeddings created successfully.')
|
||||
println('Model: ${result['model']}')
|
||||
println('Data count: ${result['data'].arr().len}')
|
||||
|
||||
// Example: List classifiers
|
||||
println('\nListing classifiers:')
|
||||
classifiers := client.list_classifiers() or {
|
||||
println('Failed to list classifiers: ${err}')
|
||||
return
|
||||
}
|
||||
|
||||
println('Classifiers retrieved successfully.')
|
||||
|
||||
// Example: Create a classifier
|
||||
println('\nTraining a classifier:')
|
||||
examples := [
|
||||
jina.TrainingExample{
|
||||
text: 'This movie was great!'
|
||||
label: 'positive'
|
||||
},
|
||||
jina.TrainingExample{
|
||||
text: 'I did not like this movie.'
|
||||
label: 'negative'
|
||||
},
|
||||
jina.TrainingExample{
|
||||
text: 'The movie was okay.'
|
||||
label: 'neutral'
|
||||
}
|
||||
]
|
||||
|
||||
training_result := client.train(examples, model, 'private') or {
|
||||
println('Failed to train classifier: ${err}')
|
||||
return
|
||||
}
|
||||
|
||||
println('Classifier trained successfully.')
|
||||
println('Classifier ID: ${training_result['classifier_id']}')
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import os
|
||||
import freeflowuniverse.herolib.core.pathlib
|
||||
|
||||
// Helper function to format file sizes
|
||||
fn format_size(size i64) string {
|
||||
if size < 1024 {
|
||||
return '${size} B'
|
||||
} else if size < 1024 * 1024 {
|
||||
kb := f64(size) / 1024.0
|
||||
return '${kb:.1f} KB'
|
||||
} else if size < 1024 * 1024 * 1024 {
|
||||
mb := f64(size) / (1024.0 * 1024.0)
|
||||
return '${mb:.1f} MB'
|
||||
} else {
|
||||
gb := f64(size) / (1024.0 * 1024.0 * 1024.0)
|
||||
return '${gb:.1f} GB'
|
||||
}
|
||||
}
|
||||
|
||||
// Set parameters directly in the script
|
||||
// Change these values as needed
|
||||
target_dir := '/tmp' // Current directory by default
|
||||
show_hidden := false // Set to true to show hidden files
|
||||
recursive := false // Set to true for recursive listing
|
||||
|
||||
// Create a Path object for the target directory
|
||||
mut path := pathlib.get(target_dir)
|
||||
|
||||
// Ensure the directory exists and is a directory
|
||||
if path.exist == .no {
|
||||
eprintln('Error: Directory "${target_dir}" does not exist')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
if path.cat != .dir && path.cat != .linkdir {
|
||||
eprintln('Error: "${target_dir}" is not a directory')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
// Main execution
|
||||
println('Listing contents of: ${path.absolute()}')
|
||||
println('----------------------------')
|
||||
|
||||
// Define list arguments
|
||||
mut list_args := pathlib.ListArgs{
|
||||
recursive: recursive,
|
||||
ignoredefault: !show_hidden
|
||||
}
|
||||
|
||||
// Use pathlib to list the directory contents
|
||||
mut list_result := path.list(list_args) or {
|
||||
eprintln('Error listing directory: ${err}')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
// Print each file/directory
|
||||
for p in list_result.paths {
|
||||
// Skip the root directory itself
|
||||
if p.path == path.path {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate the level based on the path depth relative to the root
|
||||
rel_path := p.path.replace(list_result.root, '')
|
||||
level := rel_path.count('/') - if rel_path.starts_with('/') { 1 } else { 0 }
|
||||
|
||||
// Print indentation based on level
|
||||
if level > 0 {
|
||||
print(' '.repeat(level))
|
||||
}
|
||||
|
||||
// Print file/directory info
|
||||
name := p.name()
|
||||
if p.cat == .dir || p.cat == .linkdir {
|
||||
println('📁 ${name}/')
|
||||
} else {
|
||||
// Get file size
|
||||
file_size := os.file_size(p.path)
|
||||
println('📄 ${name} (${format_size(file_size)})')
|
||||
}
|
||||
}
|
||||
|
||||
println('----------------------------')
|
||||
println('Done!')
|
||||
86
examples/clients/jina.vsh
Executable file
86
examples/clients/jina.vsh
Executable file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.clients.jina
|
||||
|
||||
mut jina_client := jina.get()!
|
||||
health := jina_client.health()!
|
||||
println('Server health: ${health}')
|
||||
|
||||
// Create embeddings
|
||||
embeddings := jina_client.create_embeddings(
|
||||
input: ['Hello', 'World']
|
||||
model: .jina_embeddings_v3
|
||||
task: 'separation'
|
||||
) or { panic('Error while creating embeddings: ${err}') }
|
||||
|
||||
println('Created embeddings: ${embeddings}')
|
||||
|
||||
// Rerank
|
||||
rerank_result := jina_client.rerank(
|
||||
model: .reranker_v2_base_multilingual
|
||||
query: 'skincare products'
|
||||
documents: ['Product A', 'Product B', 'Product C']
|
||||
top_n: 2
|
||||
) or { panic('Error while reranking: ${err}') }
|
||||
|
||||
println('Rerank result: ${rerank_result}')
|
||||
|
||||
// Train
|
||||
train_result := jina_client.train(
|
||||
model: .jina_clip_v1
|
||||
input: [
|
||||
jina.TrainingExample{
|
||||
text: 'Sample text'
|
||||
label: 'positive'
|
||||
},
|
||||
jina.TrainingExample{
|
||||
image: 'https://letsenhance.io/static/73136da51c245e80edc6ccfe44888a99/1015f/MainBefore.jpg'
|
||||
label: 'negative'
|
||||
},
|
||||
]
|
||||
) or { panic('Error while training: ${err}') }
|
||||
|
||||
println('Train result: ${train_result}')
|
||||
|
||||
// Classify
|
||||
classify_result := jina_client.classify(
|
||||
model: .jina_clip_v1
|
||||
input: [
|
||||
jina.ClassificationInput{
|
||||
text: 'A photo of a cat'
|
||||
},
|
||||
jina.ClassificationInput{
|
||||
image: 'https://letsenhance.io/static/73136da51c245e80edc6ccfe44888a99/1015f/MainBefore.jpg'
|
||||
},
|
||||
]
|
||||
labels: ['cat', 'dog']
|
||||
) or { panic('Error while classifying: ${err}') }
|
||||
|
||||
println('Classification result: ${classify_result}')
|
||||
|
||||
// List classifiers
|
||||
classifiers := jina_client.list_classifiers() or { panic('Error fetching classifiers: ${err}') }
|
||||
println('Classifiers: ${classifiers}')
|
||||
|
||||
// Delete classifier
|
||||
delete_result := jina_client.delete_classifier(classifier_id: classifiers[0].classifier_id) or {
|
||||
panic('Error deleting classifier: ${err}')
|
||||
}
|
||||
println('Delete result: ${delete_result}')
|
||||
|
||||
// Create multi vector
|
||||
multi_vector := jina_client.create_multi_vector(
|
||||
input: [
|
||||
jina.MultiVectorTextDoc{
|
||||
text: 'Hello world'
|
||||
input_type: .document
|
||||
},
|
||||
jina.MultiVectorTextDoc{
|
||||
text: "What's up?"
|
||||
input_type: .query
|
||||
},
|
||||
]
|
||||
embedding_type: ['float']
|
||||
// dimensions: 96
|
||||
)!
|
||||
println('Multi vector: ${multi_vector}')
|
||||
85
examples/clients/qdrant_example.vsh
Executable file
85
examples/clients/qdrant_example.vsh
Executable file
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.clients.qdrant
|
||||
import freeflowuniverse.herolib.core.httpconnection
|
||||
import rand
|
||||
|
||||
// 1. Get the qdrant client
|
||||
mut qdrant_client := qdrant.get()!
|
||||
|
||||
// 2. Generate collection name
|
||||
|
||||
collection_name := 'collection_' + rand.string(4)
|
||||
|
||||
// 2. Create a new collection
|
||||
|
||||
created_collection := qdrant_client.create_collection(
|
||||
collection_name: collection_name
|
||||
size: 15
|
||||
distance: 'Cosine'
|
||||
)!
|
||||
|
||||
println('Created Collection: ${created_collection}')
|
||||
|
||||
// 3. Get the created collection
|
||||
get_collection := qdrant_client.get_collection(
|
||||
collection_name: collection_name
|
||||
)!
|
||||
|
||||
println('Get Collection: ${get_collection}')
|
||||
|
||||
// 4. Delete the created collection
|
||||
// deleted_collection := qdrant_client.delete_collection(
|
||||
// collection_name: collection_name
|
||||
// )!
|
||||
|
||||
// println('Deleted Collection: ${deleted_collection}')
|
||||
|
||||
// 5. List all collections
|
||||
list_collection := qdrant_client.list_collections()!
|
||||
println('List Collection: ${list_collection}')
|
||||
|
||||
// 6. Check collection existence
|
||||
collection_existence := qdrant_client.is_collection_exists(
|
||||
collection_name: collection_name
|
||||
)!
|
||||
println('Collection Existence: ${collection_existence}')
|
||||
|
||||
// 7. Retrieve points
|
||||
collection_points := qdrant_client.retrieve_points(
|
||||
collection_name: collection_name
|
||||
ids: [
|
||||
0,
|
||||
3,
|
||||
100,
|
||||
]
|
||||
)!
|
||||
|
||||
println('Collection Points: ${collection_points}')
|
||||
|
||||
// 8. Upsert points
|
||||
upsert_points := qdrant_client.upsert_points(
|
||||
collection_name: collection_name
|
||||
points: [
|
||||
qdrant.Point{
|
||||
payload: {
|
||||
'key': 'value'
|
||||
}
|
||||
vector: [1.0, 2.0, 3.0]
|
||||
},
|
||||
qdrant.Point{
|
||||
payload: {
|
||||
'key': 'value'
|
||||
}
|
||||
vector: [4.0, 5.0, 6.0]
|
||||
},
|
||||
qdrant.Point{
|
||||
payload: {
|
||||
'key': 'value'
|
||||
}
|
||||
vector: [7.0, 8.0, 9.0]
|
||||
},
|
||||
]
|
||||
)!
|
||||
|
||||
println('Upsert Points: ${upsert_points}')
|
||||
9
examples/installers/db/qdrant.vsh
Executable file
9
examples/installers/db/qdrant.vsh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.installers.db.qdrant as qdrant_installer
|
||||
|
||||
mut db := qdrant_installer.get()!
|
||||
|
||||
db.install()!
|
||||
db.start()!
|
||||
|
||||
@@ -3,5 +3,8 @@
|
||||
import freeflowuniverse.herolib.installers.lang.python as python_module
|
||||
|
||||
mut python_installer := python_module.get()!
|
||||
// python_installer.install()!
|
||||
python_installer.destroy()!
|
||||
python_installer.install()!
|
||||
|
||||
|
||||
|
||||
// python_installer.destroy()!
|
||||
|
||||
50
examples/jina_example.vsh
Executable file
50
examples/jina_example.vsh
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.clients.jina
|
||||
import os
|
||||
import json
|
||||
|
||||
fn main() {
|
||||
// Initialize Jina client
|
||||
mut j := jina.Jina{
|
||||
name: 'test_client'
|
||||
secret: os.getenv('JINAKEY')
|
||||
}
|
||||
|
||||
// Initialize the client
|
||||
j = jina.obj_init(j) or {
|
||||
println('Error initializing Jina client: ${err}')
|
||||
return
|
||||
}
|
||||
|
||||
// Check if authentication works
|
||||
auth_ok := j.check_auth() or {
|
||||
println('Authentication failed: ${err}')
|
||||
return
|
||||
}
|
||||
|
||||
println('Authentication successful: ${auth_ok}')
|
||||
|
||||
// Create embeddings
|
||||
model := 'jina-embeddings-v2-base-en'
|
||||
input := ['Hello world', 'This is a test']
|
||||
|
||||
embeddings := j.create_embeddings(input, model, 'search') or {
|
||||
println('Error creating embeddings: ${err}')
|
||||
return
|
||||
}
|
||||
|
||||
println('Embeddings created successfully!')
|
||||
println('Model: ${embeddings.model}')
|
||||
println('Dimension: ${embeddings.dimension}')
|
||||
println('Number of embeddings: ${embeddings.data.len}')
|
||||
|
||||
// If there are embeddings, print the first one (truncated)
|
||||
if embeddings.data.len > 0 {
|
||||
first_embedding := embeddings.data[0]
|
||||
println('First embedding (first 5 values): ${first_embedding.embedding[0..5]}')
|
||||
}
|
||||
|
||||
// Usage information
|
||||
println('Token usage: ${embeddings.usage.total_tokens} ${embeddings.usage.unit}')
|
||||
}
|
||||
Reference in New Issue
Block a user