Package org.machanism.machai.bindex
Class Picker
java.lang.Object
org.machanism.machai.bindex.Picker
Performs Bindex registration, lookup, and semantic retrieval using embeddings
and MongoDB.
This type supports two primary workflows:
- Registration: serialize and persist a
Bindexinto MongoDB, including a classification embedding vector used for semantic search. - Retrieval: classify a free-text query into one or more
Classificationobjects using an LLM, then run vector search queries to retrieve relevant Bindexes and expand results using transitive dependencies.
Example
Configurator config = ...;
try (Picker picker = new Picker("openai", null, config)) {
List<Bindex> results = picker.pick("Find libraries for server-side logging");
}
- Since:
- 0.0.2
- Author:
- Viktor Tovstyi
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidaddDependencies(Set<String> dependencies, String bindexId) Adds all transitive dependencies of a Bindex into the provided set.Registers (inserts or updates) a Bindex document for the suppliedBindex.protected BindexRetrieves a Bindex instance from the database by its Bindex id.(package private) org.bson.BsonArraygetEmbeddingBson(Classification classification, int dimensions) Generates a BSON array representing the embedding of a classification for semantic search.(package private) static StringgetNormalizedLanguageName(Language language) Normalizes aLanguagename for semantic aggregation queries.getRegistredId(Bindex bindex) Looks up the registered database ID for a Bindex (if it exists).Retrieves the similarity score for a Bindex result by its ID.Performs a semantic pick/search with a query string, retrieving Bindex results and transitive dependencies.voidSets the minimum similarity score for semantic vector search queries.
-
Field Details
-
BINDEX_PROPERTY_NAME
MongoDB field name used to store the serialized Bindex JSON payload.- See Also:
-
MODEL_PROP_NAME
- See Also:
-
SCORE_PROP_NAME
- See Also:
-
DEFAULT_MODEL
- See Also:
-
DEFAULT_SCORE_VALUE
-
-
Constructor Details
-
Picker
Constructs aPickerfor registration and semantic search.- Parameters:
genai- GenAI provider identifier used for embedding and classification promptsuri- MongoDB connection URI; whennull, a default URI is constructed usingBINDEX_REG_PASSWORDwhen availableconfig- configurator used to initialize the provider- Throws:
IllegalArgumentException- ifgenaiorconfigisnull
-
Picker
Picker(com.mongodb.client.MongoCollection<org.bson.Document> collection, org.machanism.machai.ai.manager.Genai provider)
-
-
Method Details
-
create
Registers (inserts or updates) a Bindex document for the suppliedBindex.Registration deletes any existing document with the same
Bindex.getId()and inserts a fresh document containing:- Core identity fields:
id,name,version - Classification facets: domains, layers, normalized languages, integrations
- A vector embedding under "classification_embedding"
- Parameters:
bindex- Bindex instance to register- Returns:
- generated database ID string
- Throws:
IllegalArgumentException- ifbindexisnullcom.fasterxml.jackson.core.JsonProcessingException- if conversion to JSON failscom.mongodb.MongoCommandException- on MongoDB insert or connection errors
- Core identity fields:
-
getEmbeddingBson
org.bson.BsonArray getEmbeddingBson(Classification classification, int dimensions) throws com.fasterxml.jackson.core.JsonProcessingException Generates a BSON array representing the embedding of a classification for semantic search.- Parameters:
classification- classification instancedimensions- number of vector dimensions- Returns:
- BSON array of vector values
- Throws:
IllegalArgumentException- ifclassificationisnullordimensionsis not positivecom.fasterxml.jackson.core.JsonProcessingException- on embedding or serialization errors
-
getRegistredId
Looks up the registered database ID for a Bindex (if it exists).- Parameters:
bindex- Bindex instance- Returns:
- MongoDB object id as string, or
nullif not present - Throws:
IllegalArgumentException- ifbindexisnull
-
pick
Performs a semantic pick/search with a query string, retrieving Bindex results and transitive dependencies.- Parameters:
query- query string- Returns:
- list of Bindex results related to the query (and their dependencies)
- Throws:
IllegalArgumentException- ifqueryisnullIOException- if classification or database retrieval fails
-
addDependencies
Adds all transitive dependencies of a Bindex into the provided set.- Parameters:
dependencies- set of dependency IDs to accumulatebindexId- Bindex ID to explore
-
getBindex
Retrieves a Bindex instance from the database by its Bindex id.- Parameters:
id- Bindex id (theidfield in the stored document)- Returns:
- parsed
Bindex, ornullif not present - Throws:
IllegalArgumentException- ifidisnullor the stored JSON cannot be parsed
-
getNormalizedLanguageName
Normalizes aLanguagename for semantic aggregation queries.- Parameters:
language- language object- Returns:
- normalized language name string
-
setScore
Sets the minimum similarity score for semantic vector search queries.- Parameters:
minScore- minimum similarity value
-
getScore
Retrieves the similarity score for a Bindex result by its ID.- Parameters:
id- Bindex identifier- Returns:
- score value for semantic result, or
nullif not present
-