Skip to content

Commit

Permalink
Refactor AI middleware and service to include firstContext parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
thadeucbr committed Sep 30, 2024
1 parent 143fee6 commit 7936323
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/middleware/ai/gpt.middleware.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import aiService from '../../service/ai/ai.service.js';

const context = 'You are an AI that manage api responses, you will receive body and header and analyse with your tools which function to call and send responses. Your response must always be in JSON in this format: { message, statusCode, status }. Important, don’t send JSON in the response';

const gptMiddleware = async (req, res, _next) => {

Check warning on line 5 in src/middleware/ai/gpt.middleware.js

View workflow job for this annotation

GitHub Actions / lint

'_next' is defined but never used

Check warning on line 5 in src/middleware/ai/gpt.middleware.js

View workflow job for this annotation

GitHub Actions / lint

'_next' is defined but never used
console.log(`Request Method: ${req.method}, Request URL: ${req.url}`);
console.log(`Request Body: ${JSON.stringify(req.body)}`);
console.log(`Request Query: ${JSON.stringify(req.query)}`);
console.log(`Request Headers: ${JSON.stringify(req.headers)}`);

const response = await aiService({ body: req.body, header: req.headers, query: req.query, method: req.method, url: req.url });
const response = await aiService({ body: req.body, header: req.headers, query: req.query, method: req.method, url: req.url, firstContext: context });

const { message, statusCode, status } = JSON.parse(response.content);
res.status(statusCode).json({ status, message });
Expand Down
4 changes: 2 additions & 2 deletions src/service/ai/ai.service.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import gpt from '../../helper/gpt/config/gpt.config.js';
import functions from '../../service/index.js';

const aiService = async ({ body, header, query, method, url }) => {
const aiService = async ({ body, header, query, method, url, firstContext }) => {
try {
const context = [
{
role: 'system',
content: 'You are an AI that manage api responses, you will receive body and header and analyse with your tools which function to call and send responses. Your response must always be in JSON in this format: { message, statusCode, status }. Important, don’t send JSON in the response'
content: firstContext
}
];

Expand Down

0 comments on commit 7936323

Please sign in to comment.