Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat(ai): enhance AI service with optional system prompts for content (#239)
nevin57dfb12b1663
diff --git a/project/backend/apps/aitoearn-server/src/ai/ai.service.ts b/project/backend/apps/aitoearn-server/src/ai/ai.service.tsindex dd9102a5..86149883 100644--- a/project/backend/apps/aitoearn-server/src/ai/ai.service.ts+++ b/project/backend/apps/aitoearn-server/src/ai/ai.service.ts@@ -1,15 +1,14 @@-import { Injectable, Logger } from '@nestjs/common'+import { Injectable } from '@nestjs/common'import { UserType } from '@yikart/common'import OpenAI from 'openai'import { LogListResponseVo } from './ai.vo'-import { ChatModelConfigVo, ChatModelsQueryDto, ChatService, UserChatCompletionDto } from './core/chat'+import { ChatMessageDto, ChatModelConfigVo, ChatModelsQueryDto, ChatService, UserChatCompletionDto } from './core/chat'import { FireflycardResponseVo, ImageEditModelParamsVo, ImageEditModelsQueryDto, ImageGenerationModelParamsVo, ImageGenerationModelsQueryDto, ImageResponseVo, ImageService, Md2CardResponseVo, TaskStatusResponseVo, UserFireflyCardDto, UserImageEditDto, UserImageGenerationDto, UserMd2CardDto } from './core/image'import { LogListQueryDto, LogsService } from './core/logs'import { DashscopeImage2VideoRequestDto, DashscopeKeyFrame2VideoRequestDto, DashscopeTaskQueryDto, DashscopeTaskStatusResponseVo, DashscopeText2VideoRequestDto, DashscopeVideoGenerationResponseVo, KlingImage2VideoRequestDto, KlingMultiImage2VideoRequestDto, KlingTaskQueryDto, KlingTaskStatusResponseVo, KlingText2VideoRequestDto, KlingVideoGenerationResponseVo, ListVideoTasksResponseVo, UserListVideoTasksQueryDto, UserVideoGenerationRequestDto, UserVideoTaskQueryDto, VideoGenerationModelParamsVo, VideoGenerationModelsQueryDto, VideoGenerationResponseVo, VideoService, VideoTaskStatusResponseVo, VolcengineGenerationRequestDto, VolcengineTaskQueryDto, VolcengineTaskStatusResponseVo, VolcengineVideoGenerationResponseVo } from './core/video'@Injectable()export class AiService {- private readonly logger = new Logger(AiService.name)constructor(private readonly chatService: ChatService,private readonly logsService: LogsService,@@ -298,78 +297,81 @@ export class AiService {}// Intelligent image content generation- async imgContentByAi(user: { userId: string, userType: UserType }, model: string, imgUrl: string, prompt: string): Promise<string> {+ async imgContentByAi(user: { userId: string, userType: UserType }, model: string, imgUrl: string, prompt: string, option?: {+ systemPrompt?: string+ }): Promise<string> {const { userId, userType } = user- const systemContent = `Generate copy based on the pictures and prompt words, as well as the reference titles and contents. Just return the copy.`- const request: UserChatCompletionDto = {- userId,- userType,- model,- messages: [+ const messages: ChatMessageDto[] = []+ if (option?.systemPrompt) {+ messages.push({+ role: 'system',+ content: option.systemPrompt,+ })+ }++ messages.push({+ role: 'user',+ content: [{- role: 'system',- content: systemContent,+ type: 'image_url',+ image_url: {+ url: imgUrl,+ },},{- role: 'user',- content: [- {- type: 'image_url',- image_url: {- url: imgUrl,- },- },- {- type: 'text',- text: prompt,- },- ],+ type: 'text',+ text: prompt,},],- }+ })- try {- const res = await this.userAiChat(request)- return res.content as string || ''- }- catch (error) {- this.logger.log({ data: error, path: '======= imgContentByAi error =======' })- return ''+ const request: UserChatCompletionDto = {+ userId,+ userType,+ model,+ messages,}++ const res = await this.userAiChat(request)+ return res.content as string || ''}// Intelligent video content generation- async videoContentByAi(user: { userId: string, userType: UserType }, model: string, videoUrl: string, prompt: string): Promise<string> {+ async videoContentByAi(user: { userId: string, userType: UserType }, model: string, videoUrl: string, prompt: string, option?: {+ systemPrompt?: string+ }): Promise<string> {const { userId, userType } = user- const systemContent = `Generate copy based on the video and prompt words, as well as the reference titles and contents. Just return the copy.`+ const messages: ChatMessageDto[] = []+ if (option?.systemPrompt) {+ messages.push({+ role: 'system',+ content: option.systemPrompt,+ })+ }- const request: UserChatCompletionDto = {- userId,- userType,- model,- messages: [+ messages.push({+ role: 'user',+ content: [{- role: 'system',- content: systemContent,+ type: 'video',+ video_url: {+ url: videoUrl,+ },},{- role: 'user',- content: [- {- type: 'video',- video_url: {- url: videoUrl,- },- },- {- type: 'text',- text: prompt,- },- ],+ type: 'text',+ text: prompt,},],+ })++ const request: UserChatCompletionDto = {+ userId,+ userType,+ model,+ messages,}const res = await this.userAiChat(request)@@ -379,30 +381,26 @@ export class AiService {// Return text content based on imageasync getContentByAi(user: { userId: string, userType: UserType }, model: string, prompt: string, option: {coverUrl?: string- title?: string- desc?: string- max?: number- language?: string+ systemPrompt?: string}): Promise<string> {const { userId, userType } = user- if (!option.desc && !option.coverUrl)+ if (!prompt && !option.coverUrl)return ''- const systemContent = `Based on the cover image, refer to the title and the original content to generate beautiful copy. Reply in ${option.language || 'English'} and the content of your reply should not exceed ${option.max || 100} words. Just return the copy`-- let text = `prompt${prompt}.`- if (option.title)- text += `Reference Title: ${option.title}`- if (option.desc)- text += `Reference description: ${option.desc}`+ const messages: ChatMessageDto[] = []+ if (option.systemPrompt) {+ messages.push({+ role: 'system',+ content: option.systemPrompt,+ })+ }const content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = [{type: 'text',- text,+ text: prompt,},]-if (option.coverUrl) {content.push({type: 'image_url',@@ -411,20 +409,17 @@ export class AiService {},})}++ messages.push({+ role: 'user',+ content,+ })+const request: UserChatCompletionDto = {userId,userType,model,- messages: [- {- role: 'system',- content: systemContent,- },- {- role: 'user',- content,- },- ],+ messages,}const res = await this.userAiChat(request)@@ -432,42 +427,34 @@ export class AiService {}// Return text content based on image- async getTitleByAi(user: { userId: string, userType: UserType }, model: string, desc: string, option: {- title?: string- max?: number- language?: string- }): Promise<string> {+ async getTitleByAi(user: { userId: string, userType: UserType }, model: string, prompt: string): Promise<string> {const { userId, userType } = user- if (!desc)+ if (!prompt)return ''+ const systemContent = `Generate the content title based on the reference title and the original content. Keep the same language as the content, Just return the title text`- const systemContent = `Generate the content title based on the reference title and the original content. Please reply in ${option.language || 'English'} and the content of your reply should not exceed ${option.max || 100} words. Just return the title text`-- let text = `Original content: ${desc}. `- if (option.title)- text += `Reference Title: ${option.title}`+ const messages: ChatMessageDto[] = [{+ role: 'system',+ content: systemContent,+ }]const content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = [{type: 'text',- text,+ text: prompt,},]+ messages.push({+ role: 'user',+ content,+ })+const request: UserChatCompletionDto = {userId,userType,model,- messages: [- {- role: 'system',- content: systemContent,- },- {- role: 'user',- content,- },- ],+ messages,}const res = await this.userAiChat(request)diff --git a/project/backend/apps/aitoearn-server/src/ai/core/chat/chat.dto.ts b/project/backend/apps/aitoearn-server/src/ai/core/chat/chat.dto.tsindex 062a5dad..00bc90a5 100644--- a/project/backend/apps/aitoearn-server/src/ai/core/chat/chat.dto.ts+++ b/project/backend/apps/aitoearn-server/src/ai/core/chat/chat.dto.ts@@ -32,6 +32,7 @@ const chatMessageSchema = z.object({role: z.string().describe('消息角色'),content: z.union([z.string(), z.array(messageContentComplexSchema)]).describe('消息内容'),})+export class ChatMessageDto extends createZodDto(chatMessageSchema) {}const chatCompletionDtoSchema = z.object({messages: z.array(chatMessageSchema).min(1).describe('消息列表'),diff --git a/project/backend/apps/aitoearn-server/src/content/common.ts b/project/backend/apps/aitoearn-server/src/content/common.tsindex e5c1bfd9..0d388d52 100644--- a/project/backend/apps/aitoearn-server/src/content/common.ts+++ b/project/backend/apps/aitoearn-server/src/content/common.ts@@ -110,6 +110,7 @@ export interface MaterialTask {type: MaterialTypeaiModelTag: stringprompt: string // Prompt+ systemPrompt?: string // System promptcoverGroup?: stringmediaGroups: string[]option?: Record<string, any> // Advanced settingsdiff --git a/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts b/project/backend/apps/aitoearn-server/src/content/dto/material.dto.tsindex b3585722..45af486d 100644--- a/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts+++ b/project/backend/apps/aitoearn-server/src/content/dto/material.dto.ts@@ -30,6 +30,7 @@ export const createMaterialTaskSchema = z.object({num: z.number().describe('生成数量'),aiModelTag: z.string().describe('AI模型tag'),prompt: z.string().describe('提示词'),+ systemPrompt: z.string().optional().describe('系统提示词'),mediaGroups: z.array(z.string()).min(1).max(5).describe('媒体组ID列表'),coverGroup: z.string().describe('参考描述'),option: z.any().optional().describe('高级设置'),diff --git a/project/backend/apps/aitoearn-server/src/content/material-generate.consumer.ts b/project/backend/apps/aitoearn-server/src/content/material-generate.consumer.tsindex f0a4ad27..b0d31248 100644--- a/project/backend/apps/aitoearn-server/src/content/material-generate.consumer.ts+++ b/project/backend/apps/aitoearn-server/src/content/material-generate.consumer.ts@@ -1,10 +1,3 @@-/*- * @Author: nevin- * @Date: 2024-07-03 15:16:12- * @LastEditTime: 2025-02-10 17:18:50- * @LastEditors: nevin- * @Description: 素材生成队列- */import {OnWorkerEvent,Processor,@@ -44,7 +37,7 @@ export class MaterialGenerateConsumer extends WorkerHost {const taskInfo = await this.materialTaskService.getInfo(job.data.taskId)this.logger.log({data: taskInfo,- message: '任务开始执行',+ message: 'Task execution started',path: 'process --------- 0',})if (@@ -56,7 +49,7 @@ export class MaterialGenerateConsumer extends WorkerHost {) {this.logger.log({data: 0,- message: '任务退出执行',+ message: 'Task execution skipped',path: 'process --------- 1',})void job.isCompleted()@@ -67,7 +60,7 @@ export class MaterialGenerateConsumer extends WorkerHost {= await this.materialTaskService.doCreateTask(taskInfo)this.logger.log({data: { status, message },- message: '任务执行结果',+ message: 'Task execution result',path: 'process --------- 2',})if (status === -1) {diff --git a/project/backend/apps/aitoearn-server/src/content/materialTask.service.ts b/project/backend/apps/aitoearn-server/src/content/materialTask.service.tsindex 5550c508..2bf35328 100644--- a/project/backend/apps/aitoearn-server/src/content/materialTask.service.ts+++ b/project/backend/apps/aitoearn-server/src/content/materialTask.service.ts@@ -135,6 +135,7 @@ export class MaterialTaskService {type: groupInfo.type,aiModelTag: inData.aiModelTag,prompt: inData.prompt,+ systemPrompt: inData.systemPrompt,coverGroup: inData.coverGroup,mediaGroups: inData.mediaGroups,option: inData.option,@@ -167,6 +168,9 @@ export class MaterialTaskService {type: MediaType,fileUrl: string,prompt: string,+ option?: {+ systemPrompt?: string+ },) {if (type === MediaType.IMG) {const res = await this.aiService.imgContentByAi(@@ -174,6 +178,7 @@ export class MaterialTaskService {model,fileUrl,prompt,+ option,)return res}@@ -184,6 +189,7 @@ export class MaterialTaskService {model,fileUrl,prompt,+ option,)return res}@@ -200,29 +206,20 @@ export class MaterialTaskService {prompt: string,option: {coverUrl?: string- title?: string- desc?: string- max?: number- language?: string+ systemPrompt?: string},- coverUrl?: string,) {const res = {- title: option.title,- content: option.desc,+ title: '',+ content: '',}- const content = await this.aiService.getContentByAi(user, model, prompt, {- ...option,- coverUrl,- })+ const content = await this.aiService.getContentByAi(user, model, prompt, option)if (!content)return resres.content = content- const title = await this.aiService.getTitleByAi(user, model, content, {- ...option,- })+ const title = await this.aiService.getTitleByAi(user, model, content)if (!title)return resres.title = title@@ -233,16 +230,7 @@ export class MaterialTaskService {* Get content sections*/async getContentItems(- taskInfo: {- mediaUrlMap: MediaUrlInfo[][]- title?: string- desc?: string- textMax?: number- language?: string- userId: string- userType: UserType- aiModelTag: string- },+ taskInfo: MaterialTask,) {const res: {status: -1 | 0 | 1@@ -273,8 +261,11 @@ export class MaterialTaskService {{ userId: taskInfo.userId, userType: taskInfo.userType },taskInfo.aiModelTag,theOne.type,- await buildUrl(config.awsS3.endpoint, theOne.url),- taskInfo.aiModelTag,+ buildUrl(config.awsS3.endpoint, theOne.url),+ taskInfo.prompt,+ {+ systemPrompt: taskInfo.systemPrompt,+ },)if (!content) {res.status = -1@@ -378,7 +369,7 @@ export class MaterialTaskService {const {status,message,- data: { content: dbDesc, mediaList },+ data: { mediaList },} = await this.getContentItems(taskInfo)if (status === -1) {if (!preview) {@@ -414,12 +405,11 @@ export class MaterialTaskService {taskInfo.aiModelTag,taskInfo.prompt,{- coverUrl: theOneCover.url,- desc: dbDesc,+ coverUrl: theOneCover.url+ ? buildUrl(config.awsS3.endpoint, theOneCover.url)+ : undefined,+ systemPrompt: taskInfo.systemPrompt,},- theOneCover.url- ? buildUrl(config.awsS3.endpoint, theOneCover.url)- : undefined,)// Update draft informationdiff --git a/project/backend/libs/mongodb/src/schemas/materialTask.schema.ts b/project/backend/libs/mongodb/src/schemas/materialTask.schema.tsindex 4b5ea53b..a0a0ffdd 100644--- a/project/backend/libs/mongodb/src/schemas/materialTask.schema.ts+++ b/project/backend/libs/mongodb/src/schemas/materialTask.schema.ts@@ -89,6 +89,11 @@ export class MaterialTask extends WithTimestampSchema {})prompt: string // Prompt+ @Prop({+ required: false,+ })+ systemPrompt?: string // System prompt+@Prop({required: false,})