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

Enhance the AI flag by allowing ai-assistant-server to be missing #2818

Merged
merged 4 commits into from
Sep 5, 2024
Merged
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
@@ -1,16 +1,34 @@
package edu.uci.ics.texera.web.resource.aiassistant

import edu.uci.ics.amber.engine.common.AmberConfig
import java.net.{HttpURLConnection, URL}
import com.typesafe.config.Config

object AiAssistantManager {
private val aiAssistantConfig = AmberConfig.aiAssistantConfig.getOrElse(
throw new Exception("ai-assistant-server configuration is missing in application.conf")
)
val assistantType: String = aiAssistantConfig.getString("assistant")
// The accountKey is the OpenAI authentication key used to authenticate API requests and obtain responses from the OpenAI service.
// Optionally retrieve the configuration
private val aiAssistantConfigOpt: Option[Config] = AmberConfig.aiAssistantConfig
private val noAssistant: String = "NoAiAssistant"
// Public variables, accessible from outside the object
var accountKey: String = _
var sharedUrl: String = _

// Initialize accountKey and sharedUrl if the configuration is present
aiAssistantConfigOpt.foreach { aiAssistantConfig =>
xudongwu-0 marked this conversation as resolved.
Show resolved Hide resolved
accountKey = aiAssistantConfig.getString("ai-service-key")
sharedUrl = aiAssistantConfig.getString("ai-service-url")
}

val accountKey: String = aiAssistantConfig.getString("ai-service-key")
val sharedUrl: String = aiAssistantConfig.getString("ai-service-url")
val validAIAssistant: String = aiAssistantConfigOpt match {
case Some(aiAssistantConfig) =>
val assistantType: String = aiAssistantConfig.getString("assistant")
assistantType match {
case "none" => noAssistant
case "openai" => initOpenAI()
case _ => noAssistant
}
case None =>
noAssistant
}

private def initOpenAI(): String = {
var connection: HttpURLConnection = null
Expand All @@ -26,26 +44,15 @@ object AiAssistantManager {
if (responseCode == 200) {
"OpenAI"
} else {
"NoAiAssistant"
noAssistant
}
} catch {
case e: Exception =>
"NoAiAssistant"
noAssistant
} finally {
if (connection != null) {
connection.disconnect()
}
}
}

val validAIAssistant: String = assistantType match {
case "none" =>
"NoAiAssistant"

case "openai" =>
initOpenAI()

case _ =>
"NoAiAssistant"
}
}
Loading