Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

2.1 KB
Raw Blame
import type { ResponseHeadersListener } from "@/lib/http/observeResponseHeaders";
import { toast } from "@/lib/toast";
import { getGatewayBaseUrl, getGlobalLitellmHeaderName } from "@/components/networking";
import {
  buildPlaygroundHeaders,
  type CustomHeaders,
  withRequiredHeaders,
} from "@/components/llm_calls/request_headers";

export async function makeOpenAIEmbeddingsRequest(
  input: string,
  updateEmbeddingsUI: (embeddings: string, model?: string) => void,
  selectedModel: string,
  accessToken: string,
  tags?: string[],
  customBaseUrl?: string,
  customHeaders?: CustomHeaders,
  onResponseHeaders?: ResponseHeadersListener,
) {
  if (!accessToken) {
    throw new Error("Virtual Key is required");
  }

  // Base URL should be the current base_url
  const isLocal = process.env.NODE_ENV === "development";
  if (isLocal !== true) {
    console.log = function () {};
  }

  const proxyBaseUrl = customBaseUrl || getGatewayBaseUrl();
  const headers = withRequiredHeaders(buildPlaygroundHeaders(tags, customHeaders), {
    "Content-Type": "application/json",
    [getGlobalLitellmHeaderName()]: `Bearer ${accessToken}`,
  });

  try {
    const normalizedBaseUrl = proxyBaseUrl.endsWith("/") ? proxyBaseUrl.slice(0, -1) : proxyBaseUrl;
    const requestUrl = `${normalizedBaseUrl}/embeddings`;

    const response = await fetch(requestUrl, {
      method: "POST",
      headers,
      body: JSON.stringify({
        model: selectedModel,
        input,
      }),
    });
    onResponseHeaders?.(response.headers);

    if (!response.ok) {
      const errorText = await response.text();
      throw new Error(errorText || `Request failed with status ${response.status}`);
    }

    const responseData = await response.json();
    const embedding = responseData?.data?.[0]?.embedding;

    if (!embedding) {
      throw new Error("No embedding returned from server");
    }

    updateEmbeddingsUI(JSON.stringify(embedding), responseData?.model ?? selectedModel);
  } catch (error: unknown) {
    toast.fromError(`Error occurred while making embeddings request. Please try again. Error: ${error}`);

    throw error; // Re-throw to allow the caller to handle the error
  }
}