-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a17218
commit dfcfb78
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/scala/com/cloud/apim/otoroshi/extensions/aigateway/decorators/fallback.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.cloud.apim.otoroshi.extensions.aigateway.decorators | ||
|
||
import com.cloud.apim.otoroshi.extensions.aigateway.entities.AiProvider | ||
import com.cloud.apim.otoroshi.extensions.aigateway.{ChatClient, ChatPrompt, ChatResponse} | ||
import otoroshi.env.Env | ||
import otoroshi.utils.TypedMap | ||
import otoroshi.utils.syntax.implicits._ | ||
import otoroshi_plugins.com.cloud.apim.extensions.aigateway.AiExtension | ||
import play.api.libs.json.{JsValue, Json} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
object ChatClientWithProviderFallback { | ||
def applyIfPossible(tuple: (AiProvider, ChatClient)): ChatClient = { | ||
if (tuple._1.providerFallback.isDefined) { | ||
new ChatClientWithProviderFallback(tuple._1, tuple._2) | ||
} else { | ||
tuple._2 | ||
} | ||
} | ||
} | ||
|
||
class ChatClientWithProviderFallback(originalProvider: AiProvider, chatClient: ChatClient) extends ChatClient { | ||
|
||
override def call(originalPrompt: ChatPrompt, attrs: TypedMap)(implicit ec: ExecutionContext, env: Env): Future[Either[JsValue, ChatResponse]] = { | ||
chatClient.call(originalPrompt, attrs).flatMap { | ||
case Left(err) => { | ||
env.adminExtensions.extension[AiExtension].flatMap(_.states.provider(originalProvider.providerFallback.get)) match { | ||
case None => err.leftf | ||
case Some(provider) => provider.getChatClient() match { | ||
case None => err.leftf | ||
case Some(fallbackClient) => fallbackClient.call(originalPrompt, attrs) | ||
} | ||
} | ||
} | ||
case Right(resp) => resp.rightf | ||
}.recoverWith { | ||
case t: Throwable => { | ||
env.adminExtensions.extension[AiExtension].flatMap(_.states.provider(originalProvider.providerFallback.get)) match { | ||
case None => Json.obj("error" -> "fallback provider not found").leftf | ||
case Some(provider) => provider.getChatClient() match { | ||
case None => Json.obj("error" -> "fallback client not found").leftf | ||
case Some(fallbackClient) => fallbackClient.call(originalPrompt, attrs) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters