Skip to content

Commit

Permalink
Added help endpoint w/ code from equalify-llm-api repo, using OpenAI SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Apr 17, 2024
1 parent 33349ce commit 8f13660
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ WEB_CLIENT_ID=ggct1ke14ncd82ivh5su5vn4j
DB_HOST=localhost:5432
DB_NAME=equalify
DB_USER=postgres
DB_PASSWORD=password
DB_PASSWORD=password
OPENAI_API_KEY=sk-key
OPENAI_ORG_ID=org-id
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"esbuild": "^0.20.1",
"fastify": "^4.26.2",
"graphql": "^16.8.1",
"openai": "^4.36.0",
"pg": "^8.11.5",
"postgraphile": "^4.13.0",
"postgraphile-plugin-connection-filter": "^2.3.0",
"serverless-postgres": "^2.1.0"
"serverless-postgres": "^2.1.0",
"string-strip-html": "^13.4.8"
},
"resolutions": {
"graphql": "16.x"
Expand Down
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fastify from 'fastify';
import { getReports, getResults, graphql } from './routes/index.js';
import { getReports, getResults, graphql, help } from './routes/index.js';
import { CognitoJwtVerifier } from 'aws-jwt-verify';
export const fastify = Fastify({ logger: true });
const cognitoJwtVerifier = CognitoJwtVerifier.create({
Expand All @@ -22,6 +22,7 @@ fastify.addHook('preHandler', async (request, reply) => {
fastify.post('/graphql', {}, async (request, reply) => graphql({ request, reply }));
fastify.get('/get/results', {}, async (request, reply) => getResults({ request, reply }));
fastify.get('/get/reports', {}, async (request, reply) => getReports({ request, reply }));
fastify.post('/help', {}, async (request, reply) => help({ request, reply }));

fastify.listen({ port: 3000 }, (err) => {
console.log(`Server listening on ${fastify.server.address().port}`)
Expand Down
4 changes: 2 additions & 2 deletions src/routes/getReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const getReports = async ({ request, reply }) => {
}
}`,
variables: {
first: parseInt(request.query.first),
offset: parseInt(request.query.offset),
first: parseInt(request.query.first ?? 100),
offset: parseInt(request.query.offset ?? 0),
},
}))?.data;

Expand Down
60 changes: 60 additions & 0 deletions src/routes/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { stripHtml } from 'string-strip-html';
import { openai } from '../utils/index.js';

export const help = async ({ request, reply }) => {
const { reportId, messageId, dequeURL, message: accessibilityIssueToFix, codeSnippet: originalCodeSnippet, sourceUrl, status } = request.body;
const accessibilityDocumentation = stripHtml(await (await fetch(dequeURL)).text()).result;
const originalCode = await (await fetch(sourceUrl)).text();

const openaiRawResponse = await openai.chat.completions.create({
model: 'gpt-4-turbo-preview',
tool_choice: { type: 'function', function: { name: 'suggested_fix' } },
tools: [{
type: 'function',
function: {
name: 'suggested_fix',
description: 'Suggest an accessibility fix for the given code snippet and accessibility rule',
parameters: {
type: 'object',
description: 'A suggested fix',
properties: {
suggested_replacement_code: {
type: 'string',
description: 'The suggested replacement code',
},
how_to_implement: {
type: 'string',
description: 'How to implement the suggested code',
},
diff_view: {
type: 'string',
description: 'A diff view of the old and new code with character and line positions listed on the left',
},
},
required: ['suggested_code', 'how_to_implement', 'diff_view'],
},
}
}],
messages: [
{ role: 'system', content: `You are an LLM bot running for Equalify, a platform designed to identify accessibility issues for developers to fix.` },
{
role: 'user', content: `Suggest replacement code to fix the accessibility issue identified by Equalify. You may use the accessibility documentation to assist in your resolution:
\`\`\`json
${JSON.stringify({
originalCode,
originalCodeSnippet,
accessibilityIssueToFix,
accessibilityDocumentation,
})}
\`\`\`
`},
]
});

const suggestedFix = JSON.parse(openaiRawResponse.choices?.[0]?.message?.tool_calls?.[0]?.function?.arguments ?? '{}');

return {
...suggestedFix,
originalCode,
};
}
3 changes: 2 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './getResults.js'
export * from './graphql.js'
export * from './getReports.js'
export * from './getReports.js'
export * from './help.js'
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './graphqlQuery.js'
export * from './pgClient.js'
export * from './pgClient.js'
export * from './openai.js'
2 changes: 2 additions & 0 deletions src/utils/openai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import OpenAI from 'openai';
export const openai = new OpenAI();
Loading

0 comments on commit 8f13660

Please sign in to comment.