feat: save system prompt

This commit is contained in:
archer
2023-05-02 14:06:10 +08:00
parent b0d414ac12
commit 90456301d2
10 changed files with 104 additions and 27 deletions

View File

@@ -75,7 +75,7 @@ export const authModel = async ({
};
}
return { model };
return { model, showModelDetail: model.share.isShareDetail || userId === String(model.userId) };
};
// 获取对话校验
@@ -91,7 +91,12 @@ export const authChat = async ({
const userId = await authToken(authorization);
// 获取 model 数据
const { model } = await authModel({ modelId, userId, authOwner: false, reserveDetail: true });
const { model, showModelDetail } = await authModel({
modelId,
userId,
authOwner: false,
reserveDetail: true
});
// 聊天内容
let content: ChatItemSimpleType[] = [];
@@ -124,7 +129,8 @@ export const authChat = async ({
systemKey,
content,
userId,
model
model,
showModelDetail
};
};

View File

@@ -7,6 +7,7 @@ import { User } from '../models/user';
import { formatPrice } from '@/utils/user';
import { embeddingModel } from '@/constants/model';
import { pushGenerateVectorBill } from '../events/pushBill';
import { SYSTEM_PROMPT_PREFIX } from '@/constants/chat';
/* 获取用户 api 的 openai 信息 */
export const getUserApiOpenai = async (userId: string) => {
@@ -110,11 +111,13 @@ export const openaiCreateEmbedding = async ({
export const gpt35StreamResponse = ({
res,
stream,
chatResponse
chatResponse,
systemPrompt = ''
}: {
res: NextApiResponse;
stream: PassThrough;
chatResponse: any;
systemPrompt?: string;
}) =>
new Promise<{ responseContent: string }>(async (resolve, reject) => {
try {
@@ -144,8 +147,8 @@ export const gpt35StreamResponse = ({
}
};
const decoder = new TextDecoder();
try {
const decoder = new TextDecoder();
const parser = createParser(onParse);
for await (const chunk of chatResponse.data as any) {
if (stream.destroyed) {
@@ -157,6 +160,12 @@ export const gpt35StreamResponse = ({
} catch (error) {
console.log('pipe error', error);
}
// push system prompt
!stream.destroyed &&
systemPrompt &&
stream.push(`${SYSTEM_PROMPT_PREFIX}${systemPrompt.replace(/\n/g, '<br/>')}`);
// close stream
!stream.destroyed && stream.push(null);
stream.destroy();