Secured AI Backend Proxy
We prevent you from losing thousands of dollars by including a backend that securely handles your API key requests to the AI API.
Last updated
include(":ai")kotlin {
...
sourceSets {
commonMain.dependencies {
implementation(projects.ai)
}
}
}import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING
plugins {
...
id("com.codingfeline.buildkonfig")
}
buildkonfig {
packageName = "your.package.app"
val props = Properties()
try {
props.load(file("../local.properties").inputStream())
} catch (e: Exception) {
// keys are private and can not be committed to git
}
defaultConfigs {
buildConfigField(
STRING,
"MOBILE_AI_API_HMAC_SECRET_KEY",
props["mobile_ai_api_hmac_secret_key"]?.toString() ?: ""
)
buildConfigField(
STRING,
"MOBILE_AI_URL",
props["mobile_ai_url"]?.toString() ?: ""
)
}
}val aIService = AIService(
BuildKonfig.MOBILE_AI_URL,
BuildKonfig.MOBILE_AI_API_HMAC_SECRET_KEY,
)module {
singleOf {
AIService(
BuildKonfig.MOBILE_AI_URL,
BuildKonfig.MOBILE_AI_API_HMAC_SECRET_KEY,
)
}
}scope.launch {
aiService
.chatgpt(
model = model, // support gpt3.5 turbo, gpt4, etc
messages = listOf(
Message(
role = "system",
content = "Marv is a factual chatbot that is also sarcastic."
),
Message(
role = "user",
content = userContent
),
)
)
.onFailure {
result = "We found an error ${it.message}"
}
.onSuccess {
result = it
}
}scope.launch {
aiService
.dallee(prompt) // only dall-e 3 for now but you can easily change it
.onFailure { error = "We found an erorr ${it.message}" }
.onSuccess { resultUrl = it.data.firstOrNull()?.url }
.also { loading = false }
}scope.launch {
aiService
.vision(
prompt = prompt,
url = url // image url to analyze
)
.onFailure {
result = "We found an error ${it.message}"
}
.onSuccess {
result = it
}
}