UNPKG

llamaindex

Version:

<p align="center"> <img height="100" width="100" alt="LlamaIndex logo" src="https://ts.llamaindex.ai/square.svg" /> </p> <h1 align="center">LlamaIndex.TS</h1> <h3 align="center"> Data framework for your LLM application. </h3>

65 lines (64 loc) 2.46 kB
import { client, listProjectsApiV1ProjectsGet, searchPipelinesApiV1PipelinesGet } from "@llamaindex/cloud/api"; import { DEFAULT_BASE_URL } from "@llamaindex/core/global"; import { getEnv } from "@llamaindex/env"; function getBaseUrl(baseUrl) { return baseUrl ?? getEnv("LLAMA_CLOUD_BASE_URL") ?? DEFAULT_BASE_URL; } export function getAppBaseUrl() { return client.getConfig().baseUrl?.replace(/api\./, "") ?? ""; } // fixme: refactor this to init at the top level or module level let initOnce = false; export function initService({ apiKey, baseUrl } = {}) { if (initOnce) { return; } initOnce = true; client.setConfig({ baseUrl: getBaseUrl(baseUrl), throwOnError: true }); const token = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY"); client.interceptors.request.use((request)=>{ request.headers.set("Authorization", `Bearer ${token}`); return request; }); client.interceptors.error.use((error)=>{ throw new Error(`LlamaCloud API request failed. Error details: ${JSON.stringify(error)}`); }); if (!token) { throw new Error("API Key is required for LlamaCloudIndex. Please pass the apiKey parameter"); } } export async function getProjectId(projectName, organizationId) { const { data: projects } = await listProjectsApiV1ProjectsGet({ query: { project_name: projectName, organization_id: organizationId ?? null }, throwOnError: true }); if (projects.length === 0) { throw new Error(`Unknown project name ${projectName}. Please confirm a managed project with this name exists.`); } else if (projects.length > 1) { throw new Error(`Multiple projects found with name ${projectName}. Please specify organization_id.`); } const project = projects[0]; if (!project.id) { throw new Error(`No project found with name ${projectName}`); } return project.id; } export async function getPipelineId(name, projectName, organizationId) { const { data: pipelines } = await searchPipelinesApiV1PipelinesGet({ query: { project_id: await getProjectId(projectName, organizationId), pipeline_name: name }, throwOnError: true }); if (pipelines.length === 0 || !pipelines[0].id) { throw new Error(`No pipeline found with name ${name} in project ${projectName}`); } return pipelines[0].id; }