feat: dynamic model discovery from vercel ai gateway (#1353)
This commit is contained in:
parent
2b0b42d144
commit
b1da86062e
74 changed files with 7426 additions and 2277 deletions
|
|
@ -1,5 +1,28 @@
|
|||
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",
|
||||
|
|
@ -9,20 +32,116 @@ const createMockModel = (): LanguageModel => {
|
|||
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: "Hello, world!" }],
|
||||
content: [{ type: "text", text: "This is a reasoned response." }],
|
||||
reasoning: [
|
||||
{ type: "text", text: "Let me think through this step by step..." },
|
||||
],
|
||||
warnings: [],
|
||||
}),
|
||||
doStream: async () => ({
|
||||
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",
|
||||
id: "mock-id",
|
||||
delta: "Mock response",
|
||||
textDelta: "Test Conversation",
|
||||
});
|
||||
controller.enqueue({
|
||||
type: "finish",
|
||||
finishReason: "stop",
|
||||
usage: { inputTokens: 5, outputTokens: 5 },
|
||||
});
|
||||
controller.close();
|
||||
},
|
||||
|
|
@ -33,6 +152,6 @@ const createMockModel = (): LanguageModel => {
|
|||
};
|
||||
|
||||
export const chatModel = createMockModel();
|
||||
export const reasoningModel = createMockModel();
|
||||
export const titleModel = createMockModel();
|
||||
export const reasoningModel = createMockReasoningModel();
|
||||
export const titleModel = createMockTitleModel();
|
||||
export const artifactModel = createMockModel();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue