Skip to content

Commit

Permalink
Create JVM compatible versions of suspend functions. Add annotations …
Browse files Browse the repository at this point in the history
…to replace names in JVM compiled libraries.
  • Loading branch information
adrian-lorenzo committed Aug 2, 2023
1 parent 243bb3b commit 536885a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/semanticsearch/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ dependencies {
implementation(projects.core)
implementation(projects.modules.embeddingsmodel)
implementation(projects.modules.vectorstore)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.7.3")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.theagilemonkeys.ellmental.semanticsearch
import com.theagilemonkeys.ellmental.core.schema.SemanticEntry
import com.theagilemonkeys.ellmental.embeddingsmodel.EmbeddingsModel
import com.theagilemonkeys.ellmental.vectorstore.VectorStore
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.future.future
import kotlinx.serialization.Serializable

context(EmbeddingsModel<Any>, VectorStore)
Expand All @@ -21,6 +24,7 @@ class SemanticSearch {
*
* @param input A list of texts to be learned.
*/
@JvmName("learn\$Kotlin")
suspend fun learn(input: SearchInput) =
input.texts.forEach { text ->
check(text.isNotBlank()) { "Text cannot be blank" }
Expand All @@ -29,6 +33,21 @@ class SemanticSearch {
upsert(semanticEntry)
}

/**
*
* "Learns" a list of texts to make them available in future semantic searches. It uses the
* [EmbeddingsModel] to calculate text embeddings for each piece of text. Then it uses the
* [VectorStore] to persist them.
*
* @param input A list of texts to be learned.
*
* (JVM compatible version)
*/
@OptIn(DelicateCoroutinesApi::class)
@JvmName("learn")
suspend fun learnCompletableFuture(input: SearchInput) =
GlobalScope.future { learn(input) }

/**
* Performs a semantic search operation using the provided text as reference. It will look for
* texts stored in the [VectorStore] that are "semantically close" to the provided one and return
Expand All @@ -37,12 +56,28 @@ class SemanticSearch {
* @param text The text to be used as reference for semantic search.
* @return A list of semantically similar texts ranked by semantic distance (the closest first).
*/
@JvmName("search\$Kotlin")
suspend fun search(text: String): SearchOutput {
check(text.isNotBlank()) { "Text cannot be blank" }
val embedding = embed(text)
val semanticEntry = SemanticEntry(content = text, embedding = embedding)
return SearchOutput(query(semanticEntry).entries)
}

/**
* Performs a semantic search operation using the provided text as reference. It will look for
* texts stored in the [VectorStore] that are "semantically close" to the provided one and return
* a ranked list of results.
*
* @param text The text to be used as reference for semantic search.
* @return A list of semantically similar texts ranked by semantic distance (the closest first).
*
* (JVM compatible version)
*/
@OptIn(DelicateCoroutinesApi::class)
@JvmName("search")
suspend fun searchCompletableFuture(text: String) =
GlobalScope.future { search(text) }
}

@Serializable
Expand Down

0 comments on commit 536885a

Please sign in to comment.