-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed cleaning functions from Generate Description, added msw serve…
…r - missing tests
- Loading branch information
Showing
10 changed files
with
62 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
services/wiki/src/__tests__/mocks/huggingFaceHandlers/generateText.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { http, HttpResponse } from 'msw' | ||
|
||
export const generateTextHandler = http.post( | ||
process.env.HF_API_ENDPOINT as string, | ||
async ({ request }) => { | ||
const { inputs, parameters } = (await request.json()) as { | ||
inputs: string | ||
parameters: { max_length: number; temperature: number; top_p: number } | ||
} | ||
|
||
// Mock response logic | ||
if (!inputs || !parameters) { | ||
return HttpResponse.json({ message: 'Invalid request' }, { status: 400 }) | ||
} | ||
|
||
return HttpResponse.json( | ||
{ | ||
message: `Mocked response for: ${inputs}`, | ||
}, | ||
|
||
{ status: 200 } | ||
) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { generateTextHandler } from './generateText' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 9 additions & 35 deletions
44
services/wiki/src/controllers/resources/generateDescription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,28 @@ | ||
import Koa, { Middleware } from 'koa' | ||
import { HuggingFaceRepository } from '../../repository/huggingFace' | ||
import { getLanguageInput } from '../../helpers/getLanguageInput' | ||
import { TSupportedLanguage } from '../../db/knexTypes' | ||
import { | ||
DefaultError, | ||
MissingParamError, | ||
ServiceFail, | ||
} from '../../helpers/errors' | ||
import { DefaultError } from '../../helpers/errors' | ||
import generateHFDescriptionSchema from '../../schemas/huggingFace/generateHFDescription' | ||
|
||
export const generateDescription: Middleware = async (ctx: Koa.Context) => { | ||
const { title, url, topic } = ctx.request.body | ||
const { language } = ctx.query | ||
const { title, url, topic, language } = generateHFDescriptionSchema.parse( | ||
ctx.request.body | ||
) | ||
const huggingFaceRepository = new HuggingFaceRepository() | ||
try { | ||
if (!title || !url || !topic || !language) { | ||
throw new MissingParamError('required params') | ||
} | ||
const languageInput = language as TSupportedLanguage | ||
|
||
const input = getLanguageInput(languageInput, title, url, topic) | ||
const input = getLanguageInput(language, title, url, topic) | ||
|
||
const response = await huggingFaceRepository.getResponse({ | ||
input, | ||
title, | ||
url, | ||
topic, | ||
language: languageInput, | ||
language, | ||
}) | ||
|
||
const cleanResponse = await huggingFaceRepository.cleanHFResponse( | ||
[{ generated_text: response.generated_text }], | ||
languageInput, | ||
title, | ||
url, | ||
topic | ||
) | ||
|
||
if (!cleanResponse || !cleanResponse.generated_text) { | ||
throw new ServiceFail('Failed to process the response from external API') | ||
} | ||
|
||
ctx.status = 200 | ||
ctx.body = cleanResponse | ||
ctx.body = response | ||
} catch (error: any) { | ||
if (error instanceof DefaultError) { | ||
ctx.status = error.status | ||
ctx.body = { error: error.message } | ||
} else { | ||
ctx.status = 500 | ||
ctx.body = { error: error.message } | ||
} | ||
throw new DefaultError(error, 'Failed to generate description') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
services/wiki/src/schemas/huggingFace/generateHFDescription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import z from 'zod' | ||
import { TSupportedLanguage } from '../../db/knexTypes' | ||
|
||
const generateHFDescriptionSchema = z.object({ | ||
url: z.string(), | ||
title: z.string(), | ||
topic: z.string(), | ||
language: z.enum([ | ||
TSupportedLanguage.Spanish, | ||
TSupportedLanguage.English, | ||
TSupportedLanguage.Catalan, | ||
]), | ||
}) | ||
|
||
export default generateHFDescriptionSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters