Skip to content

Commit

Permalink
Move ai block to xef-kotlin module (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulraja authored Jul 4, 2023
1 parent d356d4f commit 81ce22c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
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

/** 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> =
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> =
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

0 comments on commit 81ce22c

Please sign in to comment.