llm-util.ts
Overview
The llm-util.ts file provides a set of utility functions related to handling identifiers and names for Large Language Models (LLMs) in the system. These functions facilitate parsing, formatting, and extracting meaningful parts of LLM names and IDs, which are often composite strings containing multiple pieces of information separated by delimiters.
This utility is essential for consistent naming conventions and identifier management when interacting with various third-party LLM integrations, particularly when dealing with model identifiers that combine provider IDs and model names.
Functions
getLLMIconName(fid: string, llm_name: string): string
Returns an icon name or identifier based on the feature ID (fid) and the LLM name.
Parameters:
fid(string): The feature ID or provider identifier for the LLM.llm_name(string): The full name of the LLM, which may contain subcomponents separated by a slash (/).
Returns:
A string representing the icon name.If
fidis'FastEmbed', it returns the substring ofllm_namebefore the first slash (/).Otherwise, it returns the
fiditself.
Usage Example:
const iconName = getLLMIconName('FastEmbed', 'openai/davinci'); // iconName === 'openai' const iconName2 = getLLMIconName('OpenAI', 'openai/davinci'); // iconName2 === 'OpenAI'
getLlmNameAndFIdByLlmId(llmId?: string): { fId?: string; llmName?: string }
Parses a composite LLM ID string and extracts the LLM name and feature ID.
Parameters:
llmId(optional string): A composite string typically structured as"llmName@fId".
Returns:
An object containing two properties:llmName(string | undefined): The substring before the@symbol.fId(string | undefined): The substring after the@symbol.
Usage Example:
const { llmName, fId } = getLlmNameAndFIdByLlmId('openai@OpenAI'); // llmName === 'openai' // fId === 'OpenAI'Notes:
If
llmIdisundefinedor does not contain@, bothllmNameandfIdwill beundefined.
getRealModelName(llmName: string): string
Extracts the core model name from a possibly decorated model string.
Parameters:
llmName(string): A model name string, potentially containing double underscores (__) as separators.
Returns:
The substring before the first double underscore (__). If no double underscore is present, returns the entire string.Context:
The function is designed to handle names returned by the interface that look like"deepseek-r1___OpenAI-API", where the real model name is the first part.Usage Example:
const modelName = getRealModelName('deepseek-r1___OpenAI-API'); // modelName === 'deepseek-r1' const modelName2 = getRealModelName('gpt-4'); // modelName2 === 'gpt-4'
buildLlmUuid(llm: IThirdOAIModel): string
Constructs a unique identifier string for an LLM by combining its name and feature ID.
Parameters:
llm(IThirdOAIModel): An object representing an LLM, containing at least the properties:llm.llm_name(string): The name of the LLM.llm.fid(string): The feature ID or provider identifier.
Returns:
A string concatenatingllm_nameandfidwith an@separator, e.g.,"openai@OpenAI".Usage Example:
const llmObj = { llm_name: 'gpt-4', fid: 'OpenAI' }; const uuid = buildLlmUuid(llmObj); // uuid === 'gpt-4@OpenAI'
Implementation Details
The utility functions rely heavily on string parsing using
.split()and.at(0)to extract relevant parts of the identifiers.The double underscore (
__) and slash (/) characters are used as delimiters in different contexts to separate components of LLM names.The utility assumes specific formats for LLM IDs and names that are consistent across the system, such as
"llmName@fid"or"modelName__extraInfo".
Interaction with Other System Components
The file imports the
IThirdOAIModelinterface from the database LLM interface definitions (@/interfaces/database/llm), indicating that it works closely with data models representing third-party LLMs.These utilities are likely used in:
UI components for displaying LLM icons and names appropriately.
Backend services or API layers for parsing and formatting LLM identifiers.
Any part of the system that needs to generate or interpret LLM unique identifiers and model names.
By standardizing ID and name parsing, this file ensures consistency throughout the application when handling multiple LLM integrations.
Mermaid Diagram
flowchart TD
A[getLLMIconName(fid, llm_name)]
B[getLlmNameAndFIdByLlmId(llmId)]
C[getRealModelName(llmName)]
D[buildLlmUuid(llm: IThirdOAIModel)]
A -->|Uses fid and llm_name| Output1[Icon name string]
B -->|Splits llmId by '@'| Output2[{ llmName, fId }]
C -->|Splits llmName by '__'| Output3[Real model name]
D -->|Concatenate llm_name and fid| Output4[LLM UUID string]
style A fill:#f9f,stroke:#333,stroke-width:1px
style B fill:#bbf,stroke:#333,stroke-width:1px
style C fill:#bfb,stroke:#333,stroke-width:1px
style D fill:#fbf,stroke:#333,stroke-width:1px
Summary
The llm-util.ts file is a concise but crucial utility module for managing LLM identifiers and names within the system. It encapsulates common string parsing and formatting logic to maintain consistent handling of LLM-related strings across UI and backend components. Its simple yet effective functions support the integration of multiple LLM providers by standardizing how model names and feature IDs are interpreted and combined.