use-llm-request.ts
Overview
The use-llm-request.ts file provides a set of React hooks and utility functions for fetching, managing, and querying Large Language Model (LLM) data from a backend service. It leverages the React Query library (@tanstack/react-query) to handle data fetching and caching, and interfaces with a user service API to retrieve LLM model information.
This file primarily focuses on:
Fetching a collection of LLM models based on an optional model type filter.
Flattening and augmenting the fetched LLM data with unique identifiers (UUIDs).
Providing a convenient lookup mechanism to find a specific LLM model by its UUID.
These utilities are designed to be used in React components or hooks to simplify interaction with LLM model data in the application.
Detailed Explanations
Enumerations
LLMApiAction
export const enum LLMApiAction {
LlmList = 'llmList',
}
Purpose: Defines API action keys used for query keys in React Query.
Usage: Helps maintain consistent query keys for caching and invalidation.
Hooks and Functions
useFetchLlmList
export const useFetchLlmList = (modelType?: LlmModelType) => { ... }
Purpose: Fetches a collection of LLM models from the backend API, optionally filtered by
modelType.Parameters:
modelType?(LlmModelType): Optional filter specifying the type of LLM models to fetch.
Returns:
IThirdAiModelCollection— An object containing LLM models grouped by categories or keys.
Implementation Details:
Uses
useQueryfrom React Query for data fetching and caching.Calls
userService.llm_listwith an optionalmodel_typeparameter.Returns the
data.datafield from the API response or an empty object if none.
Usage Example:
const llmList = useFetchLlmList('openai'); console.log(llmList);
useSelectFlatLlmList
export function useSelectFlatLlmList(modelType?: LlmModelType): IThirdOAIModelWithUuid[] { ... }
Purpose: Converts the nested LLM model collection into a flat array of models, each enriched with a unique UUID.
Parameters:
modelType?(LlmModelType): Optional filter for fetching specific model types.
Returns:
Array of
IThirdOAIModelWithUuidobjects, where each object includes all properties ofIThirdOAIModelplus a generateduuidstring.
Implementation Details:
Internally calls
useFetchLlmListto get the grouped model collection.Uses
Object.valuesandArray.reduceto flatten nested arrays.Calls
buildLlmUuidutility to generate a UUID for each model.
Usage Example:
const flatLlmList = useSelectFlatLlmList('openai'); flatLlmList.forEach(model => console.log(model.uuid, model.name));
useFindLlmByUuid
export function useFindLlmByUuid(modelType?: LlmModelType): (uuid: string) => IThirdOAIModelWithUuid | undefined { ... }
Purpose: Returns a function that can find a specific LLM model by its UUID.
Parameters:
modelType?(LlmModelType): Optional filter used to fetch the relevant models before searching.
Returns:
A function
(uuid: string) => IThirdOAIModelWithUuid | undefinedthat searches the flat list for a model matching the given UUID.
Implementation Details:
Calls
useSelectFlatLlmListto get the flat list of models.Implements a simple
.findmethod to locate the model by UUID.
Usage Example:
const findLlmByUuid = useFindLlmByUuid('openai'); const model = findLlmByUuid('some-uuid-string'); if (model) { console.log('Found model:', model.name); }
Important Implementation Details and Algorithms
React Query Integration:
The file usesuseQueryfor declarative data fetching with automatic caching, background updates, and stale data management.Data Flattening Algorithm:
The nested LLM model collection (an object where values are arrays of models) is flattened into a single array by:Extracting values with
Object.values.Reducing these arrays into a single array with
.reduce.Augmenting each model with a UUID using the
buildLlmUuidutility function.
UUID Generation:
ThebuildLlmUuidfunction (imported from@/utils/llm-util) is used to generate unique identifiers for LLM models, enabling easy lookup and identification.
Interaction With Other Parts of the System
userService(@/services/user-service):
This module calls the backend API to retrieve LLM model data. It abstracts the HTTP requests and endpoints.LlmModelType(@/constants/knowledge):
Defines the enumeration or types for different LLM model categories/types.IThirdOAIModelCollectionandIThirdOAIModel(@/interfaces/database/llm):
TypeScript interfaces defining the structure of the LLM data returned from the backend.buildLlmUuidUtility (@/utils/llm-util):
Responsible for generating a unique UUID string for each LLM model based on its properties.React Query (
@tanstack/react-query):
Provides hooks for asynchronous data fetching and caching.
This module acts as a bridge between backend LLM data and React components, encapsulating data fetching and transformation logic.
Visual Diagram
flowchart TD
A[useFetchLlmList(modelType?)] -->|fetches| B[userService.llm_list({ model_type })]
B -->|returns| C{IThirdAiModelCollection}
C --> D[useSelectFlatLlmList(modelType?)]
D -->|flatten & add uuid| E[IThirdOAIModelWithUuid[]]
E --> F[useFindLlmByUuid(modelType?)]
F -->|returns| G[(uuid) => IThirdOAIModelWithUuid | undefined]
subgraph External Modules
B
C
buildLlmUuid
end
D -. uses .-> buildLlmUuid
Summary
The use-llm-request.ts file encapsulates LLM model data fetching, flattening, and lookup logic in reusable React hooks. It simplifies data retrieval from the backend and provides efficient ways to access and manipulate LLM models by category or unique identifiers within the application. This modular design improves maintainability and integration with React Query for state management.