Building RAG with Pinecone on Android/Kotlin

Hi everyone, I built the prototype for my client in Python. Pinecone is so solid. Now I am releasing the solution on mobile, starting with Android/Kotlin. On mobile, we just don’t have the resources available like in the Python world. Here’s my Kotlin general API query function. Does anyone have any good Pinecone libraries to make implementing Pinecone easier on mobile? I am using OKhttp an GSON with good results.

// get embeddings from pinecone vector database

suspend fun queryPinecone(vector: List<Double>, topK: Int = 3): String = withContext(Dispatchers.IO) {
    val indexName = "your-index"
    val project = "your-project"
    val environment = "your-enviro"
    val namespace = "your_ns"

​    val jsonPayload = Gson().toJson(
​        mapOf(
​            "vector" to vector,
​            "topK" to topK,
​            "namespace" to namespace,
​            "includeValues" to false,
​            "includeMetadata" to true
​        )
​    )
​    val requestBody = jsonPayload.toRequestBody("application/json; charset=utf-8".toMediaType())
​    val request = Request.Builder()
​        .url("https://${indexName}-${project}.svc.${environment}.pinecone.io/query")
​        .addHeader("Api-Key", your_api-key)
​        .post(requestBody)
​        .build()

​    client.newCall(request).execute().use { response ->
​        if (!response.isSuccessful) {
​            val errorBody = response.body?.string()
​            val errorMessage = "PC API call failed: ${response.code}: $errorBody"
​            Log.e("MyAppTag", errorMessage)
​            throw Exception(errorMessage)
​        }
​        response.body?.string() ?: ""
​    }
}
1 Like

To integrate Pinecone with your mobile application, you could expose your Python prototype as a web service by creating an API endpoint. Use a lightweight web framework like Flask or FastAPI to wrap your Pinecone implementation. This way, your Android app can make HTTP requests to the API, sending and receiving data in JSON format, which you can easily handle with OKhttp and GSON. This not only abstracts the complexity of direct library use on mobile but also utilizes the processing power of the server, which is particularly useful for resource-intensive operations.
This works well for my projects.

2 Likes