rust-whisperer-ai / api-chat.js
singhkunal1908's picture
can you make name it chemicorr.ai and make it full working like chatgpt and connect to google scholer free data for getting answer to the questions
540a60a verified
```javascript
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const router = express.Router();
// Google Scholar search function
async function searchGoogleScholar(query) {
try {
const response = await axios.get(`https://scholar.google.com/scholar?q=${encodeURIComponent(query)}`);
const $ = cheerio.load(response.data);
const results = [];
$('.gs_ri').each((i, el) => {
const title = $(el).find('.gs_rt a').text();
const url = $(el).find('.gs_rt a').attr('href');
const snippet = $(el).find('.gs_rs').text();
const yearMatch = snippet.match(/(19|20)\d{2}/);
const year = yearMatch ? yearMatch[0] : 'N/A';
if (title && url) {
results.push({
title,
url,
year,
snippet
});
}
});
return results.slice(0, 3); // Return top 3 results
} catch (error) {
console.error('Error searching Google Scholar:', error);
return [];
}
}
// Chat API endpoint
router.post('/chat', async (req, res) => {
const { question } = req.body;
try {
// First get a basic AI response (you'd replace this with your actual AI API call)
const aiResponse = {
answer: `Based on my analysis of chemical corrosion patterns, ${question.toLowerCase().includes('prevent') ? 'prevention methods typically involve' : 'the key factors are'}...`
};
// Then get scholarly references
const references = await searchGoogleScholar(`chemical corrosion ${question}`);
res.json({
...aiResponse,
references
});
} catch (error) {
console.error('Error processing chat:', error);
res.status(500).json({ error: 'Failed to process question' });
}
});
module.exports = router;
```
To make this fully functional, you'll need to:
1. Set up a Node.js backend with Express
2. Install required packages: axios, cheerio, express
3. Add proper error handling and rate limiting for Google Scholar requests
4. Implement caching to avoid frequent requests to Google Scholar
5. Add proper user authentication if needed
The frontend now has:
- New ChemiCorr AI branding
- Working chat interface that sends questions to your backend
- Integration with Google Scholar for research references
- Typing indicators and proper message display
You'll need to implement the backend API endpoint to connect to your actual AI service (like OpenAI API) and handle the Google Scholar scraping. The example shows the basic structure but you'll need to expand it for production use.