Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embedding: support cybertron local embeddings #494

Closed
corani opened this issue Jan 5, 2024 · 3 comments · Fixed by #531 or #543
Closed

embedding: support cybertron local embeddings #494

corani opened this issue Jan 5, 2024 · 3 comments · Fixed by #531 or #543

Comments

@corani
Copy link
Contributor

corani commented Jan 5, 2024

Using https://github.com/nlpodyssey/cybertron you can generate embeddings locally in pure Go code. Not all models are supported, but I've successfully tested it with all-MiniLM-L6-v2, bge-small-en-v1.5 and e5-small-v2 (also base and large variants, but they get exponentially slower)

As this is CPU-based the performance isn't stellar, I'm getting about 20-30 embeddings per second on my laptop. Nevertheless, there are no dependencies on C-code or on CUDA, so it's very easy to get it to work.

Would it make sense to add an adapter for this in langchaingo?

Some sample code:

m, err := tasks.Load[textencoding.Interface](&tasks.Config{
    ModelsDir: "models",
    ModelName: "sentence-transformers/all-MiniLM-L6-v2",
})
if err != nil {
    panic(err)
}

result, err := m.Encode(ctx, text, int(bert.MeanPooling))
if err != nil {
    panic(err)
}

embedding := result.Vector.Normalize2().Data().F32()

Cybertron will automatically fetch the model from Huggingface and cache it locally in the ModelsDir. The last statement will give you an []float32 of the embedding.

corani added a commit to corani/langchaingo that referenced this issue Jan 19, 2024
Added a new package 'cybertron' under 'embeddings' to run embedding
models locally. This includes the main 'cybertron.go' file, a test
file 'cybertron_test.go', and an 'options.go' file for providing
various options for the client.

Resolves tmc#494
@corani
Copy link
Contributor Author

corani commented Jan 19, 2024

I had some idle time, so I've created a PR for it: #531

@tmc
Copy link
Owner

tmc commented Jan 19, 2024

Very cool! Are you open to writing an example and perhaps a docs site guide?

@corani
Copy link
Contributor Author

corani commented Jan 22, 2024

I have an example for the examples folder, but as the examples require a tagged version of langchaingo I can't submit it together with #531.

Let me know whether I should add it with a replace directive in go.mod or whether I should create a follow-up once #531 is merged and we have a new version.

corani added a commit to corani/langchaingo that referenced this issue Jan 22, 2024
Added a new package 'cybertron' under 'embeddings' to run embedding
models locally. This includes the main 'cybertron.go' file, a test
file 'cybertron_test.go', and an 'options.go' file for providing
various options for the client.

Resolves tmc#494
corani added a commit to corani/langchaingo that referenced this issue Jan 23, 2024
Added a new package 'cybertron' under 'embeddings' to run embedding
models locally. This includes the main 'cybertron.go' file, a test
file 'cybertron_test.go', and an 'options.go' file for providing
various options for the client.

Resolves tmc#494
@tmc tmc closed this as completed in #531 Jan 23, 2024
corani added a commit to corani/langchaingo that referenced this issue Jan 23, 2024
1. Updated the .gitignore file to ignore the models generated in the
   new example directory 'examples/cybertron-embedding-example/models/*'.
2. Added a new example 'cybertron-embedding.go' in
   'examples/cybertron-embedding-example' directory. This example
   demonstrates the usage of embeddings in the context of the
   'cybertron' package.
3. Added the necessary go.mod and go.sum files for the new example.

The changes provide a practical example of how to use embeddings with
the 'cybertron' package. The update to .gitignore prevents the version
control system from tracking the unnecessary model files generated by
the new example.

Resolves tmc#494
haochunchang pushed a commit to haochunchang/langchaingo that referenced this issue Jan 23, 2024
Added a new package 'cybertron' under 'embeddings' to run embedding
models locally. This includes the main 'cybertron.go' file, a test
file 'cybertron_test.go', and an 'options.go' file for providing
various options for the client.

Resolves tmc#494
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants