feat: ai module (#1649)

* fix: pass `truncate`

Signed-off-by: Innei <i@innei.in>

* feat: add openai summary

Signed-off-by: Innei <i@innei.in>

* feat: ai list api

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-04-26 19:52:11 +08:00
committed by GitHub
parent d78a359a7d
commit c989a2a7b0
30 changed files with 7989 additions and 5764 deletions

View File

@@ -0,0 +1,61 @@
import type { IRequestAdapter } from '~/interfaces/adapter'
import type { IController } from '~/interfaces/controller'
import type { IRequestHandler } from '~/interfaces/request'
import type { HTTPClient } from '../core'
import type { AISummaryModel } from '../models/ai'
import { autoBind } from '~/utils/auto-bind'
declare module '../core/client' {
interface HTTPClient<
T extends IRequestAdapter = IRequestAdapter,
ResponseWrapper = unknown,
> {
ai: AIController<ResponseWrapper>
}
}
/**
* @support core >= 5.6.0
*/
export class AIController<ResponseWrapper> implements IController {
base = 'ai'
name = 'ai'
constructor(private client: HTTPClient) {
autoBind(this)
}
public get proxy(): IRequestHandler<ResponseWrapper> {
return this.client.proxy(this.base)
}
async getSummary({
articleId,
lang = 'zh-CN',
onlyDb,
}: {
articleId: string
lang?: string
onlyDb?: boolean
}) {
return this.proxy.summaries.article(articleId).get<AISummaryModel>({
params: {
lang,
onlyDb,
},
})
}
async generateSummary(articleId: string, lang = 'zh-CN', token = '') {
return this.proxy('generate-summary').post<AISummaryModel>({
params: {
token,
},
data: {
lang,
refId: articleId,
},
})
}
}