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

Add neural_sparse #84

Closed
Tracked by #86
esmarkowski opened this issue Mar 16, 2024 · 0 comments · Fixed by #92
Closed
Tracked by #86

Add neural_sparse #84

esmarkowski opened this issue Mar 16, 2024 · 0 comments · Fixed by #92
Milestone

Comments

@esmarkowski
Copy link
Member

esmarkowski commented Mar 16, 2024

Add neural_sparse for providing sparse vector search and access to results.

class OnboardingDoc < StretchyModel
	attribute :body, :text
	attribute :status, :string
	attribute :filename, :keyword
	attribute :path, :keyword
	attribute :owner, :keyword
	attribute :client, :keyword
	attribute :embedding, :rank_features

	default_pipeline :nlp_sparse_pipeline, model_id: 'q32Pw02BJ3squ3VZa'

end
question = "What clients are due for a monthly review?"
context = OnboardingDoc.neural_sparse(embedding: question)
#=>
   {
     "_index" : "onboarding_docs",
     "_id" : "1",
     "_score" : 30.0029,
     "_source" : {
       "body" : "Perform monthly review by QA team",
       "embedding" : {
         "review" : 0.8708904,
         "monthly" : 0.8587369,
         "QA" : 2.3929274,
         "team" : 2.7839446,
         "weekly" : 0.75845814,
       },
       "id" : "s1"
     }
   }

context.embedding.review
#=> 0.8708904

Neural Sparse

model.neural_sparse1

query_text String Required The query text from which to generate vector embeddings.
model_id String Required The ID of the sparse encoding model or tokenizer model that will be used to generate vector embeddings from the query text. The model must be deployed in OpenSearch before it can be used in sparse neural search. For more information, see Using custom models within OpenSearch and Neural sparse search.
max_token_score Float Optional (Deprecated) The theoretical upper bound of the score for all tokens in the vocabulary (required for performance optimization). For OpenSearch-provided pretrained sparse embedding models, we recommend setting max_token_scoreto 2 for amazon/neural-sparse/opensearch-neural-sparse-encoding-doc-v1and to 3.5 for amazon/neural-sparse/opensearch-neural-sparse-encoding-v1. This field has been deprecated as of OpenSearch 2.12.
# model.neural_sparse(field: 'query_text', **options)

model.neural_sparse(passage_embedding: 'Hi world')
#or 
model.neural_sparse(passage_embedding: 'Hi world', model_id: 'aP2Q8ooBpBj3wT4HVS8a')
{
  "query": {
    "neural_sparse": {
      "passage_embedding": {
        "query_text": "Hi world",
        "model_id": "aP2Q8ooBpBj3wT4HVS8a"
      }
    }
  }
}

Footnotes

  1. https://opensearch.org/docs/latest/query-dsl/specialized/neural-sparse/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant