feat: add generate cook & intro
This commit is contained in:
40
server/api/recipes/text/generate.ts
Normal file
40
server/api/recipes/text/generate.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import consola from 'consola'
|
||||
import type { AIRecipeInfo } from '~/packages/ai/src'
|
||||
import { getAIRecipeInfo } from '~/packages/ai/src'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
const zhFoods = body.foods as string[]
|
||||
|
||||
consola.debug(zhFoods)
|
||||
|
||||
const data = await getAIRecipeInfo(zhFoods)
|
||||
const { content } = data
|
||||
|
||||
let unWrapperContent = content || ''
|
||||
const startPos = unWrapperContent.indexOf('{')
|
||||
const endPos = unWrapperContent.lastIndexOf('}')
|
||||
|
||||
if (startPos === -1 || endPos === -1) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(content)
|
||||
return
|
||||
}
|
||||
|
||||
unWrapperContent = unWrapperContent.slice(startPos, endPos + 1)
|
||||
unWrapperContent = (unWrapperContent || '{}')?.replace('```json\n', '').replace('```', '')
|
||||
|
||||
unWrapperContent = unWrapperContent.endsWith('}') ? unWrapperContent : `${unWrapperContent}}`
|
||||
|
||||
let coupletData: AIRecipeInfo | undefined
|
||||
try {
|
||||
coupletData = JSON.parse(unWrapperContent) as AIRecipeInfo
|
||||
}
|
||||
catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(content)
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
return coupletData
|
||||
})
|
||||
7
server/api/recipes/text/test.ts
Normal file
7
server/api/recipes/text/test.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
const startAt = Date.now()
|
||||
let count = 0
|
||||
|
||||
export default defineEventHandler(() => ({
|
||||
pageview: count++,
|
||||
startAt,
|
||||
}))
|
||||
Reference in New Issue
Block a user