graphlit-client
Version:
Graphlit API Client for TypeScript
233 lines (232 loc) • 11 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",
// O1 models
[]: "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",
};
// 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-20241022",
[]: "claude-3-5-sonnet-20240620",
[]: "claude-3-5-sonnet-20241022",
[]: "claude-3-5-haiku-20241022",
[]: "claude-3-5-haiku-20241022",
// Claude 3.7 models
[]: "claude-3-7-sonnet-20250219",
[]: "claude-3-7-sonnet-20250219",
// Claude 4 models
[]: "claude-4-opus-20250514",
[]: "claude-4-sonnet-20250514",
};
// 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.5 models
[]: "gemini-2.5-flash-preview-05-20",
[]: "gemini-2.5-pro-preview-06-05",
};
// 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",
};
// Cerebras model mappings
const CEREBRAS_MODEL_MAP = {
[]: "llama3.3-70b",
[]: "llama3.1-8b",
};
// 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",
};
/**
* 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;
}
// 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;
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,
];
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;
}