Spaces:
Running
Running
File size: 1,193 Bytes
1b44660 |
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 |
import { err, ok } from 'neverthrow';
import { z } from 'zod';
import type { Env } from '../index';
import { tryCatchAsync } from './tryCatchAsync';
const embeddingsResponseSchema = z.object({
embeddings: z.array(z.array(z.number())),
});
export async function createEmbeddings(env: Env, texts: string[]) {
const response = await tryCatchAsync(
fetch(`${env.MERIDIAN_ML_SERVICE_URL}/embeddings`, {
method: 'POST',
body: JSON.stringify({ texts }),
headers: {
Authorization: `Bearer ${env.MERIDIAN_ML_SERVICE_API_KEY}`,
'Content-Type': 'application/json',
},
})
);
if (response.isErr()) {
return err(response.error);
}
if (!response.value.ok) {
return err(new Error(`Failed to fetch embeddings: ${response.value.statusText}`));
}
const jsonResult = await tryCatchAsync(response.value.json());
if (jsonResult.isErr()) {
return err(jsonResult.error);
}
const parsedResponse = embeddingsResponseSchema.safeParse(jsonResult.value);
if (parsedResponse.success === false) {
return err(new Error(`Invalid response ${JSON.stringify(parsedResponse.error)}`));
}
return ok(parsedResponse.data.embeddings);
}
|