fix: combine message correctly (#7038)

This commit is contained in:
darkskygit
2024-05-23 06:16:31 +00:00
parent 3eaddd6e42
commit f8fee55b3d
3 changed files with 10 additions and 22 deletions

View File

@@ -25,6 +25,8 @@ function extractMustacheParams(template: string) {
return Array.from(new Set(params)); return Array.from(new Set(params));
} }
const EXCLUDE_MISSING_WARN_PARAMS = ['lora'];
export class ChatPrompt { export class ChatPrompt {
private readonly logger = new Logger(ChatPrompt.name); private readonly logger = new Logger(ChatPrompt.name);
public readonly encoder: Tokenizer | null; public readonly encoder: Tokenizer | null;
@@ -97,7 +99,7 @@ export class ChatPrompt {
typeof income !== 'string' || typeof income !== 'string' ||
(Array.isArray(options) && !options.includes(income)) (Array.isArray(options) && !options.includes(income))
) { ) {
if (sessionId) { if (sessionId && !EXCLUDE_MISSING_WARN_PARAMS.includes(key)) {
const prefix = income const prefix = income
? `Invalid param value: ${key}=${income}` ? `Invalid param value: ${key}=${income}`
: `Missing param value: ${key}`; : `Missing param value: ${key}`;

View File

@@ -89,7 +89,7 @@ export class FalProvider
).filter(v => typeof v === 'string' && v.length); ).filter(v => typeof v === 'string' && v.length);
return { return {
image_url: attachments?.[0], image_url: attachments?.[0],
prompt: content || undefined, prompt: content,
lora: lora.length ? lora : undefined, lora: lora.length ? lora : undefined,
}; };
} }

View File

@@ -7,7 +7,7 @@ import { FeatureManagementService } from '../../core/features';
import { QuotaService } from '../../core/quota'; import { QuotaService } from '../../core/quota';
import { PaymentRequiredException } from '../../fundamentals'; import { PaymentRequiredException } from '../../fundamentals';
import { ChatMessageCache } from './message'; import { ChatMessageCache } from './message';
import { ChatPrompt, PromptService } from './prompt'; import { PromptService } from './prompt';
import { import {
AvailableModel, AvailableModel,
ChatHistory, ChatHistory,
@@ -129,7 +129,7 @@ export class ChatSession implements AsyncDisposable {
// we should combine it with the user message in the prompt // we should combine it with the user message in the prompt
if ( if (
messages.length === 1 && messages.length === 1 &&
firstMessage?.content && firstMessage &&
this.state.prompt.paramKeys.includes('content') this.state.prompt.paramKeys.includes('content')
) { ) {
const normalizedParams = { const normalizedParams = {
@@ -258,27 +258,13 @@ export class ChatSessionService {
createdAt: 'asc', createdAt: 'asc',
}, },
}, },
prompt: { promptName: true,
select: {
name: true,
action: true,
model: true,
messages: {
select: {
role: true,
content: true,
createdAt: true,
},
orderBy: {
idx: 'asc',
},
},
},
},
}, },
}) })
.then(async session => { .then(async session => {
if (!session) return; if (!session) return;
const prompt = await this.prompt.get(session.promptName);
if (!prompt) throw new Error(`Prompt not found: ${session.promptName}`);
const messages = ChatMessageSchema.array().safeParse(session.messages); const messages = ChatMessageSchema.array().safeParse(session.messages);
@@ -287,7 +273,7 @@ export class ChatSessionService {
userId: session.userId, userId: session.userId,
workspaceId: session.workspaceId, workspaceId: session.workspaceId,
docId: session.docId, docId: session.docId,
prompt: ChatPrompt.createFromPrompt(session.prompt), prompt,
messages: messages.success ? messages.data : [], messages: messages.success ? messages.data : [],
}; };
}); });