|
|
@@ -1,17 +1,16 @@
|
|
|
-import { BadRequestException, Injectable, InternalServerErrorException, Logger } from '@nestjs/common'
|
|
|
+import {BadRequestException, Injectable, InternalServerErrorException, Logger} from '@nestjs/common'
|
|
|
import * as PdfParse from '@cyber2024/pdf-parse-fixed'
|
|
|
-import { createHash } from 'crypto'
|
|
|
-import { Tiktoken, get_encoding } from '@dqbd/tiktoken'
|
|
|
-import { Configuration, OpenAIApi } from 'azure-openai'
|
|
|
+import {createHash} from 'crypto'
|
|
|
+import {get_encoding, Tiktoken} from '@dqbd/tiktoken'
|
|
|
+import {Configuration, OpenAIApi} from 'azure-openai'
|
|
|
import * as queue from 'fastq'
|
|
|
-import { setTimeout } from 'timers/promises'
|
|
|
+import {setTimeout} from 'timers/promises'
|
|
|
import * as dedent from 'dedent'
|
|
|
-import { Sequelize, Model, DataTypes } from 'sequelize'
|
|
|
-import { ConfigService } from '@nestjs/config'
|
|
|
-import { ChatEmbedding } from './entities/chat-embedding.entity'
|
|
|
-import { VECTOR } from './pgvector'
|
|
|
-import { SysConfigService } from '../sys-config/sys-config.service'
|
|
|
-import { ApiUserService } from '../api-users/api-user.service'
|
|
|
+import {DataTypes, Sequelize} from 'sequelize'
|
|
|
+import {ConfigService} from '@nestjs/config'
|
|
|
+import {ChatEmbedding} from './entities/chat-embedding.entity'
|
|
|
+import {VECTOR} from './pgvector'
|
|
|
+import {ApiUserService} from '../api-users/api-user.service'
|
|
|
|
|
|
function formatEmbedding(embedding: number[]) {
|
|
|
return `[${embedding.join(', ')}]`
|
|
|
@@ -23,7 +22,7 @@ export class ChatPdfService {
|
|
|
private readonly openai: OpenAIApi
|
|
|
private readonly sequelize: Sequelize
|
|
|
constructor(
|
|
|
- private readonly sysConfigService: SysConfigService,
|
|
|
+ // private readonly sysConfigService: SysConfigService,
|
|
|
private readonly configService: ConfigService,
|
|
|
private readonly apiUserService: ApiUserService
|
|
|
) {
|
|
|
@@ -218,8 +217,30 @@ export class ChatPdfService {
|
|
|
return context
|
|
|
}
|
|
|
|
|
|
+ async getSystemConfig(q: string, name: string, code: string) {
|
|
|
+ const apiUser = await this.apiUserService.findByCode(code)
|
|
|
+ const keywords = await this.getKeywords(q)
|
|
|
+ const { embedding: keywordEmbedding } = await this.getEmbedding(keywords)
|
|
|
+ const context = this.cutContext((await this.searchEmbedding(name, keywordEmbedding)).map((item) => item.text))
|
|
|
+ if (!context || !context.length) {
|
|
|
+ return {
|
|
|
+ answer: '未找到相关内容'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dedent`${apiUser.desc}'
|
|
|
+ 根据相关性从高到低排序。 '
|
|
|
+ 这是用户提出的问题:
|
|
|
+ ${q}
|
|
|
+ 你只能根据用户的问题,以下面的内容为准进行回答:
|
|
|
+ \`\`\`
|
|
|
+ ${context.join('\n')}
|
|
|
+ \`\`\`
|
|
|
+ 你要确保你的回答全部基于上面的问题和内容,如果无法找到答案,你需要根据给你设定的角色表述你不能回答这个问题。
|
|
|
+ 你只能用中文回答.`
|
|
|
+ }
|
|
|
async ask(q: string, name: string) {
|
|
|
- const defSysMsg = (await this.sysConfigService.findByName('customer_system_message'))?.value
|
|
|
+ // const defSysMsg = (await this.sysConfigService.findByName('customer_system_message'))?.value
|
|
|
+ const apiUser = await this.apiUserService.findByCode(name)
|
|
|
const keywords = await this.getKeywords(q)
|
|
|
const { embedding: keywordEmbedding } = await this.getEmbedding(keywords)
|
|
|
const context = this.cutContext((await this.searchEmbedding(name, keywordEmbedding)).map((item) => item.text))
|
|
|
@@ -228,7 +249,7 @@ export class ChatPdfService {
|
|
|
answer: '未找到相关内容'
|
|
|
}
|
|
|
}
|
|
|
- const content = dedent`${defSysMsg}'
|
|
|
+ const content = dedent`${apiUser.desc}'
|
|
|
根据相关性从高到低排序。 '
|
|
|
这是用户提出的问题:
|
|
|
${q}
|
|
|
@@ -257,11 +278,11 @@ export class ChatPdfService {
|
|
|
}
|
|
|
|
|
|
async customerAsk(q: string, name: string) {
|
|
|
- let apiUser = this.apiUserService.findByCode(name)
|
|
|
+ let apiUser = await this.apiUserService.findByCode(name)
|
|
|
if(!apiUser) {
|
|
|
throw new BadRequestException("not a enabled api user")
|
|
|
}
|
|
|
- const defSysMsg = (await this.sysConfigService.findByName('customer_system_message'))?.value
|
|
|
+ // const defSysMsg = (await this.sysConfigService.findByName('customer_system_message'))?.value
|
|
|
const keywords = await this.getKeywords(q)
|
|
|
const { embedding: keywordEmbedding } = await this.getEmbedding(keywords)
|
|
|
const context = this.cutContext((await this.searchEmbedding(name, keywordEmbedding)).map((item) => item.text))
|
|
|
@@ -270,7 +291,7 @@ export class ChatPdfService {
|
|
|
answer: '客服无法回答这个问题,换个试试吧'
|
|
|
}
|
|
|
}
|
|
|
- const content = dedent`${defSysMsg}'
|
|
|
+ const content = dedent`${apiUser.desc}'
|
|
|
根据相关性从高到低排序。 '
|
|
|
这是用户提出的问题:
|
|
|
${q}
|