graphlit-client
Version:
Graphlit API Client for TypeScript
388 lines (387 loc) • 18.7 kB
JavaScript
import * as Types from "./generated/graphql-types.js";
/**
* Model mapping utilities to convert Graphlit specification enums
* to actual model names used by LLM SDKs
*/
// OpenAI model mappings
const OPENAI_MODEL_MAP = {
// GPT-4 Turbo models
[]: "gpt-4-turbo",
[]: "gpt-4-0125-preview",
[]: "gpt-4-1106-preview",
[]: "gpt-4-turbo-2024-04-09",
// GPT-4o models
[]: "gpt-4o",
[]: "gpt-4o-2024-05-13",
[]: "gpt-4o-2024-08-06",
[]: "gpt-4o-2024-11-20",
[]: "chatgpt-4o-latest",
// GPT-4o Mini models
[]: "gpt-4o-mini",
[]: "gpt-4o-mini-2024-07-18",
// GPT 4.1 models
[]: "gpt-4.1",
[]: "gpt-4.1-2025-04-14",
[]: "gpt-4.1-mini",
[]: "gpt-4.1-mini-2025-04-14",
[]: "gpt-4.1-nano",
[]: "gpt-4.1-nano-2025-04-14",
// GPT-4.5 Preview models (deprecated)
[]: "gpt-4.5-preview",
[]: "gpt-4.5-preview-2025-02-27",
// GPT-5 models
[]: "gpt-5",
[]: "gpt-5-2025-08-07",
[]: "chatgpt-5-latest",
[]: "gpt-5-mini",
[]: "gpt-5-mini-2025-08-07",
[]: "gpt-5-nano",
[]: "gpt-5-nano-2025-08-07",
// GPT-5.1 models
[]: "gpt-5.1",
[]: "gpt-5.1-2025-11-13",
// GPT-5.2 models
[]: "gpt-5.2",
[]: "gpt-5.2-2025-12-11",
// GPT-5.4 models
[]: "gpt-5.4",
[]: "gpt-5.4-2026-03-05",
[]: "gpt-5.4-mini",
[]: "gpt-5.4-mini-2026-03-17",
[]: "gpt-5.4-nano",
[]: "gpt-5.4-nano-2026-03-17",
[]: "gpt-5.5",
[]: "gpt-5.5-2026-04-23",
// O1 models
[]: "o1",
[]: "o1-2024-12-17",
[]: "o1-mini",
[]: "o1-mini-2024-09-12",
[]: "o1-preview",
[]: "o1-preview-2024-09-12",
// O3 models
[]: "o3-mini",
[]: "o3-mini-2025-01-31",
[]: "o3",
[]: "o3-2025-04-16",
// O4 models
[]: "o4-mini",
[]: "o4-mini-2025-04-16",
};
const OPENAI_RESPONSES_MODEL_ENUMS = new Set([
Types.OpenAiModels.Gpt54_1024K,
Types.OpenAiModels.Gpt54_1024K_20260305,
Types.OpenAiModels.Gpt54Mini_400K,
Types.OpenAiModels.Gpt54Mini_400K_20260317,
Types.OpenAiModels.Gpt54Nano_400K,
Types.OpenAiModels.Gpt54Nano_400K_20260317,
Types.OpenAiModels.Gpt55_1024K,
Types.OpenAiModels.Gpt55_1024K_20260423,
]);
// Anthropic model mappings
const ANTHROPIC_MODEL_MAP = {
// Claude 3 models
[]: "claude-3-opus-20240229",
[]: "claude-3-opus-20240229",
[]: "claude-3-sonnet-20240229",
[]: "claude-3-sonnet-20240229",
[]: "claude-3-haiku-20240307",
[]: "claude-3-haiku-20240307",
// Claude 3.5 models
[]: "claude-3-5-sonnet-latest",
[]: "claude-3-5-sonnet-20240620",
[]: "claude-3-5-sonnet-20241022",
[]: "claude-3-5-haiku-latest",
[]: "claude-3-5-haiku-20241022",
// Claude 3.7 models
[]: "claude-3-7-sonnet-latest",
[]: "claude-3-7-sonnet-20250219",
// Claude 4 models
[]: "claude-opus-4-0",
[]: "claude-opus-4-20250514",
[]: "claude-sonnet-4-0",
[]: "claude-sonnet-4-20250514",
// Claude 4.1 models
[]: "claude-opus-4-1",
[]: "claude-opus-4-1-20250805",
// Claude 4.5 models
[]: "claude-opus-4-5",
[]: "claude-opus-4-5-20251101",
[]: "claude-sonnet-4-5",
[]: "claude-sonnet-4-5-20250929",
[]: "claude-haiku-4-5",
[]: "claude-haiku-4-5-20251001",
// Claude 4.6 models (1M variants use the same model ID; context window is a beta flag)
[]: "claude-opus-4-6",
[]: "claude-opus-4-6-20260205",
[]: "claude-opus-4-6",
[]: "claude-opus-4-6-20260205",
[]: "claude-sonnet-4-6",
[]: "claude-sonnet-4-6-20260217",
[]: "claude-sonnet-4-6",
[]: "claude-sonnet-4-6-20260217",
// Claude 4.7 models (1M context is native to the model, no beta header required)
[]: "claude-opus-4-7",
};
// Google model mappings
const GOOGLE_MODEL_MAP = {
// Gemini 1.5 Pro models
[]: "gemini-1.5-pro",
[]: "gemini-1.5-pro-001",
[]: "gemini-1.5-pro-002",
// Gemini 1.5 Flash models
[]: "gemini-1.5-flash",
[]: "gemini-1.5-flash-001",
[]: "gemini-1.5-flash-002",
// Gemini 1.5 Flash 8B models
[]: "gemini-1.5-flash-8b",
[]: "gemini-1.5-flash-8b-001",
// Gemini 2.0 Flash models
[]: "gemini-2.0-flash-exp",
[]: "gemini-2.0-flash-001",
[]: "gemini-2.0-flash-exp",
[]: "gemini-2.0-flash-thinking-exp",
[]: "gemini-2.0-pro-exp",
// Gemini 2.5 models
[]: "gemini-2.5-flash-preview-05-20",
[]: "gemini-2.5-pro-preview-06-05",
[]: "gemini-2.5-pro-exp",
[]: "gemini-2.5-flash-lite",
[]: "gemini-2.5-flash",
[]: "gemini-2.5-pro",
// Gemini 3 models
[]: "gemini-3-flash-preview",
[]: "gemini-3.5-flash",
[]: "gemini-3-pro-preview",
[]: "gemini-3.1-flash-lite-preview",
[]: "gemini-3.1-pro-preview",
// Gemini Latest models
[]: "gemini-flash-latest",
[]: "gemini-flash-lite-latest",
};
// Groq model mappings
const GROQ_MODEL_MAP = {
[]: "meta-llama/llama-4-scout-17b-16e-instruct",
[]: "meta-llama/llama-4-maverick-17b-128e-instruct",
[]: "deepseek-r1-distill-llama-70b",
[]: "llama-3.3-70b-versatile",
[]: "mixtral-8x7b-32768",
[]: "llama-3.1-8b-instant",
[]: "llama3-70b-8192",
[]: "llama3-8b-8192",
[]: "qwen/qwen3-32b",
[]: "moonshotai/kimi-k2-instruct",
};
// Cerebras model mappings
const CEREBRAS_MODEL_MAP = {
[]: "llama3.3-70b",
[]: "llama3.1-8b",
[]: "llama-4-scout-17b-16e-instruct",
[]: "qwen-3-32b",
};
// Cohere model mappings
const COHERE_MODEL_MAP = {
[]: "command-r-plus",
[]: "command-r-plus-04-2024",
[]: "command-r-plus-08-2024",
[]: "command-r",
[]: "command-r-03-2024",
[]: "command-r-08-2024",
[]: "command-r7b-12-2024",
[]: "command-a-03-2025",
[]: "command-a-03-2025",
};
// Mistral model mappings
const MISTRAL_MODEL_MAP = {
[]: "mistral-embed",
[]: "pixtral-large-latest",
[]: "pixtral-12b-2409",
[]: "open-mistral-nemo",
[]: "mistral-small-latest",
[]: "mistral-medium-latest",
[]: "mistral-large-latest",
};
// Bedrock model mappings
// Note: These use "us." prefix for us-east-2 region. For other regions,
// use the modelName field in specification to override with correct region prefix
const BEDROCK_MODEL_MAP = {
[]: "us.amazon.nova-premier-v1:0",
[]: "us.amazon.nova-pro-v1:0",
[]: "us.anthropic.claude-3-7-sonnet-20250219-v1:0",
[]: "us.meta.llama4-scout-17b-instruct-v1:0",
[]: "us.meta.llama4-maverick-17b-instruct-v1:0",
};
// Deepseek model mappings (uses OpenAI-compatible API)
const DEEPSEEK_MODEL_MAP = {
[]: "deepseek-chat",
[]: "deepseek-reasoner",
};
// xAI model mappings
const XAI_MODEL_MAP = {
[]: "grok-4",
[]: "grok-3",
[]: "grok-3-mini",
};
/**
* Get the actual model name for a given specification
* @param specification - The Graphlit specification object
* @returns The SDK-compatible model name
*/
export function getModelName(specification) {
const serviceType = specification?.serviceType;
// Check for custom model names first
if (specification?.openAI?.modelName) {
return specification.openAI.modelName;
}
if (specification?.anthropic?.modelName) {
return specification.anthropic.modelName;
}
if (specification?.google?.modelName) {
return specification.google.modelName;
}
if (specification?.groq?.modelName) {
return specification.groq.modelName;
}
if (specification?.cerebras?.modelName) {
return specification.cerebras.modelName;
}
if (specification?.cohere?.modelName) {
return specification.cohere.modelName;
}
if (specification?.mistral?.modelName) {
return specification.mistral.modelName;
}
if (specification?.bedrock?.modelName) {
return specification.bedrock.modelName;
}
if (specification?.deepseek?.modelName) {
return specification.deepseek.modelName;
}
if (specification?.xai?.modelName) {
return specification.xai.modelName;
}
// Map based on service type and model enum
switch (serviceType) {
case Types.ModelServiceTypes.OpenAi:
const openAIModel = specification?.openAI?.model;
return openAIModel ? OPENAI_MODEL_MAP[openAIModel] : undefined;
case Types.ModelServiceTypes.Anthropic:
const anthropicModel = specification?.anthropic?.model;
return anthropicModel ? ANTHROPIC_MODEL_MAP[anthropicModel] : undefined;
case Types.ModelServiceTypes.Google:
const googleModel = specification?.google?.model;
return googleModel ? GOOGLE_MODEL_MAP[googleModel] : undefined;
case Types.ModelServiceTypes.Groq:
const groqModel = specification?.groq?.model;
return groqModel ? GROQ_MODEL_MAP[groqModel] : undefined;
case Types.ModelServiceTypes.Cerebras:
const cerebrasModel = specification?.cerebras?.model;
return cerebrasModel ? CEREBRAS_MODEL_MAP[cerebrasModel] : undefined;
case Types.ModelServiceTypes.Cohere:
const cohereModel = specification?.cohere?.model;
return cohereModel ? COHERE_MODEL_MAP[cohereModel] : undefined;
case Types.ModelServiceTypes.Mistral:
const mistralModel = specification?.mistral?.model;
return mistralModel ? MISTRAL_MODEL_MAP[mistralModel] : undefined;
case Types.ModelServiceTypes.Bedrock:
// For Bedrock, we use the bedrock model field
const bedrockModel = specification?.bedrock?.model;
return bedrockModel ? BEDROCK_MODEL_MAP[bedrockModel] : undefined;
case Types.ModelServiceTypes.Deepseek:
const deepseekModel = specification?.deepseek?.model;
return deepseekModel ? DEEPSEEK_MODEL_MAP[deepseekModel] : undefined;
case Types.ModelServiceTypes.Xai:
const xaiModel = specification?.xai?.model;
return xaiModel ? XAI_MODEL_MAP[xaiModel] : undefined;
default:
return undefined;
}
}
/**
* Check if a service type supports streaming
* @param serviceType - The model service type
* @returns True if streaming is supported
*/
export function isStreamingSupported(serviceType) {
const streamingServices = [
Types.ModelServiceTypes.OpenAi,
Types.ModelServiceTypes.Anthropic,
Types.ModelServiceTypes.Google,
Types.ModelServiceTypes.Groq,
Types.ModelServiceTypes.Cerebras,
Types.ModelServiceTypes.Cohere,
Types.ModelServiceTypes.Mistral,
Types.ModelServiceTypes.Bedrock,
Types.ModelServiceTypes.Deepseek,
Types.ModelServiceTypes.Xai,
];
return streamingServices.includes(serviceType);
}
/**
* Get the service type from specification
* @param specification - The specification object
* @returns The service type string
*/
export function getServiceType(specification) {
return specification?.serviceType;
}
/**
* Get the model enum value from specification
* @param specification - The specification object
* @returns The model enum value
*/
export function getModelEnum(specification) {
const serviceType = specification?.serviceType;
switch (serviceType) {
case Types.ModelServiceTypes.OpenAi:
return specification?.openAI?.model;
case Types.ModelServiceTypes.Anthropic:
return specification?.anthropic?.model;
case Types.ModelServiceTypes.Google:
return specification?.google?.model;
case Types.ModelServiceTypes.Groq:
return specification?.groq?.model;
case Types.ModelServiceTypes.Cerebras:
return specification?.cerebras?.model;
case Types.ModelServiceTypes.Cohere:
return specification?.cohere?.model;
case Types.ModelServiceTypes.Mistral:
return specification?.mistral?.model;
case Types.ModelServiceTypes.Bedrock:
return specification?.bedrock?.model;
case Types.ModelServiceTypes.Deepseek:
return specification?.deepseek?.model;
case Types.ModelServiceTypes.Xai:
return specification?.xai?.model;
default:
return undefined;
}
}
/**
* Check whether an OpenAI specification should use the Responses API path.
* Eligible models are GPT-5.4 and newer.
*/
export function isOpenAIResponsesEligibleModel(specification) {
if (specification?.serviceType !== Types.ModelServiceTypes.OpenAi) {
return false;
}
const modelEnum = getModelEnum(specification);
if (modelEnum && OPENAI_RESPONSES_MODEL_ENUMS.has(modelEnum)) {
return true;
}
const modelName = getModelName(specification);
if (!modelName) {
return false;
}
const match = modelName.match(/^gpt-(\d+)(?:\.(\d+))?(?:-[a-z0-9]+)?(?:-\d{4}-\d{2}-\d{2})?$/);
if (!match) {
return false;
}
const major = Number(match[1]);
const minor = match[2] ? Number(match[2]) : 0;
if (!Number.isFinite(major) || !Number.isFinite(minor)) {
return false;
}
return major > 5 || (major === 5 && minor >= 4);
}
export const shouldUseOpenAIResponsesModel = isOpenAIResponsesEligibleModel;