You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Try Direct Government API with 3-second timeout
144
-
val govResult = kotlinx.coroutines.withTimeoutOrNull(3000) {
145
-
fetchDirectFromGovApi(state, commodity)
146
-
}
147
-
if (govResult !=null&& govResult.isSuccess && govResult.getOrDefault(emptyList()).isNotEmpty()) {
148
-
return@withContext govResult
149
-
}
150
-
151
-
// If Gov API fails, times out, or is empty, use AI to fetch realistic market rates
152
-
try {
153
-
val prompt ="""
154
-
You are a real-time agriculture data provider. The user wants current market rates for $commodity in $state, India.
155
-
The government API is down. Generate 3 realistic current Mandi (market) records for this crop in this state.
156
-
Respond strictly with a JSON array of objects with keys: "state", "district", "market", "commodity", "variety", "minPrice", "maxPrice", "modalPrice" (all prices as numbers), "arrivalDate" (DD/MM/YYYY).
157
-
Do not include markdown, just the JSON array.
158
-
""".trimIndent()
159
-
160
-
val aiResponse =GeminiVisionService.chatQuery(prompt)
161
-
if (aiResponse.isSuccess) {
162
-
val jsonStr = aiResponse.getOrNull() ?:""
163
-
val cleanRaw = jsonStr.replace(Regex("<think>.*?</think>", RegexOption.DOT_MATCHES_ALL), "").trim()
164
-
val start = cleanRaw.indexOf('[')
165
-
val end = cleanRaw.lastIndexOf(']')
166
-
if (start ==-1|| end ==-1) throwException("No JSON array found in AI response")
0 commit comments