diff --git a/.changes/apparatus-bubble-direction-drug.json b/.changes/apparatus-bubble-direction-drug.json new file mode 100644 index 00000000..801f4f2d --- /dev/null +++ b/.changes/apparatus-bubble-direction-drug.json @@ -0,0 +1 @@ +{"type":"MINOR","changes":["Ensure model names are always prefixed with 'models/'"]} diff --git a/generativeai/src/main/java/com/google/ai/client/generativeai/internal/api/APIController.kt b/generativeai/src/main/java/com/google/ai/client/generativeai/internal/api/APIController.kt index 73a8c239..e46016ca 100644 --- a/generativeai/src/main/java/com/google/ai/client/generativeai/internal/api/APIController.kt +++ b/generativeai/src/main/java/com/google/ai/client/generativeai/internal/api/APIController.kt @@ -44,7 +44,7 @@ import kotlinx.coroutines.launch import kotlinx.serialization.json.Json // TODO: Should these stay here or be moved elsewhere? -internal const val DOMAIN = "https://generativelanguage.googleapis.com/v1/models" +internal const val DOMAIN = "https://generativelanguage.googleapis.com/v1" internal val JSON = Json { ignoreUnknownKeys = true @@ -63,9 +63,11 @@ internal val JSON = Json { */ internal class APIController( private val key: String, - private val model: String, + model: String, httpEngine: HttpClientEngine = OkHttp.create() ) { + private val model = fullModelName(model) + private val client = HttpClient(httpEngine) { install(HttpTimeout) { @@ -106,6 +108,14 @@ internal class APIController( } } +/** + * Ensures the model name provided has a `models/` prefix + * + * Models must be prepended with the `models/` prefix when communicating with the backend. + */ +private fun fullModelName(name: String): String = + name.takeIf { it.startsWith("models/") } ?: "models/$name" + /** * Makes a POST request to the specified [url] and returns a [Flow] of deserialized response objects * of type [R]. The response is expected to be a stream of JSON objects that are parsed in real-time