File size: 8,222 Bytes
867b17d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
import { generateId, type ModelMessage } from 'ai';
import { TEST_PROMPTS } from './basic';
import type { LanguageModelV2StreamPart } from '@ai-sdk/provider';
export function compareMessages(
firstMessage: ModelMessage,
secondMessage: ModelMessage,
): boolean {
if (firstMessage.role !== secondMessage.role) return false;
if (
!Array.isArray(firstMessage.content) ||
!Array.isArray(secondMessage.content)
) {
return false;
}
if (firstMessage.content.length !== secondMessage.content.length) {
return false;
}
for (let i = 0; i < firstMessage.content.length; i++) {
const item1 = firstMessage.content[i];
const item2 = secondMessage.content[i];
if (item1.type !== item2.type) return false;
if (item1.type === 'file' && item2.type === 'file') {
// if (item1.image.toString() !== item2.image.toString()) return false;
// if (item1.mimeType !== item2.mimeType) return false;
} else if (item1.type === 'text' && item2.type === 'text') {
if (item1.text !== item2.text) return false;
} else if (item1.type === 'tool-result' && item2.type === 'tool-result') {
if (item1.toolCallId !== item2.toolCallId) return false;
} else {
return false;
}
}
return true;
}
const textToDeltas = (text: string): LanguageModelV2StreamPart[] => {
const id = generateId();
const deltas = text.split(' ').map((char) => ({
id,
type: 'text-delta' as const,
delta: `${char} `,
}));
return [{ id, type: 'text-start' }, ...deltas, { id, type: 'text-end' }];
};
const reasoningToDeltas = (text: string): LanguageModelV2StreamPart[] => {
const id = generateId();
const deltas = text.split(' ').map((char) => ({
id,
type: 'reasoning-delta' as const,
delta: `${char} `,
}));
return [
{ id, type: 'reasoning-start' },
...deltas,
{ id, type: 'reasoning-end' },
];
};
export const getResponseChunksByPrompt = (
prompt: ModelMessage[],
isReasoningEnabled = false,
): LanguageModelV2StreamPart[] => {
const recentMessage = prompt.at(-1);
if (!recentMessage) {
throw new Error('No recent message found!');
}
if (isReasoningEnabled) {
if (compareMessages(recentMessage, TEST_PROMPTS.USER_SKY)) {
return [
...reasoningToDeltas('The sky is blue because of rayleigh scattering!'),
...textToDeltas("It's just blue duh!"),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_GRASS)) {
return [
...reasoningToDeltas(
'Grass is green because of chlorophyll absorption!',
),
...textToDeltas("It's just green duh!"),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
}
}
if (compareMessages(recentMessage, TEST_PROMPTS.USER_THANKS)) {
return [
...textToDeltas("You're welcome!"),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_GRASS)) {
return [
...textToDeltas("It's just green duh!"),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_SKY)) {
return [
...textToDeltas("It's just blue duh!"),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_NEXTJS)) {
return [
...textToDeltas('With Next.js, you can ship fast!'),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (
compareMessages(recentMessage, TEST_PROMPTS.USER_IMAGE_ATTACHMENT)
) {
return [
...textToDeltas('This painting is by Monet!'),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.USER_TEXT_ARTIFACT)) {
const toolCallId = generateId();
return [
{
id: toolCallId,
type: 'tool-input-start',
toolName: 'createDocument',
},
{
id: toolCallId,
type: 'tool-input-delta',
delta: JSON.stringify({
title: 'Essay about Silicon Valley',
kind: 'text',
}),
},
{
id: toolCallId,
type: 'tool-input-end',
},
{
toolCallId: toolCallId,
type: 'tool-result',
toolName: 'createDocument',
result: {
id: 'doc_123',
title: 'Essay about Silicon Valley',
kind: 'text',
},
},
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (
compareMessages(recentMessage, TEST_PROMPTS.CREATE_DOCUMENT_TEXT_CALL)
) {
return [
...textToDeltas(`\n
# Silicon Valley: The Epicenter of Innovation
## Origins and Evolution
Silicon Valley, nestled in the southern part of the San Francisco Bay Area, emerged as a global technology hub in the late 20th century. Its transformation began in the 1950s when Stanford University encouraged its graduates to start their own companies nearby, leading to the formation of pioneering semiconductor firms that gave the region its name.
## The Innovation Ecosystem
What makes Silicon Valley unique is its perfect storm of critical elements: prestigious universities like Stanford and Berkeley, abundant venture capital, a culture that celebrates risk-taking, and a dense network of talented individuals. This ecosystem has consistently nurtured groundbreaking technologies from personal computers to social media platforms to artificial intelligence.
## Challenges and Criticisms
Despite its remarkable success, Silicon Valley faces significant challenges including extreme income inequality, housing affordability crises, and questions about technology's impact on society. Critics argue the region has developed a monoculture that sometimes struggles with diversity and inclusion.
## Future Prospects
As we move forward, Silicon Valley continues to reinvent itself. While some predict its decline due to remote work trends and competition from other tech hubs, the region's adaptability and innovative spirit suggest it will remain influential in shaping our technological future for decades to come.
`),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (
compareMessages(recentMessage, TEST_PROMPTS.CREATE_DOCUMENT_TEXT_RESULT)
) {
return [
...textToDeltas('A document was created and is now visible to the user.'),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.GET_WEATHER_CALL)) {
return [
{
type: 'tool-call',
toolCallId: 'call_456',
toolName: 'getWeather',
input: JSON.stringify({ latitude: 37.7749, longitude: -122.4194 }),
},
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
} else if (compareMessages(recentMessage, TEST_PROMPTS.GET_WEATHER_RESULT)) {
return [
...textToDeltas('The current temperature in San Francisco is 17°C.'),
{
type: 'finish',
finishReason: 'stop',
usage: { inputTokens: 3, outputTokens: 10, totalTokens: 13 },
},
];
}
return [{ id: '6', type: 'text-delta', delta: 'Unknown test prompt!' }];
};
|