-
Notifications
You must be signed in to change notification settings - Fork 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
GEMINI SETUP, BETTER ERROR HANDLING AND COMPLETE CHANGE OF CHAT INTERFACE UI #38
base: main
Are you sure you want to change the base?
Changes from all commits
c6417ca
661d247
a2e6f8b
a56cab8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
DATABASE_URL= | ||
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= | ||
CLERK_SECRET_KEY= | ||
GEMINI_API_KEY= | ||
NEXT_PUBLIC_UMAMI_WEBSITE_ID |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"$schema": "https://ui.shadcn.com/schema.json", | ||
"style": "new-york", | ||
"rsc": true, | ||
"tsx": true, | ||
"tailwind": { | ||
"config": "tailwind.config.ts", | ||
"css": "src/app/globals.css", | ||
"baseColor": "neutral", | ||
"cssVariables": true, | ||
"prefix": "" | ||
}, | ||
"aliases": { | ||
"components": "@/components", | ||
"utils": "@/lib/utils", | ||
"ui": "@/components/ui", | ||
"lib": "@/lib", | ||
"hooks": "@/hooks" | ||
}, | ||
"iconLibrary": "lucide" | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
}, | ||
"dependencies": { | ||
"@clerk/nextjs": "^5.2.4", | ||
"@google/generative-ai": "^0.21.0", | ||
"@heroicons/react": "^2.1.5", | ||
"@mdx-js/loader": "^3.0.1", | ||
"@mdx-js/react": "^3.0.1", | ||
|
@@ -23,16 +24,20 @@ | |
"@radix-ui/react-dropdown-menu": "^2.1.1", | ||
"@radix-ui/react-slot": "^1.1.0", | ||
"@radix-ui/react-tabs": "^1.1.0", | ||
"@radix-ui/react-toast": "^1.2.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider consolidating toast libraries The PR adds both Also applies to: 50-50 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rahulsoni9321 check this pls let's just keep @radix-ui/react-toast There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"@types/dompurify": "^3.0.5", | ||
"@types/mdx": "^2.0.13", | ||
"@vercel/analytics": "^1.3.1", | ||
"@vercel/speed-insights": "^1.0.12", | ||
"axios": "^1.7.4", | ||
"class-variance-authority": "^0.7.0", | ||
"clsx": "^2.1.1", | ||
"dompurify": "^3.2.2", | ||
"framer-motion": "^10.18.0", | ||
"geist": "^1.3.1", | ||
"gray-matter": "^4.0.3", | ||
"highlight.js": "^11.10.0", | ||
"html-react-parser": "^5.1.18", | ||
"lucide-react": "^0.416.0", | ||
"mermaid": "^11.0.2", | ||
"next": "^14.2.13", | ||
|
@@ -45,8 +50,10 @@ | |
"react-intersection-observer": "^9.13.1", | ||
"react-type-animation": "^3.2.0", | ||
"rehype-highlight": "^7.0.0", | ||
"sonner": "^1.7.0", | ||
"tailwind-merge": "^2.4.0", | ||
"tailwindcss-animate": "^1.0.7", | ||
"ui": "github:shadcn/ui", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Version pin the shadcn/ui dependency Using a direct GitHub repository reference without version pinning (
Apply this change: - "ui": "github:shadcn/ui",
+ "ui": "github:shadcn/ui#<specific-commit-hash>",
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rahulsoni9321 check this one as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
"vaul": "^0.9.1" | ||
}, | ||
"devDependencies": { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,39 +1,54 @@ | ||||||||||||||||||||||||
import { NextResponse } from 'next/server'; | ||||||||||||||||||||||||
import axios from 'axios'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY; | ||||||||||||||||||||||||
const ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import { GoogleGenerativeAI, GoogleGenerativeAIError } from "@google/generative-ai"; | ||||||||||||||||||||||||
import { NextResponse } from "next/server"; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
const GEMINI_API_KEY = process.env.GEMINI_API_KEY; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add validation for GEMINI_API_KEY The API key is used without validation. If the environment variable is missing, this could lead to runtime errors. -const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
+const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
+if (!GEMINI_API_KEY) {
+ throw new Error('GEMINI_API_KEY environment variable is not set');
+} 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rahulsoni9321 pls check all the suggestions raised by @coderabbitai There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@tyaga001 made all the suggested changes by @coderabbitai and open a new pr (#49), Please have a look and let me know if changes needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rahulsoni9321, thank you for addressing the suggestions and opening PR #49. Let me know if you need any further assistance. |
||||||||||||||||||||||||
if (!GEMINI_API_KEY) { | ||||||||||||||||||||||||
throw new Error("Something went wrong while fetching API Keys.") | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY!); | ||||||||||||||||||||||||
const model = genAI.getGenerativeModel({ | ||||||||||||||||||||||||
model: "gemini-1.5-flash", | ||||||||||||||||||||||||
systemInstruction: "You're an AI assistant, Who only answers questions related to the blog post that have provided to you in the prompt.\n\nTry to Answer using numeric points for readability and Bold the title of the point. \n\n If the user wants help related to the topic mentioned in the blog you can answer that.\n\n If the question is not related to the blog post or the topic that is mentioned on the blog, Throw error response." | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
export async function POST(request: Request) { | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const { content, query } = await request.json(); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add input validation for request body The content and query are destructured without validation. Missing or invalid input could cause unexpected behavior. -const { content, query } = await request.json();
+const body = await request.json();
+
+if (!body.content || typeof body.content !== 'string' || !body.query || typeof body.query !== 'string') {
+ return NextResponse.json(
+ { error: 'Invalid request: content and query are required and must be strings', success: false },
+ { status: 400 }
+ );
+}
+
+const { content, query } = body; 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
try { | ||||||||||||||||||||||||
const { content, query } = await request.json(); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if (!ANTHROPIC_API_KEY) { | ||||||||||||||||||||||||
throw new Error('ANTHROPIC_API_KEY is not set'); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const response = await axios.post(ANTHROPIC_API_URL, { | ||||||||||||||||||||||||
model: "claude-3-sonnet-20240229", | ||||||||||||||||||||||||
messages: [ | ||||||||||||||||||||||||
{ role: "system", content: "You are an AI assistant that answers questions about blog posts." }, | ||||||||||||||||||||||||
{ role: "human", content: `Here's a blog post:\n\n${content}\n\nQuestion: ${query}` } | ||||||||||||||||||||||||
], | ||||||||||||||||||||||||
max_tokens: 1000, | ||||||||||||||||||||||||
}, { | ||||||||||||||||||||||||
headers: { | ||||||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||||||
'x-api-key': ANTHROPIC_API_KEY, | ||||||||||||||||||||||||
'anthropic-version': '2023-06-01' | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
const prompt = `Here's a blog post:\n\n${content}\n\nQuestion: ${query}`; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const result = await model.generateContent(prompt); | ||||||||||||||||||||||||
return NextResponse.json({ | ||||||||||||||||||||||||
answer: result.response.text(), | ||||||||||||||||||||||||
success: true | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Success field is inconsistently used across API endpoints The
🔗 Analysis chainVerify success response structure The response structure includes a success field as mentioned in the PR objectives. However, let's verify this is consistently used across the frontend. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for usage of the success field in frontend components
rg -l "success.*true|success.*false" --type ts --type tsx
Length of output: 87 Script: #!/bin/bash
# Search for usage of the success field in frontend components and API responses
# Using ripgrep without file type restrictions since .tsx extension wasn't recognized
rg -l "success.*true|success.*false"
Length of output: 69 Script: #!/bin/bash
# Search for API response handling in components
rg -A 5 "NextResponse.json"
Length of output: 3037 Script: #!/bin/bash
# Look for other API route handlers to check response consistency
fd "route.ts" --exec cat {}
Length of output: 3656 |
||||||||||||||||||||||||
catch (error: any) { | ||||||||||||||||||||||||
const isGoogleAIError = error instanceof GoogleGenerativeAIError; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
console.error('API Error:', { | ||||||||||||||||||||||||
type: isGoogleAIError ? 'GoogleGenerativeAI' : 'Unknown', | ||||||||||||||||||||||||
message: error.message, | ||||||||||||||||||||||||
timestamp: new Date().toISOString() | ||||||||||||||||||||||||
}); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const answer = response.data.content[0].text; | ||||||||||||||||||||||||
return NextResponse.json({ answer }); | ||||||||||||||||||||||||
} catch (error: any) { | ||||||||||||||||||||||||
console.error('Error in API:', error.response?.data || error.message); | ||||||||||||||||||||||||
return NextResponse.json( | ||||||||||||||||||||||||
{ error: 'Error processing request. Please try again.' }, | ||||||||||||||||||||||||
{ status: error.response?.status || 500 } | ||||||||||||||||||||||||
); | ||||||||||||||||||||||||
const clientMessage = isGoogleAIError | ||||||||||||||||||||||||
? 'The AI service is temporarily unavailable. Please try again later.' | ||||||||||||||||||||||||
: 'Internal Server Error. Please try again after some time.'; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return NextResponse.json( | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
error: clientMessage, | ||||||||||||||||||||||||
success: false | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
{ status: isGoogleAIError ? 503 : 500 } | ||||||||||||||||||||||||
); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Security vulnerabilities detected in DOMPurify dependency
Critical and high severity vulnerabilities have been found in DOMPurify. The current version 3.2.2 is affected by:
Action required:
Other dependencies (@google/generative-ai and html-react-parser) are at their latest versions and have no known vulnerabilities.
🔗 Analysis chain
Verify compatibility of new dependencies
Please ensure the versions of new dependencies are compatible:
@google/generative-ai@0.21.0
@types/dompurify@3.0.5
dompurify@3.2.2
html-react-parser@5.1.18
Also applies to: 28-28, 35-35, 40-40
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 1907