perf: completion chatId

This commit is contained in:
archer
2023-07-23 20:07:35 +08:00
parent b7d18e38d1
commit 67e10d6f2c
35 changed files with 447 additions and 385 deletions

View File

@@ -1,9 +1,13 @@
import { Schema, model, models, Model } from 'mongoose';
import { ChatSchema as ChatType } from '@/types/mongoSchema';
import { ChatRoleMap } from '@/constants/chat';
import { ChatRoleMap, TaskResponseKeyEnum } from '@/constants/chat';
import { ChatSourceEnum, ChatSourceMap } from '@/constants/chat';
const ChatSchema = new Schema({
chatId: {
type: String,
require: true
},
userId: {
type: Schema.Types.ObjectId,
ref: 'user',
@@ -33,14 +37,14 @@ const ChatSchema = new Schema({
type: Object,
default: {}
},
// source: {
// type: String,
// enum: Object.keys(ChatSourceMap),
// required: true
// },
// shareId: {
// type: String
// },
source: {
type: String,
enum: Object.keys(ChatSourceMap),
required: true
},
shareId: {
type: String
},
content: {
type: [
{
@@ -53,14 +57,22 @@ const ChatSchema = new Schema({
type: String,
default: ''
},
rawSearch: {
[TaskResponseKeyEnum.responseData]: {
type: [
{
id: String,
q: String,
a: String,
kb_id: String,
source: String
moduleName: String,
price: String,
model: String,
tokens: Number,
question: String,
answer: String,
temperature: Number,
maxToken: Number,
finishMessages: Array,
similarity: Number,
limit: Number,
cqList: Array,
cqResult: String
}
],
default: []

View File

@@ -24,10 +24,6 @@ const ShareChatSchema = new Schema({
type: Number,
default: 0
},
maxContext: {
type: Number,
default: 20
},
lastTime: {
type: Date
}

View File

@@ -113,7 +113,7 @@ ${quoteQA.map((item, i) => `${i + 1}. [${item.q}\n${item.a}]`).join('\n')}
const adaptMessages = adaptChatItem_openAI({ messages: filterMessages, reserveId: false });
const chatAPI = getOpenAIApi();
// console.log(adaptMessages);
console.log(adaptMessages);
/* count response max token */
const promptsToken = modelToolMap.countTokens({
@@ -128,8 +128,8 @@ ${quoteQA.map((item, i) => `${i + 1}. [${item.q}\n${item.a}]`).join('\n')}
temperature: Number(temperature || 0),
max_tokens: maxToken,
messages: adaptMessages,
frequency_penalty: 0.5, // 越大,重复内容越少
presence_penalty: -0.5, // 越大,越容易出现新内容
// frequency_penalty: 0.5, // 越大,重复内容越少
// presence_penalty: -0.5, // 越大,越容易出现新内容
stream
},
{

View File

@@ -0,0 +1,65 @@
import { ChatItemType } from '@/types/chat';
import { Chat, App } from '@/service/mongo';
import { ChatSourceEnum } from '@/constants/chat';
type Props = {
chatId: string;
appId: string;
userId: string;
variables?: Record<string, any>;
isOwner: boolean;
source: `${ChatSourceEnum}`;
shareId?: string;
content: [ChatItemType, ChatItemType];
};
export async function saveChat({
chatId,
appId,
userId,
variables,
isOwner,
source,
shareId,
content
}: Props) {
const chatHistory = await Chat.findOne(
{
chatId,
userId
},
'_id'
);
if (chatHistory) {
await Chat.findOneAndUpdate(
{ chatId },
{
$push: {
content: {
$each: content
}
},
title: content[0].value.slice(0, 20),
updateTime: new Date()
}
);
} else {
await Chat.create({
chatId,
userId,
appId,
variables,
title: content[0].value.slice(0, 20),
source,
shareId,
content: content
});
}
if (isOwner && source === ChatSourceEnum.online) {
App.findByIdAndUpdate(appId, {
updateTime: new Date()
});
}
}