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

Create JVM compatible versions of suspend functions. Add annotations to replace names in JVM compiled libraries. #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -4,6 +4,9 @@ import com.theagilemonkeys.ellmental.core.schema.SemanticEntry
import com.theagilemonkeys.ellmental.core.schema.SemanticEntryMatch
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 @@ -22,6 +25,7 @@ class SemanticSearch {
*
* @param input A list of texts to be learned.
*/
@JvmName("learn\$Kotlin")
suspend fun learn(input: LearnInput) =
input.texts.forEach { text ->
check(text.isNotBlank()) { "Text cannot be blank" }
Expand All @@ -30,6 +34,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")
fun learnCompletableFuture(input: LearnInput) =
GlobalScope.future { learn(input) }
Copy link
Member

@juanjoman juanjoman Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a Kotlin expert but I was reading that using CoroutineScope is more correct and less "delicate" (you can remove the @OptIn annotation):

CoroutineScope(EmptyCoroutineContext).future { learn(input) }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your problem seems to be related to a problem that is not part of the function created. Probably because of the usage of Kotlin functionalities that are not compatible with Java.

Also, CoroutineScope seems to have the same problems as GlobalScope, and it is not recommended to be used by Kotlin's official documentation.


/**
* 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 @@ -39,12 +58,28 @@ class SemanticSearch {
* @param itemsLimit The maximum number of results to return.
* @return A list of semantically similar texts ranked by semantic distance (the closest first).
*/
@JvmName("search\$Kotlin")
suspend fun search(text: String, itemsLimit: Int): SearchOutput {
check(text.isNotBlank()) { "Text cannot be blank" }
val embedding = embed(text)
val semanticEntry = SemanticEntry(content = text, embedding = embedding)
return SearchOutput(query(semanticEntry, itemsLimit).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")
fun searchCompletableFuture(text: String, itemsLimit: Int) =
GlobalScope.future { search(text, itemsLimit) }
}

@Serializable
Expand Down
Loading