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

AI.contextScope resources #88

Merged
merged 2 commits into from
May 22, 2023
Merged
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
35 changes: 19 additions & 16 deletions core/src/commonMain/kotlin/com/xebia/functional/xef/auto/AI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import arrow.core.left
import arrow.core.raise.Raise
import arrow.core.raise.recover
import arrow.core.right
import arrow.fx.coroutines.Resource
import arrow.fx.coroutines.ResourceScope
import arrow.fx.coroutines.resourceScope
import com.xebia.functional.xef.AIError
Expand Down Expand Up @@ -154,31 +153,35 @@ class AIScope(
context.addTexts(docs.toList())
}

/**
* Creates a new scoped [VectorStore] using [store], which is scoped to the [block] lambda. The
* [block] also runs on a _nested_ [resourceScope], meaning that all additional resources created
* within [block] will be finalized after [block] finishes.
*/
@AiDsl
suspend fun <A> contextScope(
store: suspend (Embeddings) -> Resource<VectorStore>,
store: suspend ResourceScope.(Embeddings) -> VectorStore,
block: AI<A>
): A {
val newStore = store(embeddings).bind()
return AIScope(
openAIClient,
CombinedVectorStore(newStore, context),
embeddings,
logger,
): A = resourceScope {
val newStore = store([email protected])
AIScope(
this@AIScope.openAIClient,
CombinedVectorStore(newStore, this@AIScope.context),
this@AIScope.embeddings,
this@AIScope.logger,
this,
this
this@AIScope
)
.block()
}

@AiDsl
suspend fun <A> contextScope(block: AI<A>): A = contextScope(LocalVectorStoreBuilder, block)

/** Add new [docs] to the [context], and then executes the [scope]. */
/** Add new [docs] to the [context], and then executes the [block]. */
@AiDsl
suspend fun <A> contextScope(docs: List<String>, scope: suspend AIScope.() -> A): A =
contextScope {
extendContext(*docs.toTypedArray())
scope(this)
}
suspend fun <A> contextScope(docs: List<String>, block: AI<A>): A = contextScope {
extendContext(*docs.toTypedArray())
block(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xebia.functional.xef.vectorstores

import arrow.fx.coroutines.Resource
import arrow.fx.coroutines.continuations.resource
import arrow.fx.coroutines.ResourceScope
import arrow.fx.stm.TMap
import arrow.fx.stm.TVar
import arrow.fx.stm.atomically
Expand All @@ -11,8 +10,8 @@ import com.xebia.functional.xef.llm.openai.EmbeddingModel
import com.xebia.functional.xef.llm.openai.RequestConfig
import kotlin.math.sqrt

val LocalVectorStoreBuilder: (Embeddings) -> Resource<LocalVectorStore> = { e ->
resource { LocalVectorStore(e) }
val LocalVectorStoreBuilder: suspend ResourceScope.(Embeddings) -> LocalVectorStore = { e ->
LocalVectorStore(e)
}

class LocalVectorStore
Expand Down