-
Notifications
You must be signed in to change notification settings - Fork 0
Switch AI integration from OpenAI to Gemini. #17
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8c0131
- Replace OpenAI API with Gemini API for generating Kotlin topic deta…
yesshreyes bb45203
Remove unused import from `KtorHttpClient.kt`.
yesshreyes ab490b3
Merge branch 'master' into shreyas/migrate-openai-to-gemini
yesshreyes 0dd7be6
Removed extra lines
yesshreyes 0236d19
Update workflow with environmental variables + configure android sdk …
RajashekarRaju faab7a2
Refactor `KtorHttpClient` for Gemini API interaction:
yesshreyes dad3e5a
Adding missing dependency injection in implementation separated in mo…
RajashekarRaju 1b868bf
Moved `CodeExample`, `Section`, and `Syntax` data classes into `Kotli…
yesshreyes 15fc888
Mark "Replace search icon" and "Switch AI integration to Gemini" task…
yesshreyes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 0 additions & 136 deletions
136
.../kotlin/com/developersbreach/kotlindictionarymultiplatform/core/network/KtorHttpClient.kt
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
...n/com/developersbreach/kotlindictionarymultiplatform/core/network/api/GeminiApiService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.developersbreach.kotlindictionarymultiplatform.core.network.api | ||
|
|
||
| import com.developersbreach.kotlindictionarymultiplatform.Log | ||
| import com.developersbreach.kotlindictionarymultiplatform.core.network.parser.GeminiJsonParser | ||
| import com.developersbreach.kotlindictionarymultiplatform.core.network.request.GeminiPromptBuilder | ||
| import com.developersbreach.kotlindictionarymultiplatform.data.detail.model.KotlinTopicDetails | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.request.post | ||
| import io.ktor.client.request.setBody | ||
| import io.ktor.client.statement.bodyAsText | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.contentType | ||
| import kotlinx.serialization.json.Json | ||
|
|
||
| class GeminiApiService( | ||
| private val client: HttpClient, | ||
| private val json: Json, | ||
| ) { | ||
| suspend fun generateTopicDetails( | ||
| topicId: String, | ||
| apiKey: String, | ||
| ): KotlinTopicDetails { | ||
| val prompt = GeminiPromptBuilder.buildRequest(topicId) | ||
| val requestBody = GeminiPromptBuilder.buildRequestBody(prompt) | ||
|
|
||
| val response = client.post( | ||
| "https://generativelanguage.googleapis.com/v1/models/gemini-2.0-flash:generateContent?key=$apiKey", | ||
| ) { | ||
| contentType(ContentType.Application.Json) | ||
| setBody(requestBody) | ||
| } | ||
|
|
||
| val responseBody = response.bodyAsText() | ||
| Log.i("GeminiRawResponse", responseBody) | ||
|
|
||
| val cleanJson = GeminiJsonParser.extractCleanJson(responseBody, json) | ||
| Log.i("CleanJson", cleanJson) | ||
|
|
||
| return json.decodeFromString(KotlinTopicDetails.serializer(), cleanJson) | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
...om/developersbreach/kotlindictionarymultiplatform/core/network/parser/GeminiJsonParser.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.developersbreach.kotlindictionarymultiplatform.core.network.parser | ||
|
|
||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.jsonObject | ||
| import kotlinx.serialization.json.jsonArray | ||
| import kotlinx.serialization.json.jsonPrimitive | ||
|
|
||
| object GeminiJsonParser { | ||
|
|
||
| fun extractCleanJson( | ||
| rawResponse: String, | ||
| json: Json, | ||
| ): String { | ||
| val root = json.parseToJsonElement(rawResponse).jsonObject | ||
| val text = root["candidates"] | ||
| ?.jsonArray?.firstOrNull() | ||
| ?.jsonObject?.get("content") | ||
| ?.jsonObject?.get("parts") | ||
| ?.jsonArray?.firstOrNull() | ||
| ?.jsonObject?.get("text") | ||
| ?.jsonPrimitive?.content | ||
| ?: error("Malformed Gemini response") | ||
|
|
||
| return text | ||
| .removePrefix("```json\n") | ||
| .removePrefix("```json") | ||
| .removePrefix("json\n") | ||
| .removePrefix("json") | ||
| .removeSuffix("```") | ||
| .trim() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.