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

Move ai block to xef-kotlin module #225

Merged
merged 1 commit into from
Jul 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ import com.xebia.functional.xef.vectorstores.VectorStore
* [VectorStore], [OpenAIEmbeddings] and [AIClient] instances.
*/
typealias AI<A> = suspend CoreAIScope.() -> A
raulraja marked this conversation as resolved.
Show resolved Hide resolved

/** A DSL block that makes it more convenient to construct [AI] values. */
inline fun <A> ai(noinline block: suspend CoreAIScope.() -> A): AI<A> = block
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.xebia.functional.gpt4all

import arrow.core.Either
import arrow.core.left
import arrow.core.right
import com.xebia.functional.xef.AIError
import com.xebia.functional.xef.auto.AI
import com.xebia.functional.xef.auto.CoreAIScope
import com.xebia.functional.xef.auto.ai

/**
* Run the [AI] value to produce an [A], this method initialises all the dependencies required to
Expand Down Expand Up @@ -38,7 +35,7 @@ suspend inline fun <reified A> AI<A>.getOrThrow(): A = getOrElse { throw it }
* @see getOrElse for an operator that allow directly handling the [AIError] case.
*/
suspend inline fun <reified A> AI<A>.toEither(): Either<AIError, A> =
raulraja marked this conversation as resolved.
Show resolved Hide resolved
ai { invoke().right() }.getOrElse { it.left() }
Either.catchOrThrow { getOrThrow() }

suspend fun <A> AIScope(block: AI<A>, orElse: suspend (AIError) -> A): A =
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.xebia.functional.xef.auto

/** A DSL block that makes it more convenient to construct [AI] values. */
inline fun <A> ai(noinline block: suspend CoreAIScope.() -> A): AI<A> = block
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
package com.xebia.functional.xef.auto.llm.openai

import arrow.core.Either
import arrow.core.left
import arrow.core.right
import com.xebia.functional.xef.AIError
import com.xebia.functional.xef.auto.AI
import com.xebia.functional.xef.auto.CoreAIScope
import com.xebia.functional.xef.auto.ai
import kotlin.jvm.JvmName
import kotlin.time.ExperimentalTime

/**
* Run the [AI] value to produce an [A], this method initialises all the dependencies required to
Expand Down Expand Up @@ -42,9 +38,8 @@ suspend inline fun <reified A> AI<A>.getOrThrow(): A = getOrElse { throw it }
* @see getOrElse for an operator that allow directly handling the [AIError] case.
*/
suspend inline fun <reified A> AI<A>.toEither(): Either<AIError, A> =
raulraja marked this conversation as resolved.
Show resolved Hide resolved
ai { invoke().right() }.getOrElse { it.left() }
Either.catchOrThrow { getOrThrow() }

@OptIn(ExperimentalTime::class)
suspend fun <A> AIScope(block: AI<A>, orElse: suspend (AIError) -> A): A =
try {
val scope = CoreAIScope(OpenAIEmbeddings(OpenAI.DEFAULT_EMBEDDING))
Expand Down