Skip to content
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

Refactor AI middleware and service to include firstContext parameter #5

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions .github/workflows/reviewer.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
name: AI Code Reviewer

name: Code Review with OpenAI
on:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the workflow should reflect its purpose more clearly. Consider renaming it to something that indicates it is specifically for AI code reviewing.

pull_request:
types:
- opened
- synchronize
permissions: write-all
jobs:
review:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The permissions key is set to write-all, which may grant excessive permissions. Consider specifying more restrictive permissions based on the actual needs of the workflow.

code_review:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
- name: Checkout repository
uses: actions/checkout@v3

- name: AI Code Reviewer
uses: Ostrich-Cyber-Risk/ai-codereviewer@main
- name: Code Review
uses: freeedcom/ai-codereviewer@main
with:
GITHUB_TOKEN: ${{ secrets.OCTOKIT_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4o-mini" # Optional: defaults to "gpt-4"
exclude: "**/*.json, **/*.md" # Optional: exclude patterns separated by commas
OPENAI_API_MODEL: "gpt-4o-mini"
exclude: "yarn.lock,dist/**"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exclusion pattern yarn.lock,dist/** may not be comprehensive enough. Ensure that all necessary files are excluded to avoid unintended reviews.

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
Loading