Skip to content

Commit

Permalink
Merge pull request #158 from kalviumcommunity/enhancement/new-languag…
Browse files Browse the repository at this point in the history
…e-support

Add support for new AI language `promptv3`
  • Loading branch information
shashanksingh2002 authored Aug 8, 2024
2 parents 864ff82 + bde1e83 commit 10f8b1a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 47 deletions.
5 changes: 4 additions & 1 deletion configs/language.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { CPP, C, PYTHON, JAVA, NODEJS, RUBY, PROMPTV1, PROMPTV2 } = require('../enums/supportedLanguages')
const { CPP, C, PYTHON, JAVA, NODEJS, RUBY, PROMPTV1, PROMPTV2, PROMPTV3 } = require('../enums/supportedLanguages')
const ONE_MB = 1024 // ulimit uses Kilobyte as base unit
const ALLOWED_RAM = process.env.ALLOWED_RAM || 512

Expand Down Expand Up @@ -51,6 +51,9 @@ const LANGUAGES_CONFIG = {
[PROMPTV2]: {
model: 'gpt-3.5-turbo-1106',
},
[PROMPTV3]: {
model: 'gpt-4o-2024-05-13',
},
}

module.exports = { LANGUAGES_CONFIG }
1 change: 1 addition & 0 deletions enums/supportedLanguages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
RUBY: 'ruby',
PROMPTV1: 'promptv1',
PROMPTV2: 'promptv2',
PROMPTV3: 'promptv3',
MULTIFILE: 'multifile',
SQLITE3: 'sqlite3',
}
4 changes: 2 additions & 2 deletions services/code.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const os = require('os')
const fs = require('fs')
const path = require('path')
const sqlite3 = require('sqlite3').verbose()
const { PYTHON, PROMPTV1, PROMPTV2 } = require('../enums/supportedLanguages')
const { PYTHON, PROMPTV1, PROMPTV2, PROMPTV3 } = require('../enums/supportedLanguages')
const logger = require('../loader').helpers.l
const OpenAI = require('openai')
const openai = new OpenAI()
Expand Down Expand Up @@ -507,7 +507,7 @@ const execute = async (req, res) => {
errorMessage: '',
}

if ([PROMPTV1, PROMPTV2].includes(req.language)) {
if ([PROMPTV1, PROMPTV2, PROMPTV3].includes(req.language)) {
await _getAiScore(
LANGUAGES_CONFIG[req.language],
req.question,
Expand Down
101 changes: 57 additions & 44 deletions validators/code.validator.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
const Joi = require('joi')
const { PROMPTV1, PROMPTV2, MULTIFILE } = require('../enums/supportedLanguages')
const { FRONTEND_REACT_JASMINE, FRONTEND_STATIC_JASMINE } = require('../enums/supportedMultifileSetupTypes')
const {
PROMPTV1,
PROMPTV2,
PROMPTV3,
MULTIFILE,
} = require('../enums/supportedLanguages')
const {
FRONTEND_REACT_JASMINE,
FRONTEND_STATIC_JASMINE,
} = require('../enums/supportedMultifileSetupTypes')

const isValidForExecute = async (body) => {
const schema = Joi.object({
const _getBaseSchema = () => {
return Joi.object({
language: Joi.string().required(),
question: Joi.string().when('language', {
is: [PROMPTV1, PROMPTV2],
then: Joi.required(),
otherwise: Joi.optional(),
}),
userAnswer: Joi.string().when('language', {
is: [PROMPTV1, PROMPTV2],
then: Joi.required(),
otherwise: Joi.forbidden(),
}),
rubric: Joi.string().when('language', {
is: [PROMPTV1, PROMPTV2],
then: Joi.optional(),
otherwise: Joi.forbidden(),
}),
script: Joi.string()
.when('language', {
is: [PROMPTV1, PROMPTV2, MULTIFILE],
then: Joi.optional(),
otherwise: Joi.required(),
}),
url: Joi.string().trim()
.when('language', {
is: MULTIFILE,
then: Joi.required(),
otherwise: Joi.forbidden(),
}),
type: Joi.string().trim()
.valid(FRONTEND_REACT_JASMINE, FRONTEND_STATIC_JASMINE)
.when('language', {
is: MULTIFILE,
then: Joi.required(),
otherwise: Joi.forbidden(),
}),
non_editable_files: Joi.object().pattern(
Joi.string(),
Joi.string().pattern(/^[a-fA-F0-9]{64}$/)
).optional(),
points: Joi.number().integer().optional(), // totalScore
hasInputFiles: Joi.bool(),
args: Joi.string(),
stdin: Joi.string(),
})
}

const _getAiSchema = () => {
return _getBaseSchema().keys({
question: Joi.string().required(),
userAnswer: Joi.string().required(),
points: Joi.number().integer().optional(),
rubric: Joi.string().optional(),
})
}

const _getMultiFileSchema = () => {
return _getBaseSchema().keys({
url: Joi.string().trim().required(),
type: Joi.string()
.trim()
.valid(FRONTEND_REACT_JASMINE, FRONTEND_STATIC_JASMINE)
.required(),
non_editable_files: Joi.object()
.pattern(Joi.string(), Joi.string().pattern(/^[a-fA-F0-9]{64}$/))
.optional(),
})
}

const _getDefaultSchema = () => {
return _getBaseSchema().keys({
script: Joi.string().required(),
})
}

const _getSchema = (language) => {
switch (language) {
case PROMPTV1:
case PROMPTV2:
case PROMPTV3:
return _getAiSchema()
case MULTIFILE:
return _getMultiFileSchema()
default:
return _getDefaultSchema()
}
}

return schema.validateAsync(body)
const isValidForExecute = async (payload) => {
const schema = _getSchema(payload?.language)
return schema.validateAsync(payload)
}

module.exports = { isValidForExecute }

0 comments on commit 10f8b1a

Please sign in to comment.