-
Notifications
You must be signed in to change notification settings - Fork 140
feat(gemini): add secure Gemini API SDK setup for backend and frontend #143
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
10 commits
Select commit
Hold shift + click to select a range
801d772
added gemini integration guide for ai agents
Saahi30 11e5a17
feat:gemini integration setup
Saahi30 644b22e
chore: modify env for gemini integration
Saahi30 fa3ab16
fix:added the google.gemic here
Saahi30 48140fb
docs(gemini): add Gemini API implementation guide
Saahi30 e9dd408
feat(gemini): implement Gemini API integration
Saahi30 e8e7d6c
fix(backend):handle missing key in Gemini route
Saahi30 ef1b7e8
chore(frontend): remove unused @google/genai dependency and orphaned …
Saahi30 7c4e08a
docs(guide): correct implementation guide
Saahi30 b9d8646
fix(api): switch to async httpx for Gemini endpoint and update requir…
Saahi30 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import os | ||
| import httpx | ||
| from fastapi import APIRouter, HTTPException | ||
| from pydantic import BaseModel | ||
| from app.core.config import settings | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| router = APIRouter() | ||
| GEMINI_API_KEY = settings.gemini_api_key | ||
| GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" | ||
|
|
||
| class GenerateRequest(BaseModel): | ||
| prompt: str | ||
|
|
||
| @router.post("/generate") | ||
| async def generate_content(request: GenerateRequest): | ||
| if not GEMINI_API_KEY: | ||
| raise HTTPException(status_code=500, detail="Gemini API is not configured. Please set GEMINI_API_KEY in environment.") | ||
| payload = { | ||
| "contents": [{"role": "user", "parts": [{"text": request.prompt}]}] | ||
| } | ||
| headers = { | ||
| "Content-Type": "application/json", | ||
| } | ||
| params = {"key": GEMINI_API_KEY} | ||
| try: | ||
| async with httpx.AsyncClient(timeout=30.0) as client: | ||
| response = await client.post(GEMINI_API_URL, json=payload, headers=headers, params=params) | ||
| response.raise_for_status() | ||
| return response.json() | ||
| except httpx.RequestError as e: | ||
| raise HTTPException(status_code=502, detail=f"Gemini API error: {str(e)}") | ||
| except httpx.HTTPStatusError as e: | ||
| raise HTTPException(status_code=502, detail=f"Gemini API error: {str(e)}") | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Gemini text generation API integration | ||
| // Calls backend /generate endpoint securely | ||
| export async function generateGeminiText(prompt: string): Promise<any> { | ||
| const apiUrl = process.env.NEXT_PUBLIC_API_URL; | ||
| if (!apiUrl) { | ||
| throw new Error("NEXT_PUBLIC_API_URL is not set in environment."); | ||
| } | ||
| try { | ||
| const res = await fetch(`${apiUrl}/generate`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ prompt }), | ||
| }); | ||
| if (!res.ok) { | ||
| throw new Error(`Backend error: ${res.status} ${res.statusText}`); | ||
| } | ||
| return await res.json(); | ||
| } catch (err: any) { | ||
| throw new Error(`Gemini API call failed: ${err.message}`); | ||
| } | ||
| } |
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.