chatbot-template/lib/ai/models.mock.ts

157 lines
4.7 KiB
TypeScript

import type { LanguageModel } from "ai";
const mockResponses: Record<string, string> = {
default: "This is a mock response for testing.",
weather: "The weather in San Francisco is sunny and 72°F.",
greeting: "Hello! How can I help you today?",
};
function getResponseForPrompt(prompt: unknown): string {
const promptStr = JSON.stringify(prompt).toLowerCase();
if (promptStr.includes("weather") || promptStr.includes("temperature")) {
return mockResponses.weather;
}
if (
promptStr.includes("hello") ||
promptStr.includes("hi") ||
promptStr.includes("hey")
) {
return mockResponses.greeting;
}
return mockResponses.default;
}
const createMockModel = (): LanguageModel => {
return {
specificationVersion: "v2",
provider: "mock",
modelId: "mock-model",
defaultObjectGenerationMode: "tool",
supportedUrls: [],
supportsImageUrls: false,
supportsStructuredOutputs: false,
doGenerate: async ({ prompt }: { prompt: unknown }) => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 20, totalTokens: 30 },
content: [{ type: "text", text: getResponseForPrompt(prompt) }],
warnings: [],
}),
doStream: ({ prompt }: { prompt: unknown }) => {
const response = getResponseForPrompt(prompt);
const words = response.split(" ");
return {
stream: new ReadableStream({
async start(controller) {
for (const word of words) {
controller.enqueue({
type: "text-delta",
textDelta: `${word} `,
});
await new Promise((resolve) => {
setTimeout(resolve, 10);
});
}
controller.enqueue({
type: "finish",
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 20 },
});
controller.close();
},
}),
rawCall: { rawPrompt: null, rawSettings: {} },
};
},
} as unknown as LanguageModel;
};
const createMockReasoningModel = (): LanguageModel => {
return {
specificationVersion: "v2",
provider: "mock",
modelId: "mock-reasoning-model",
defaultObjectGenerationMode: "tool",
supportedUrls: [],
supportsImageUrls: false,
supportsStructuredOutputs: false,
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 20, totalTokens: 30 },
content: [{ type: "text", text: "This is a reasoned response." }],
reasoning: [
{ type: "text", text: "Let me think through this step by step..." },
],
warnings: [],
}),
doStream: () => ({
stream: new ReadableStream({
async start(controller) {
controller.enqueue({
type: "reasoning",
textDelta: "Let me think through this step by step... ",
});
await new Promise((resolve) => {
setTimeout(resolve, 10);
});
controller.enqueue({
type: "text-delta",
textDelta: "This is a reasoned response.",
});
controller.enqueue({
type: "finish",
finishReason: "stop",
usage: { inputTokens: 10, outputTokens: 20 },
});
controller.close();
},
}),
rawCall: { rawPrompt: null, rawSettings: {} },
}),
} as unknown as LanguageModel;
};
const createMockTitleModel = (): LanguageModel => {
return {
specificationVersion: "v2",
provider: "mock",
modelId: "mock-title-model",
defaultObjectGenerationMode: "tool",
supportedUrls: [],
supportsImageUrls: false,
supportsStructuredOutputs: false,
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { inputTokens: 5, outputTokens: 5, totalTokens: 10 },
content: [{ type: "text", text: "Test Conversation" }],
warnings: [],
}),
doStream: () => ({
stream: new ReadableStream({
start(controller) {
controller.enqueue({
type: "text-delta",
textDelta: "Test Conversation",
});
controller.enqueue({
type: "finish",
finishReason: "stop",
usage: { inputTokens: 5, outputTokens: 5 },
});
controller.close();
},
}),
rawCall: { rawPrompt: null, rawSettings: {} },
}),
} as unknown as LanguageModel;
};
export const chatModel = createMockModel();
export const reasoningModel = createMockReasoningModel();
export const titleModel = createMockTitleModel();
export const artifactModel = createMockModel();