-
Notifications
You must be signed in to change notification settings - Fork 2
Added a very basic groq backend for AI support #1
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
Open
QuantumChemist
wants to merge
21
commits into
SimonNir:main
Choose a base branch
from
QuantumChemist:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
727dbe2
Implement Groq backend with API integration and error handling
QuantumChemist b03779d
Add .env to .gitignore to prevent sensitive data exposure
QuantumChemist 2984c79
Add async function to handle Groq API requests
QuantumChemist e316e4e
Add AI Q&A feature to sidebar for term inquiries
QuantumChemist 9aa0517
Enhance AI Q&A input styling for better visibility and user experience
QuantumChemist 33d8f93
Add package.json with initial dependencies for the project
QuantumChemist d6a2b14
Add dotenv dependency to package.json and package-lock.json
QuantumChemist 5958cb0
Refactor Groq API request handling and improve logging for better deb…
QuantumChemist 1e4e46b
Update AI Q&A answer styling for improved readability
QuantumChemist 5d661d6
Refactor Groq API request to include system prompt and improve messag…
QuantumChemist 81f8450
Enhance Q&A functionality by adding in-memory history tracking and im…
QuantumChemist c2d73f1
Refactor system prompt to remove redundant phrasing for clarity
QuantumChemist b7c1865
Refactor chat history construction to streamline message handling for…
QuantumChemist 7373ab8
Refactor Groq API handling to improve system prompt and increase Q&A …
QuantumChemist 7f30d4e
Improve sidebar button layout and styling for better usability
QuantumChemist 85c7094
Update sidebar header layout to improve readability
QuantumChemist 438e11a
Update input placeholder to indicate local server requirement for AI …
QuantumChemist fc4f160
Add experimental AI Q&A feature with local server requirement and usa…
QuantumChemist ef98a8d
Update AI Q&A section to clarify local server requirement in the UI
QuantumChemist 12855dd
Reduce in-memory history limit for Q&A from 12 to 3
QuantumChemist 51a1dab
Merge branch 'SimonNir:main' into main
QuantumChemist 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .env | ||
| node_modules/ |
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,84 @@ | ||
| const express = require('express'); | ||
| const cors = require('cors'); | ||
| const axios = require('axios'); | ||
| require('dotenv').config(); | ||
|
|
||
|
|
||
| const app = express(); | ||
| app.use(cors()); | ||
| app.use(express.json()); | ||
|
|
||
| // In-memory history of last 3 Q&A | ||
| const HISTORY_LIMIT = 3; | ||
| const aiHistory = []; | ||
|
|
||
| app.post('/ask-groq', async (req, res) => { | ||
| const { question } = req.body; | ||
| console.log('[ask-groq] Received question:', question); | ||
|
|
||
|
|
||
| // Improved system prompt for Groq | ||
| const systemPrompt = `You are HypeLessLi, an assistant that helps users critically read scientific texts by highlighting hype-like, subjective, promotional, and vague terms. You provide clear, concise explanations for why a term is considered hype, and always suggest less hyped, more objective alternatives for any term or phrase the user asks about. If the user does not specify, always include a suggestion for a more objective or neutral alternative. If the user asks a follow-up, use the previous questions and answers in this conversation for context. Always try to resolve ambiguous or short follow-ups by referencing the last exchange.`; | ||
|
|
||
| // Helper: is the question a likely follow-up (short or vague)? | ||
| function isLikelyFollowup(q) { | ||
| return q.trim().length < 20 || /^(what|which|and|also|more|how about|the second|the first|that one|this one|another|other|else|too|again|continue|next|previous|last|first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|it|he|she|they|him|her|them|those|these|such|so|then|now|why|how|where|when|who|whose|whom|is|are|was|were|do|does|did|can|could|should|would|will|shall|may|might|must|has|have|had|does|did|doesn't|didn't|isn't|aren't|wasn't|weren't|hasn't|haven't|hadn't|won't|wouldn't|can't|couldn't|shouldn't|mightn't|mustn't|doesnt|didnt|isnt|arent|wasnt|werent|hasnt|havent|hadnt|wont|wouldnt|cant|couldnt|shouldnt|mightnt|mustnt)\b/i.test(q.trim()); | ||
| } | ||
|
|
||
| // Build chat history for Groq (up to HISTORY_LIMIT) | ||
| let historyPairs = aiHistory.slice(-HISTORY_LIMIT); | ||
|
|
||
| // If the new question is a likely follow-up, prepend the last Q&A as context | ||
| let chatHistory = [ { role: 'system', content: systemPrompt } ]; | ||
| if (isLikelyFollowup(question) && historyPairs.length > 0) { | ||
| const last = historyPairs[historyPairs.length - 1]; | ||
| chatHistory.push({ role: 'user', content: last.question }); | ||
| chatHistory.push({ role: 'assistant', content: last.answer }); | ||
| } | ||
| chatHistory = chatHistory.concat( | ||
| historyPairs.flatMap(pair => [ | ||
| { role: 'user', content: pair.question }, | ||
| { role: 'assistant', content: pair.answer } | ||
| ]) | ||
| ); | ||
| chatHistory.push({ role: 'user', content: question }); | ||
|
|
||
| try { | ||
| console.log('[ask-groq] Sending request to Groq API...'); | ||
| const groqRes = await axios.post( | ||
| 'https://api.groq.com/openai/v1/chat/completions', | ||
| { | ||
| model: 'llama-3.3-70b-versatile', | ||
| messages: chatHistory | ||
| }, | ||
| { | ||
| headers: { | ||
| 'Authorization': `Bearer ${process.env.GROQ_API_KEY}`, | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| } | ||
| ); | ||
| console.log('[ask-groq] Groq API response status:', groqRes.status); | ||
| let answer = ''; | ||
| if (groqRes.data && groqRes.data.choices && groqRes.data.choices[0]) { | ||
| answer = groqRes.data.choices[0].message.content; | ||
| console.log('[ask-groq] Groq API answer:', answer.slice(0, 100), '...'); | ||
| } else { | ||
| console.log('[ask-groq] Groq API response missing expected data:', groqRes.data); | ||
| answer = '[No answer returned]'; | ||
| } | ||
| // Add to history (keep only last HISTORY_LIMIT) | ||
| aiHistory.push({ question, answer, ts: new Date().toISOString() }); | ||
| if (aiHistory.length > HISTORY_LIMIT) aiHistory.shift(); | ||
| res.json({ answer }); | ||
| } catch (err) { | ||
| console.error('[ask-groq] Error from Groq API:', err.response ? err.response.data : err.message); | ||
| res.status(500).json({ error: 'Groq API error', details: err.message }); | ||
| } | ||
| // Endpoint to get last 7 Q&A | ||
| app.get('/ask-groq/history', (req, res) => { | ||
| res.json({ history: aiHistory }); | ||
| }); | ||
| }); | ||
|
|
||
| app.listen(3001, () => console.log('Groq backend running on port 3001')); |
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.
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.
it will be crucial to generalize this and make it independent from groq