Skip to content

Commit

Permalink
Merge pull request #211 from serlo/refactor-ai
Browse files Browse the repository at this point in the history
refactor(ai): use body instead of query params for post request
  • Loading branch information
hugotiburtino authored Dec 20, 2024
2 parents 5010719 + 5212bdf commit d59eabe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/backend/ai-route-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './ai/generate-prompts'
import { changePrompt, changeSystemPrompt } from './ai/change-prompts'

const GenerateQuery = t.type({
const GenerateBody = t.type({
prompt: t.string,
before: t.string,
after: t.string,
Expand All @@ -28,11 +28,11 @@ export async function generateContent(req: Request, res: Response) {
return res.status(405).end()
}

if (!GenerateQuery.is(req.query)) {
if (!GenerateBody.is(req.body)) {
return res.status(400).send('Input vars are invalid')
}

if (req.query.prompt.trim() === '') {
if (req.body.prompt.trim() === '') {
return res.status(400).send('Missing prompt')
}

Expand All @@ -44,12 +44,12 @@ export async function generateContent(req: Request, res: Response) {
{
role: 'system',
content: generateBeforeAfterPrompt
.replace('{{before}}', req.query.before)
.replace('{{after}}', req.query.after),
.replace('{{before}}', req.body.before)
.replace('{{after}}', req.body.after),
},
{
role: 'user',
content: `Fulfill the following prompt of the user: ${req.query.prompt}`,
content: `Fulfill the following prompt of the user: ${req.body.prompt}`,
},
],
temperature: 0.25,
Expand All @@ -73,7 +73,7 @@ export async function generateContent(req: Request, res: Response) {
}
}

const ChangeQuery = t.type({
const ChangeBody = t.type({
prompt: t.string,
content: t.string,
})
Expand All @@ -86,11 +86,11 @@ export async function changeContent(req: Request, res: Response) {
return res.status(405).end()
}

if (!ChangeQuery.is(req.query)) {
if (!ChangeBody.is(req.body)) {
return res.status(400).send('Input vars are invalid')
}

if (req.query.prompt.trim() === '') {
if (req.body.prompt.trim() === '') {
return res.status(400).send('Missing prompt')
}

Expand All @@ -101,11 +101,11 @@ export async function changeContent(req: Request, res: Response) {
{ role: 'system', content: changeSystemPrompt },
{
role: 'system',
content: changePrompt.replace('{{content}}', req.query.content),
content: changePrompt.replace('{{content}}', req.body.content),
},
{
role: 'user',
content: `Use the following prompt for the change: ${req.query.prompt}`,
content: `Use the following prompt for the change: ${req.body.prompt}`,
},
],
temperature: 0.25,
Expand Down
1 change: 1 addition & 0 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const setup = async () => {
'/edusharing-embed/login',
'/edusharing-embed/done',
'/edusharing-embed/keys',
// disage ai to make it easier to develop, revert afterwards
'/ai/generate-content',
'/ai/change-content'
)
Expand Down

0 comments on commit d59eabe

Please sign in to comment.