2024-10-13 18:53:43 +00:00
|
|
|
// @ts-nocheck
|
|
|
|
// Preventing TS checks with files presented in the video for a better presentation.
|
2024-07-17 18:54:46 +00:00
|
|
|
import { streamText as _streamText, convertToCoreMessages } from 'ai';
|
2024-10-13 18:53:43 +00:00
|
|
|
import { getModel } from '~/lib/.server/llm/model';
|
2024-07-17 18:54:46 +00:00
|
|
|
import { MAX_TOKENS } from './constants';
|
|
|
|
import { getSystemPrompt } from './prompts';
|
2024-10-13 18:53:43 +00:00
|
|
|
import { MODEL_LIST, DEFAULT_MODEL, DEFAULT_PROVIDER } from '~/utils/constants';
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
interface ToolResult<Name extends string, Args, Result> {
|
|
|
|
toolCallId: string;
|
|
|
|
toolName: Name;
|
|
|
|
args: Args;
|
|
|
|
result: Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Message {
|
|
|
|
role: 'user' | 'assistant';
|
|
|
|
content: string;
|
|
|
|
toolInvocations?: ToolResult<string, unknown, unknown>[];
|
2024-10-13 18:53:43 +00:00
|
|
|
model?: string;
|
2024-07-17 18:54:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type Messages = Message[];
|
|
|
|
|
|
|
|
export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
|
|
|
|
|
2024-11-06 10:10:08 +00:00
|
|
|
function extractPropertiesFromMessage(message: Message): { model: string; provider: string; content: string } {
|
2024-10-13 18:53:43 +00:00
|
|
|
const modelRegex = /^\[Model: (.*?)\]\n\n/;
|
2024-11-06 10:10:08 +00:00
|
|
|
const providerRegex = /\[Provider: (.*?)\]\n\n/;
|
2024-10-13 18:53:43 +00:00
|
|
|
|
2024-11-06 10:10:08 +00:00
|
|
|
// Extract model
|
|
|
|
const modelMatch = message.content.match(modelRegex);
|
|
|
|
const model = modelMatch ? modelMatch[1] : DEFAULT_MODEL;
|
2024-10-13 18:53:43 +00:00
|
|
|
|
2024-11-06 10:10:08 +00:00
|
|
|
// Extract provider
|
|
|
|
const providerMatch = message.content.match(providerRegex);
|
|
|
|
const provider = providerMatch ? providerMatch[1] : DEFAULT_PROVIDER;
|
|
|
|
|
|
|
|
// Remove model and provider lines from content
|
|
|
|
const cleanedContent = message.content
|
|
|
|
.replace(modelRegex, '')
|
|
|
|
.replace(providerRegex, '')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
return { model, provider, content: cleanedContent };
|
2024-10-13 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 18:54:46 +00:00
|
|
|
export function streamText(messages: Messages, env: Env, options?: StreamingOptions) {
|
2024-10-13 18:53:43 +00:00
|
|
|
let currentModel = DEFAULT_MODEL;
|
2024-11-06 10:10:08 +00:00
|
|
|
let currentProvider = DEFAULT_PROVIDER;
|
|
|
|
|
2024-10-13 18:53:43 +00:00
|
|
|
const processedMessages = messages.map((message) => {
|
|
|
|
if (message.role === 'user') {
|
2024-11-06 10:10:08 +00:00
|
|
|
const { model, provider, content } = extractPropertiesFromMessage(message);
|
|
|
|
|
|
|
|
if (MODEL_LIST.find((m) => m.name === model)) {
|
|
|
|
currentModel = model;
|
2024-10-13 18:53:43 +00:00
|
|
|
}
|
2024-11-06 10:10:08 +00:00
|
|
|
|
|
|
|
currentProvider = provider;
|
|
|
|
|
2024-10-13 18:53:43 +00:00
|
|
|
return { ...message, content };
|
|
|
|
}
|
|
|
|
|
2024-11-06 10:10:08 +00:00
|
|
|
return message; // No changes for non-user messages
|
|
|
|
});
|
2024-10-13 18:53:43 +00:00
|
|
|
|
2024-07-17 18:54:46 +00:00
|
|
|
return _streamText({
|
2024-11-06 10:10:08 +00:00
|
|
|
model: getModel(currentProvider, currentModel, env),
|
2024-07-17 18:54:46 +00:00
|
|
|
system: getSystemPrompt(),
|
|
|
|
maxTokens: MAX_TOKENS,
|
2024-10-13 18:53:43 +00:00
|
|
|
messages: convertToCoreMessages(processedMessages),
|
2024-07-17 18:54:46 +00:00
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|