mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-21 06:45:25 -04:00
fix(core): async local storage in Setting.with API (#1413)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@llamaindex/core": patch
|
||||
---
|
||||
|
||||
fix: async local storage in `Setting.with` API
|
||||
@@ -4,7 +4,7 @@ const chunkSizeAsyncLocalStorage = new AsyncLocalStorage<number | undefined>();
|
||||
let globalChunkSize: number = 1024;
|
||||
|
||||
export function getChunkSize(): number {
|
||||
return globalChunkSize ?? chunkSizeAsyncLocalStorage.getStore();
|
||||
return chunkSizeAsyncLocalStorage.getStore() ?? globalChunkSize;
|
||||
}
|
||||
|
||||
export function setChunkSize(chunkSize: number | undefined) {
|
||||
|
||||
@@ -5,7 +5,7 @@ const llmAsyncLocalStorage = new AsyncLocalStorage<LLM>();
|
||||
let globalLLM: LLM | undefined;
|
||||
|
||||
export function getLLM(): LLM {
|
||||
const currentLLM = globalLLM ?? llmAsyncLocalStorage.getStore();
|
||||
const currentLLM = llmAsyncLocalStorage.getStore() ?? globalLLM;
|
||||
if (!currentLLM) {
|
||||
throw new Error(
|
||||
"Cannot find LLM, please set `Settings.llm = ...` on the top of your code",
|
||||
|
||||
@@ -4,7 +4,7 @@ const chunkSizeAsyncLocalStorage = new AsyncLocalStorage<Tokenizer>();
|
||||
let globalTokenizer: Tokenizer = tokenizers.tokenizer();
|
||||
|
||||
export function getTokenizer(): Tokenizer {
|
||||
return globalTokenizer ?? chunkSizeAsyncLocalStorage.getStore();
|
||||
return chunkSizeAsyncLocalStorage.getStore() ?? globalTokenizer;
|
||||
}
|
||||
|
||||
export function setTokenizer(tokenizer: Tokenizer | undefined) {
|
||||
|
||||
Generated
+6
@@ -1324,6 +1324,12 @@ importers:
|
||||
'@llamaindex/cloud':
|
||||
specifier: workspace:*
|
||||
version: link:../packages/cloud
|
||||
'@llamaindex/core':
|
||||
specifier: workspace:*
|
||||
version: link:../packages/core
|
||||
'@llamaindex/openai':
|
||||
specifier: workspace:*
|
||||
version: link:../packages/providers/openai
|
||||
'@llamaindex/readers':
|
||||
specifier: workspace:*
|
||||
version: link:../packages/readers
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@llamaindex/cloud": "workspace:*",
|
||||
"@llamaindex/core": "workspace:*",
|
||||
"@llamaindex/openai": "workspace:*",
|
||||
"@llamaindex/readers": "workspace:*",
|
||||
"llamaindex": "workspace:*"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Settings as RootSettings } from "@llamaindex/core/global";
|
||||
import { OpenAI } from "@llamaindex/openai";
|
||||
import { Settings } from "llamaindex";
|
||||
import { beforeEach, expect, test } from "vitest";
|
||||
const defaultLLM = new OpenAI();
|
||||
|
||||
beforeEach(() => {
|
||||
RootSettings.llm = defaultLLM;
|
||||
});
|
||||
|
||||
test("async local storage with core", () => {
|
||||
const symbol = Symbol("llm");
|
||||
RootSettings.withLLM(symbol as never, () => {
|
||||
expect(RootSettings.llm).toBe(symbol);
|
||||
});
|
||||
});
|
||||
|
||||
test("async local storage with llamaindex", () => {
|
||||
const symbol = Symbol("llm");
|
||||
Settings.withLLM(symbol as never, () => {
|
||||
expect(Settings.llm).toBe(symbol);
|
||||
expect(RootSettings.llm).toBe(symbol);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user