SemanticStore

class SemanticStore(store: Store, embeddings: EmbeddingModel, minScore: Double = -1.0, scope: Int = Store.SEARCH_SCOPE) : Store

A Store whose search finds the items that a query means, with the vectors of an EmbeddingModel: "what does she eat?" finds "Piscivegetarian: fish, but no meat", which has none of its words. Everything else goes to store as it is.

val embeddings = OpenAiEmbeddingModel(client, apiKey = key)
val store = SemanticStore(SqlDelightStore(driver), embeddings)
graph.invoke(input, GraphConfig(threadId = "plan-7", store = store.within("users", userId)))

// In a node or a tool:
val found = store().search(listOf("memories"), "what does the traveller eat?", limit = 5)

How it works:

  • A vector is made when a search first needs it, not when an item is put: a put calls no model, and an item that other code put into store is found too. A search makes the vectors of the items that have none, in calls of at most 100 texts to embeddings, and one for the query.

  • The vectors are kept in store, in the namespace VECTORS followed by the namespace of the item, with the item's key. So a store that keeps its items keeps their vectors, and the next search only makes the vector of the query. Do not put items of your own under VECTORS: list leaves that namespace out.

  • A vector is made again when the value of its item changed, or the EmbeddingModel.id did. A vector whose item was deleted is deleted by the next search that sees all the items of its namespace.

  • The vector of an item is made from its key and its value, as "diet: Piscivegetarian".

  • A search looks at the scope items that list returns first, the ones changed last, and ranks them by the cosine of their vector and the vector of the query. An item below minScore is left out.

Use it through within as any store. A run with this store gives recall of memoryTools a search by meaning, and a GraphConfig takes it as its store.

Parameters

store

where the items and their vectors are kept.

embeddings

makes the vectors.

minScore

the lowest cosine, from -1 to 1, of an item that a search returns. The default returns every item, the best first. What a good limit is depends on the model.

scope

how many items a search looks at.

Throws

if minScore is not between -1 and 1 or scope is not positive.

Constructors

Link copied to clipboard
constructor(store: Store, embeddings: EmbeddingModel, minScore: Double = -1.0, scope: Int = Store.SEARCH_SCOPE)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open suspend override fun delete(namespace: List<String>, key: String)
Link copied to clipboard
open suspend override fun get(namespace: List<String>, key: String): StoreItem?
Link copied to clipboard
open suspend override fun list(namespace: List<String>, limit: Int): List<StoreItem>
Link copied to clipboard
open suspend override fun put(namespace: List<String>, key: String, value: String)
Link copied to clipboard
open suspend override fun search(namespace: List<String>, query: String, limit: Int): List<StoreItem>