|
1 | 1 | import { createGroq } from '@ai-sdk/groq';
|
| 2 | +//import Groq from "groq-sdk"; |
2 | 3 | import type {
|
3 | 4 | ImageDescriptionParams,
|
4 | 5 | ModelTypeName,
|
@@ -234,87 +235,103 @@ export const groqPlugin: Plugin = {
|
234 | 235 | params: TextEmbeddingParams | string | null
|
235 | 236 | ): Promise<number[]> => {
|
236 | 237 | // Handle null input (initialization case)
|
237 |
| - if (params === null) { |
238 |
| - logger.debug('Creating test embedding for initialization'); |
239 |
| - // Return a consistent vector for null input |
240 |
| - const testVector = Array(1536).fill(0); |
241 |
| - testVector[0] = 0.1; // Make it non-zero |
242 |
| - return testVector; |
243 |
| - } |
244 |
| - |
245 |
| - // Get the text from whatever format was provided |
246 |
| - let text: string; |
247 |
| - if (typeof params === 'string') { |
248 |
| - text = params; // Direct string input |
249 |
| - } else if (typeof params === 'object' && params.text) { |
250 |
| - text = params.text; // Object with text property |
251 |
| - } else { |
252 |
| - logger.warn('Invalid input format for embedding'); |
253 |
| - // Return a fallback for invalid input |
254 |
| - const fallbackVector = Array(1536).fill(0); |
255 |
| - fallbackVector[0] = 0.2; // Different value for tracking |
256 |
| - return fallbackVector; |
257 |
| - } |
258 |
| - |
259 |
| - // Skip API call for empty text |
260 |
| - if (!text.trim()) { |
261 |
| - logger.warn('Empty text for embedding'); |
262 |
| - const emptyVector = Array(1536).fill(0); |
263 |
| - emptyVector[0] = 0.3; // Different value for tracking |
264 |
| - return emptyVector; |
265 |
| - } |
266 |
| - |
267 |
| - // truncate |
268 |
| - console.log('PROMPT', text); |
269 |
| - if (text.length > 8000) { |
270 |
| - text = text.substring(0, 8000); |
271 |
| - console.log('TRUNCATED PROMPT', text); |
272 |
| - } |
273 |
| - |
274 |
| - try { |
275 |
| - const baseURL = getCloudflareGatewayBaseURL(runtime, 'groq'); |
276 |
| - const model = runtime.getSetting('EMBEDDING_GROQ_MODEL') ?? 'text-embedding-3-small'; |
277 |
| - |
278 |
| - // Call the OpenAI API |
279 |
| - const response = await fetch(`${baseURL}/embeddings`, { |
280 |
| - method: 'POST', |
281 |
| - headers: { |
282 |
| - Authorization: `Bearer ${process.env.GROQ_API_KEY}`, |
283 |
| - 'Content-Type': 'application/json', |
284 |
| - }, |
285 |
| - body: JSON.stringify({ |
286 |
| - model: model, |
287 |
| - input: text, |
288 |
| - }), |
289 |
| - }); |
290 |
| - |
291 |
| - if (!response.ok) { |
292 |
| - logger.error(`OpenAI API error: ${response.status} - ${response.statusText}`); |
293 |
| - const errorVector = Array(1536).fill(0); |
294 |
| - errorVector[0] = 0.4; // Different value for tracking |
295 |
| - return errorVector; |
296 |
| - } |
297 |
| - |
298 |
| - const data = (await response.json()) as { |
299 |
| - data: [{ embedding: number[] }]; |
300 |
| - }; |
301 |
| - |
302 |
| - if (!data?.data?.[0]?.embedding) { |
303 |
| - logger.error('API returned invalid structure'); |
304 |
| - const errorVector = Array(1536).fill(0); |
305 |
| - errorVector[0] = 0.5; // Different value for tracking |
306 |
| - return errorVector; |
307 |
| - } |
308 |
| - |
309 |
| - const embedding = data.data[0].embedding; |
310 |
| - logger.log(`Got valid embedding with length ${embedding.length}`); |
311 |
| - return embedding; |
312 |
| - } catch (error) { |
313 |
| - logger.error('Error generating embedding:', error); |
314 |
| - const errorVector = Array(1536).fill(0); |
315 |
| - errorVector[0] = 0.6; // Different value for tracking |
316 |
| - return errorVector; |
317 |
| - } |
| 238 | + //if (params === null) { |
| 239 | + //logger.debug('Creating test embedding for initialization'); |
| 240 | + // Return a consistent vector for null input |
| 241 | + const testVector = Array(1536).fill(0); |
| 242 | + testVector[0] = 0.1; // Make it non-zero |
| 243 | + return testVector; |
| 244 | + //} |
| 245 | + |
| 246 | + // // Get the text from whatever format was provided |
| 247 | + // let text: string; |
| 248 | + // if (typeof params === 'string') { |
| 249 | + // text = params; // Direct string input |
| 250 | + // } else if (typeof params === 'object' && params.text) { |
| 251 | + // text = params.text; // Object with text property |
| 252 | + // } else { |
| 253 | + // logger.warn('Invalid input format for embedding'); |
| 254 | + // // Return a fallback for invalid input |
| 255 | + // const fallbackVector = Array(1536).fill(0); |
| 256 | + // fallbackVector[0] = 0.2; // Different value for tracking |
| 257 | + // return fallbackVector; |
| 258 | + // } |
| 259 | + |
| 260 | + // // Skip API call for empty text |
| 261 | + // if (!text.trim()) { |
| 262 | + // logger.warn('Empty text for embedding'); |
| 263 | + // const emptyVector = Array(1536).fill(0); |
| 264 | + // emptyVector[0] = 0.3; // Different value for tracking |
| 265 | + // return emptyVector; |
| 266 | + // } |
| 267 | + |
| 268 | + // // truncate |
| 269 | + // console.log('INPUT PROMPT:', text); |
| 270 | + // if (text.length > 8000) { |
| 271 | + // text = text.substring(0, 8000); |
| 272 | + // console.log('TRUNCATED PROMPT', text); |
| 273 | + // } |
| 274 | + |
| 275 | + // const baseURL = getCloudflareGatewayBaseURL(runtime, 'groq'); |
| 276 | + // const data4 = { |
| 277 | + // method: 'GET', |
| 278 | + // headers: { |
| 279 | + // Authorization: `Bearer ${process.env.GROQ_API_KEY}`, |
| 280 | + // 'Content-Type': 'application/json', |
| 281 | + // } |
| 282 | + // }; |
| 283 | + // const response2 = await fetch(`${baseURL}/models`, data4); |
| 284 | + // if (!response2.ok) { |
| 285 | + // logger.error(`GROQ error: ${response2.status} - ${response2.statusText}`); |
| 286 | + // const errorVector = Array(1536).fill(0); |
| 287 | + // errorVector[0] = 0.4; // Different value for tracking |
| 288 | + // return errorVector; |
| 289 | + // } |
| 290 | + // const models = (await response2.json()) as { data: { id: string }[] } |
| 291 | + // console.log(models); |
| 292 | + // console.log('MODELS:', models); |
| 293 | + // //getModels().then((models) => |
| 294 | + |
| 295 | + // //console.log(models); |
| 296 | + // models.data.forEach(async (model) => { |
| 297 | + // console.log("MODEL", model); |
| 298 | + // console.log(model.id) |
| 299 | + // const data1 = { |
| 300 | + // method: 'POST', |
| 301 | + // headers: { |
| 302 | + // Authorization: `Bearer ${process.env.GROQ_API_KEY}`, |
| 303 | + // 'Content-Type': 'application/json', |
| 304 | + // }, |
| 305 | + // body: JSON.stringify({ |
| 306 | + // model: model.id, |
| 307 | + // input: text, |
| 308 | + // }), |
| 309 | + // }; |
| 310 | + // console.log('DATA', data1); |
| 311 | + // const response = await fetch(`${baseURL}/embeddings`, data1); |
| 312 | + // if (!response.ok) { |
| 313 | + // console.log("response", response) |
| 314 | + // logger.error(`GROQ error: ${response.status} - ${response.statusText}`); |
| 315 | + // const errorVector = Array(1536).fill(0); |
| 316 | + // errorVector[0] = 0.4; // Different value for tracking |
| 317 | + // return errorVector; |
| 318 | + // } |
| 319 | + // const data = (await response.json()) as { |
| 320 | + // data: [{ embedding: number[] }]; |
| 321 | + // }; |
| 322 | + // if (!data?.data?.[0]?.embedding) { |
| 323 | + // logger.error('API returned invalid structure'); |
| 324 | + // const errorVector = Array(1536).fill(0); |
| 325 | + // errorVector[0] = 0.5; // Different value for tracking |
| 326 | + // return errorVector; |
| 327 | + // } |
| 328 | + // const embedding = data.data[0].embedding; |
| 329 | + // logger.log(`Got valid embedding with length ${embedding.length}`); |
| 330 | + // return embedding; |
| 331 | + // }) |
| 332 | + //const errorVector = Array(1536).fill(0); |
| 333 | + //errorVector[0] = 0.6; // Different value for tracking |
| 334 | + //return errorVector; |
318 | 335 | },
|
319 | 336 | [ModelType.TEXT_TOKENIZER_ENCODE]: async (
|
320 | 337 | _runtime,
|
|
0 commit comments