Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
refactor(ai): migrate AI content generation methods to Content (#264)
nevin2814bdc6b7a0
diff --git a/project/backend/apps/aitoearn-server/src/ai/chat/chat.service.ts b/project/backend/apps/aitoearn-server/src/ai/chat/chat.service.tsindex d37ab0d4..f4b9243f 100644--- a/project/backend/apps/aitoearn-server/src/ai/chat/chat.service.ts+++ b/project/backend/apps/aitoearn-server/src/ai/chat/chat.service.ts@@ -6,7 +6,6 @@ import { AiLogChannel, AiLogRepository, AiLogStatus, AiLogType } from '@yikart/mimport { BigNumber } from 'bignumber.js'import dayjs from 'dayjs'import _ from 'lodash'-import OpenAI from 'openai'import { PointsService } from '../../user/points.service'import { UserService } from '../../user/user.service'import { OpenaiService } from '../libs/openai'@@ -174,204 +173,4 @@ export class ChatService {return this.modelsConfigService.config.chat}-- // 智能图片文案- async imgContentByAi(user: { userId: string, userType: UserType }, model: string, imgUrl: string, prompt: string, option?: {- title?: string- desc?: string- max?: number- language?: string- systemPrompt?: string- }): Promise<string> {- const { userId, userType } = user-- const systemContent = option?.systemPrompt || `Generate copy based on the pictures and prompt words, as well as the reference titles and contents. Reply in ${option?.language || 'English'}. The reply should not exceed ${option?.max || 100} characters. 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 request: UserChatCompletionDto = {- userId,- userType,- model,- messages: [- {- role: 'system',- content: systemContent,- },- {- role: 'user',- content: [- {- type: 'image_url',- image_url: {- url: imgUrl,- },- },- {- type: 'text',- text,- },- ],- },- ],- }-- try {- const res = await this.userChatCompletion(request)- return res.content as string || ''- }- catch (error) {- this.logger.log({ data: error, path: '======= imgContentByAi error =======' })- return ''- }- }-- // 智能视频文案- async videoContentByAi(user: { userId: string, userType: UserType }, model: string, videoUrl: string, prompt: string, option?: {- title?: string- desc?: string- max?: number- language?: string- systemPrompt?: string- }): Promise<string> {- const { userId, userType } = user-- const systemContent = option?.systemPrompt || `Generate copy based on the video and prompt words, as well as the reference titles and contents. Reply in ${option?.language || 'English'}. The reply should not exceed ${option?.max || 100} characters. 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 request: UserChatCompletionDto = {- userId,- userType,- model,- messages: [- {- role: 'system',- content: systemContent,- },- {- role: 'user',- content: [- {- type: 'video',- video_url: {- url: videoUrl,- },- },- {- type: 'text',- text,- },- ],- },- ],- }-- const res = await this.userChatCompletion(request)- return res.content as string || ''- }-- // 根据图片返回文字内容- async getContentByAi(user: { userId: string, userType: UserType }, model: string, prompt: string, option: {- coverUrl?: string- title?: string- desc?: string- max?: number- language?: string- }): Promise<string> {- const { userId, userType } = user- if (!option.desc && !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 content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = [- {- type: 'text',- text,- },- ]-- if (option.coverUrl) {- content.push({- type: 'image_url',- image_url: {- url: option.coverUrl,- },- })- }- const request: UserChatCompletionDto = {- userId,- userType,- model,- messages: [- {- role: 'system',- content: systemContent,- },- {- role: 'user',- content,- },- ],- }-- const res = await this.userChatCompletion(request)- return res.content as string || ''- }-- // 根据图片返回文字内容- async getTitleByAi(user: { userId: string, userType: UserType }, model: string, desc: string, option: {- title?: string- max?: number- language?: string- }): Promise<string> {- const { userId, userType } = user- if (!desc)- return ''-- 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 content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = [- {- type: 'text',- text,- },- ]-- const request: UserChatCompletionDto = {- userId,- userType,- model,- messages: [- {- role: 'system',- content: systemContent,- },- {- role: 'user',- content,- },- ],- }-- const res = await this.userChatCompletion(request)- return res.content as string || ''- }}diff --git a/project/backend/apps/aitoearn-server/src/content/content.module.ts b/project/backend/apps/aitoearn-server/src/content/content.module.tsindex 0c64e605..7d3e4bc9 100644--- a/project/backend/apps/aitoearn-server/src/content/content.module.ts+++ b/project/backend/apps/aitoearn-server/src/content/content.module.ts@@ -12,6 +12,7 @@ import { MediaController } from './media.controller'import { MediaService } from './media.service'import { MediaGroupController } from './mediaGroup.controller'import { MediaGroupService } from './mediaGroup.service'+import { ContentAiUtil } from './util/ai.util'@Global()@Module({@@ -20,7 +21,7 @@ import { MediaGroupService } from './mediaGroup.service'AiModule,],controllers: [MediaController, MediaGroupController, MaterialGroupController, MaterialController],- providers: [MediaService, MediaGroupService, MaterialGroupService, MaterialService, MaterialTaskService, MaterialGenerateConsumer],+ providers: [MediaService, MediaGroupService, MaterialGroupService, MaterialService, MaterialTaskService, MaterialGenerateConsumer, ContentAiUtil],exports: [MediaService, MediaGroupService, MaterialGroupService, MaterialService, MaterialTaskService],})export class ContentModule { }diff --git a/project/backend/apps/aitoearn-server/src/content/materialTask.service.ts b/project/backend/apps/aitoearn-server/src/content/materialTask.service.tsindex 24b94676..57b1fec1 100644--- a/project/backend/apps/aitoearn-server/src/content/materialTask.service.ts+++ b/project/backend/apps/aitoearn-server/src/content/materialTask.service.ts@@ -11,7 +11,6 @@ import { buildUrl } from '@yikart/aws-s3'import { AppException, ResponseCode, UserType } from '@yikart/common'import { MaterialStatus, MaterialTaskRepository, MaterialType } from '@yikart/mongodb'import { RedisService } from '@yikart/redis'-import { ChatService } from '../ai/chat'import { config } from '../config'import { MaterialMedia, MaterialTask, MediaType, MediaUrlInfo, NewMaterial, NewMaterialTask } from './common'import { CreateMaterialTaskDto } from './dto/material.dto'@@ -19,6 +18,7 @@ import { MaterialService } from './material.service'import { MaterialGroupService } from './materialGroup.service'import { MediaService } from './media.service'import { MediaGroupService } from './mediaGroup.service'+import { ContentAiUtil } from './util/ai.util'export const MaterialMediaTypeMap = new Map<MaterialType, MediaType>([[MaterialType.VIDEO, MediaType.VIDEO],@@ -31,7 +31,7 @@ export class MaterialTaskService {constructor(readonly redisService: RedisService,private readonly materialTaskRepository: MaterialTaskRepository,- readonly chatService: ChatService,+ readonly contentAiUtil: ContentAiUtil,readonly mediaService: MediaService,readonly materialService: MaterialService,private readonly materialGroupService: MaterialGroupService,@@ -173,7 +173,7 @@ export class MaterialTaskService {},) {if (type === MediaType.IMG) {- const res = await this.chatService.imgContentByAi(+ const res = await this.contentAiUtil.imgContentByAi(user,model,fileUrl,@@ -184,7 +184,7 @@ export class MaterialTaskService {}if (type === MediaType.VIDEO) {- const res = await this.chatService.videoContentByAi(+ const res = await this.contentAiUtil.videoContentByAi(user,model,fileUrl,@@ -214,12 +214,12 @@ export class MaterialTaskService {content: '',}- const content = await this.chatService.getContentByAi(user, model, prompt, option)+ const content = await this.contentAiUtil.getContentByAi(user, model, prompt, option)if (!content)return resres.content = content- const title = await this.chatService.getTitleByAi(user, model, content, {})+ const title = await this.contentAiUtil.getTitleByAi(user, model, content)if (!title)return resres.title = titlediff --git a/project/backend/apps/aitoearn-server/src/content/util/ai.util.ts b/project/backend/apps/aitoearn-server/src/content/util/ai.util.tsnew file mode 100644index 00000000..2a3b631d--- /dev/null+++ b/project/backend/apps/aitoearn-server/src/content/util/ai.util.ts@@ -0,0 +1,176 @@+import { Injectable } from '@nestjs/common'+import { UserType } from '@yikart/common'+import OpenAI from 'openai'+import { ChatMessageDto, ChatService, UserChatCompletionDto } from '../../ai/chat'++@Injectable()+export class ContentAiUtil {+ constructor(+ private readonly chatService: ChatService,+ ) { }++ // Intelligent image content generation+ async imgContentByAi(user: { userId: string, userType: UserType }, model: string, imgUrl: string, prompt: string, option?: {+ systemPrompt?: string+ }): Promise<string> {+ const { userId, userType } = user++ const messages: ChatMessageDto[] = []+ if (option?.systemPrompt) {+ messages.push({+ role: 'system',+ content: option.systemPrompt,+ })+ }++ messages.push({+ role: 'user',+ content: [+ {+ type: 'image_url',+ image_url: {+ url: imgUrl,+ },+ },+ {+ type: 'text',+ text: prompt,+ },+ ],+ })++ const request: UserChatCompletionDto = {+ userId,+ userType,+ model,+ messages,+ }++ const res = await this.chatService.userChatCompletion(request)+ return res.content as string || ''+ }++ // Intelligent video content generation+ async videoContentByAi(user: { userId: string, userType: UserType }, model: string, videoUrl: string, prompt: string, option?: {+ systemPrompt?: string+ }): Promise<string> {+ const { userId, userType } = user++ const messages: ChatMessageDto[] = []+ if (option?.systemPrompt) {+ messages.push({+ role: 'system',+ content: option.systemPrompt,+ })+ }++ messages.push({+ role: 'user',+ content: [+ {+ type: 'video',+ video_url: {+ url: videoUrl,+ },+ },+ {+ type: 'text',+ text: prompt,+ },+ ],+ })++ const request: UserChatCompletionDto = {+ userId,+ userType,+ model,+ messages,+ }++ const res = await this.chatService.userChatCompletion(request)+ return res.content as string || ''+ }++ // Return text content based on image+ async getContentByAi(user: { userId: string, userType: UserType }, model: string, prompt: string, option: {+ coverUrl?: string+ systemPrompt?: string+ }): Promise<string> {+ const { userId, userType } = user+ if (!prompt && !option.coverUrl)+ return ''++ const messages: ChatMessageDto[] = []+ if (option.systemPrompt) {+ messages.push({+ role: 'system',+ content: option.systemPrompt,+ })+ }++ const content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = [+ {+ type: 'text',+ text: prompt,+ },+ ]+ if (option.coverUrl) {+ content.push({+ type: 'image_url',+ image_url: {+ url: option.coverUrl,+ },+ })+ }++ messages.push({+ role: 'user',+ content,+ })++ const request: UserChatCompletionDto = {+ userId,+ userType,+ model,+ messages,+ }++ const res = await this.chatService.userChatCompletion(request)+ return res.content as string || ''+ }++ // Return text content based on image+ async getTitleByAi(user: { userId: string, userType: UserType }, model: string, prompt: string): Promise<string> {+ const { userId, userType } = user+ 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 messages: ChatMessageDto[] = [{+ role: 'system',+ content: systemContent,+ }]++ const content: OpenAI.Chat.Completions.ChatCompletionContentPart[] = [+ {+ type: 'text',+ text: prompt,+ },+ ]++ messages.push({+ role: 'user',+ content,+ })++ const request: UserChatCompletionDto = {+ userId,+ userType,+ model,+ messages,+ }++ const res = await this.chatService.userChatCompletion(request)+ return res.content as string || ''+ }+}