Data framework for your LLM application
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Global configuration system for managing LLMs, embedding models, and processing parameters across the LlamaIndex.TS framework.
import { Settings } from "llamaindex";The Settings object provides centralized configuration management for all LlamaIndex.TS components. It uses a singleton pattern to ensure consistent settings across your application and provides temporary context switching capabilities.
interface GlobalSettings {
llm: LLM;
embedModel: BaseEmbedding;
nodeParser: NodeParser;
promptHelper: PromptHelper;
callbackManager: CallbackManager;
chunkSize: number | undefined;
chunkOverlap: number | undefined;
prompt: PromptConfig;
debug: boolean;
withLLM<Result>(llm: LLM, fn: () => Result): Result;
withEmbedModel<Result>(embedModel: BaseEmbedding, fn: () => Result): Result;
withNodeParser<Result>(nodeParser: NodeParser, fn: () => Result): Result;
withPromptHelper<Result>(promptHelper: PromptHelper, fn: () => Result): Result;
withCallbackManager<Result>(callbackManager: CallbackManager, fn: () => Result): Result;
withChunkSize<Result>(chunkSize: number, fn: () => Result): Result;
withChunkOverlap<Result>(chunkOverlap: number, fn: () => Result): Result;
withPrompt<Result>(prompt: PromptConfig, fn: () => Result): Result;
}interface Config {
prompt: PromptConfig;
promptHelper: PromptHelper | null;
embedModel: BaseEmbedding | null;
nodeParser: NodeParser | null;
callbackManager: CallbackManager | null;
chunkSize: number | undefined;
chunkOverlap: number | undefined;
}
interface PromptConfig {
llm?: string;
lang?: string;
}import { Settings, OpenAI, OpenAIEmbedding } from "llamaindex";
// Set global LLM
Settings.llm = new OpenAI({
model: "gpt-4",
temperature: 0.1,
});
// Set global embedding model
Settings.embedModel = new OpenAIEmbedding({
model: "text-embedding-ada-002",
});
// Set chunk size for text splitting
Settings.chunkSize = 1024;
Settings.chunkOverlap = 20;
// Enable debug mode
Settings.debug = true;Use the with* methods to temporarily override settings for specific operations:
import { Settings, OpenAI, Document, VectorStoreIndex } from "llamaindex";
const documents = [new Document({ text: "Example content" })];
// Use different LLM for this specific operation
const result = Settings.withLLM(
new OpenAI({ model: "gpt-3.5-turbo" }),
() => {
return VectorStoreIndex.fromDocuments(documents);
}
);
// Settings.llm reverts to original value after the function completes// Override multiple settings temporarily
Settings.withChunkSize(512, () => {
Settings.withChunkOverlap(50, () => {
// Process with different chunking parameters
const index = VectorStoreIndex.fromDocuments(documents);
return index;
});
});llm: The language model used for text generation and completionembedModel: The embedding model used for generating vector representationsnodeParser: The text splitter used for chunking documentspromptHelper: Helper for managing prompt templates and formattingcallbackManager: Manager for handling events and callbackschunkSize: Default size for text chunks (undefined uses model default)chunkOverlap: Overlap between text chunks for better continuityprompt: Prompt configuration including language and LLM preferencesdebug: Enable debug logging and verbose outputAll with* methods follow the same pattern - they temporarily override a setting for the duration of the provided function:
Temporarily use a different language model.
Parameters:
llm: LLM - The LLM to use temporarilyfn: () => Result - Function to execute with the temporary LLMReturns: The result of executing fn
Temporarily use a different embedding model.
Parameters:
embedModel: BaseEmbedding - The embedding model to use temporarilyfn: () => Result - Function to execute with the temporary embedding modelReturns: The result of executing fn
Temporarily use a different chunk size.
Parameters:
chunkSize: number - The chunk size to use temporarilyfn: () => Result - Function to execute with the temporary chunk sizeReturns: The result of executing fn
Set up your global settings at the start of your application:
// At app initialization
Settings.llm = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
Settings.embedModel = new OpenAIEmbedding();
Settings.chunkSize = 1024;
Settings.chunkOverlap = 20;Use temporary context switching when you need different settings for specific operations:
// Different chunk size for processing large documents
const processLargeDoc = (doc: Document) => {
return Settings.withChunkSize(2048, () => {
return VectorStoreIndex.fromDocuments([doc]);
});
};
// Different LLM for specific queries
const getCheapAnswer = (query: string, index: VectorStoreIndex) => {
return Settings.withLLM(new OpenAI({ model: "gpt-3.5-turbo" }), () => {
const queryEngine = index.asQueryEngine();
return queryEngine.query(query);
});
};Configure settings based on your environment:
if (process.env.NODE_ENV === "development") {
Settings.debug = true;
Settings.llm = new OpenAI({ model: "gpt-3.5-turbo" }); // Cheaper for dev
} else {
Settings.debug = false;
Settings.llm = new OpenAI({ model: "gpt-4" }); // Better for prod
}