2024-07-10 16:44:39 +00:00
|
|
|
import { type ActionFunctionArgs } from '@remix-run/cloudflare';
|
2024-12-16 14:17:18 +00:00
|
|
|
import { createDataStream } from 'ai';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { MAX_RESPONSE_SEGMENTS, MAX_TOKENS } from '~/lib/.server/llm/constants';
|
2024-12-15 11:17:16 +00:00
|
|
|
import { CONTINUE_PROMPT } from '~/lib/common/prompts/prompts';
|
2024-07-24 15:43:32 +00:00
|
|
|
import { streamText, type Messages, type StreamingOptions } from '~/lib/.server/llm/stream-text';
|
|
|
|
import SwitchableStream from '~/lib/.server/llm/switchable-stream';
|
2024-12-11 08:32:21 +00:00
|
|
|
import type { IProviderSetting } from '~/types/model';
|
2024-12-31 17:17:32 +00:00
|
|
|
import { createScopedLogger } from '~/utils/logger';
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-07-29 18:31:45 +00:00
|
|
|
export async function action(args: ActionFunctionArgs) {
|
2024-09-26 16:45:41 +00:00
|
|
|
return chatAction(args);
|
2024-07-29 18:31:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-31 17:17:32 +00:00
|
|
|
const logger = createScopedLogger('api.chat');
|
|
|
|
|
2024-12-09 15:26:33 +00:00
|
|
|
function parseCookies(cookieHeader: string): Record<string, string> {
|
|
|
|
const cookies: Record<string, string> = {};
|
2024-11-13 20:20:51 +00:00
|
|
|
|
2024-11-21 21:05:35 +00:00
|
|
|
const items = cookieHeader.split(';').map((cookie) => cookie.trim());
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
const [name, ...rest] = item.split('=');
|
2024-11-13 20:20:51 +00:00
|
|
|
|
|
|
|
if (name && rest) {
|
|
|
|
const decodedName = decodeURIComponent(name.trim());
|
2024-11-21 21:05:35 +00:00
|
|
|
const decodedValue = decodeURIComponent(rest.join('=').trim());
|
2024-11-13 20:20:51 +00:00
|
|
|
cookies[decodedName] = decodedValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return cookies;
|
|
|
|
}
|
|
|
|
|
2024-09-25 18:54:09 +00:00
|
|
|
async function chatAction({ context, request }: ActionFunctionArgs) {
|
2024-12-29 10:06:31 +00:00
|
|
|
const { messages, files, promptId, contextOptimization } = await request.json<{
|
2024-11-21 21:05:35 +00:00
|
|
|
messages: Messages;
|
2024-12-07 10:28:13 +00:00
|
|
|
files: any;
|
2024-12-15 11:17:16 +00:00
|
|
|
promptId?: string;
|
2024-12-29 10:06:31 +00:00
|
|
|
contextOptimization: boolean;
|
2024-10-29 03:19:30 +00:00
|
|
|
}>();
|
2024-07-25 15:28:23 +00:00
|
|
|
|
2024-11-21 21:05:35 +00:00
|
|
|
const cookieHeader = request.headers.get('Cookie');
|
2024-12-06 11:28:04 +00:00
|
|
|
const apiKeys = JSON.parse(parseCookies(cookieHeader || '').apiKeys || '{}');
|
2024-12-11 08:32:21 +00:00
|
|
|
const providerSettings: Record<string, IProviderSetting> = JSON.parse(
|
|
|
|
parseCookies(cookieHeader || '').providers || '{}',
|
|
|
|
);
|
2024-11-13 20:20:51 +00:00
|
|
|
|
2024-07-19 09:12:55 +00:00
|
|
|
const stream = new SwitchableStream();
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-12-16 09:01:41 +00:00
|
|
|
const cumulativeUsage = {
|
|
|
|
completionTokens: 0,
|
|
|
|
promptTokens: 0,
|
|
|
|
totalTokens: 0,
|
|
|
|
};
|
|
|
|
|
2024-07-10 16:44:39 +00:00
|
|
|
try {
|
2024-07-19 09:12:55 +00:00
|
|
|
const options: StreamingOptions = {
|
|
|
|
toolChoice: 'none',
|
2024-12-09 15:26:33 +00:00
|
|
|
onFinish: async ({ text: content, finishReason, usage }) => {
|
2024-12-31 17:17:32 +00:00
|
|
|
logger.debug('usage', JSON.stringify(usage));
|
2024-12-09 15:26:33 +00:00
|
|
|
|
2024-12-16 14:17:18 +00:00
|
|
|
if (usage) {
|
2024-12-16 09:01:41 +00:00
|
|
|
cumulativeUsage.completionTokens += usage.completionTokens || 0;
|
|
|
|
cumulativeUsage.promptTokens += usage.promptTokens || 0;
|
|
|
|
cumulativeUsage.totalTokens += usage.totalTokens || 0;
|
|
|
|
}
|
|
|
|
|
2024-07-19 09:12:55 +00:00
|
|
|
if (finishReason !== 'length') {
|
2024-12-31 17:17:32 +00:00
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const usageStream = createDataStream({
|
|
|
|
async execute(dataStream) {
|
|
|
|
dataStream.writeMessageAnnotation({
|
|
|
|
type: 'usage',
|
|
|
|
value: {
|
|
|
|
completionTokens: cumulativeUsage.completionTokens,
|
|
|
|
promptTokens: cumulativeUsage.promptTokens,
|
|
|
|
totalTokens: cumulativeUsage.totalTokens,
|
2024-12-16 14:17:18 +00:00
|
|
|
},
|
2024-12-31 17:17:32 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
onError: (error: any) => `Custom error: ${error.message}`,
|
|
|
|
}).pipeThrough(
|
|
|
|
new TransformStream({
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
// Convert the string stream to a byte stream
|
|
|
|
const str = typeof chunk === 'string' ? chunk : JSON.stringify(chunk);
|
|
|
|
controller.enqueue(encoder.encode(str));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await stream.switchSource(usageStream);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
stream.close();
|
|
|
|
|
|
|
|
return;
|
2024-07-19 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.switches >= MAX_RESPONSE_SEGMENTS) {
|
2024-07-22 15:40:28 +00:00
|
|
|
throw Error('Cannot continue message: Maximum segments reached');
|
2024-07-19 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 15:40:28 +00:00
|
|
|
const switchesLeft = MAX_RESPONSE_SEGMENTS - stream.switches;
|
|
|
|
|
2024-12-31 17:17:32 +00:00
|
|
|
logger.info(`Reached max token limit (${MAX_TOKENS}): Continuing message (${switchesLeft} switches left)`);
|
2024-07-22 15:40:28 +00:00
|
|
|
|
2024-07-19 09:12:55 +00:00
|
|
|
messages.push({ role: 'assistant', content });
|
|
|
|
messages.push({ role: 'user', content: CONTINUE_PROMPT });
|
|
|
|
|
2024-12-11 21:14:36 +00:00
|
|
|
const result = await streamText({
|
|
|
|
messages,
|
|
|
|
env: context.cloudflare.env,
|
|
|
|
options,
|
|
|
|
apiKeys,
|
|
|
|
files,
|
|
|
|
providerSettings,
|
2024-12-15 11:17:16 +00:00
|
|
|
promptId,
|
2024-12-29 10:06:31 +00:00
|
|
|
contextOptimization,
|
2024-12-11 21:14:36 +00:00
|
|
|
});
|
2024-07-19 09:12:55 +00:00
|
|
|
|
2024-12-31 17:17:32 +00:00
|
|
|
stream.switchSource(result.toDataStream());
|
|
|
|
|
|
|
|
return;
|
2024-07-19 09:12:55 +00:00
|
|
|
},
|
|
|
|
};
|
2025-01-12 22:51:29 +00:00
|
|
|
const totalMessageContent = messages.reduce((acc, message) => acc + message.content, '');
|
|
|
|
logger.debug(`Total message length: ${totalMessageContent.split(' ').length}, words`);
|
2024-07-19 09:12:55 +00:00
|
|
|
|
2024-12-11 21:14:36 +00:00
|
|
|
const result = await streamText({
|
|
|
|
messages,
|
|
|
|
env: context.cloudflare.env,
|
|
|
|
options,
|
|
|
|
apiKeys,
|
|
|
|
files,
|
|
|
|
providerSettings,
|
2024-12-15 11:17:16 +00:00
|
|
|
promptId,
|
2024-12-29 10:06:31 +00:00
|
|
|
contextOptimization,
|
2024-12-11 21:14:36 +00:00
|
|
|
});
|
2024-07-19 09:12:55 +00:00
|
|
|
|
2025-01-12 22:51:29 +00:00
|
|
|
(async () => {
|
|
|
|
for await (const part of result.fullStream) {
|
|
|
|
if (part.type === 'error') {
|
|
|
|
const error: any = part.error;
|
|
|
|
logger.error(`${error}`);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2024-12-09 15:26:33 +00:00
|
|
|
stream.switchSource(result.toDataStream());
|
2024-07-19 09:12:55 +00:00
|
|
|
|
2025-01-12 22:51:29 +00:00
|
|
|
// return createrespo
|
2024-08-08 15:48:36 +00:00
|
|
|
return new Response(stream.readable, {
|
|
|
|
status: 200,
|
|
|
|
headers: {
|
2025-01-12 22:51:29 +00:00
|
|
|
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
'Text-Encoding': 'chunked',
|
2024-08-08 15:48:36 +00:00
|
|
|
},
|
|
|
|
});
|
2024-12-06 11:28:04 +00:00
|
|
|
} catch (error: any) {
|
2024-12-31 17:17:32 +00:00
|
|
|
logger.error(error);
|
2024-11-13 20:20:51 +00:00
|
|
|
|
2024-10-29 03:19:30 +00:00
|
|
|
if (error.message?.includes('API key')) {
|
|
|
|
throw new Response('Invalid or missing API key', {
|
|
|
|
status: 401,
|
2024-11-21 21:05:35 +00:00
|
|
|
statusText: 'Unauthorized',
|
2024-10-29 03:19:30 +00:00
|
|
|
});
|
|
|
|
}
|
2024-07-10 16:44:39 +00:00
|
|
|
|
|
|
|
throw new Response(null, {
|
|
|
|
status: 500,
|
|
|
|
statusText: 'Internal Server Error',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|