fix(core): async local storage in Setting.with API (#1413)

This commit is contained in:
Alex Yang
2024-10-31 11:06:14 -07:00
committed by GitHub
parent 52a4d2b83d
commit 9c73f0a530
7 changed files with 40 additions and 3 deletions
+5
View File
@@ -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) {
+1 -1
View File
@@ -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) {
+6
View File
@@ -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
+2
View File
@@ -13,6 +13,8 @@
},
"dependencies": {
"@llamaindex/cloud": "workspace:*",
"@llamaindex/core": "workspace:*",
"@llamaindex/openai": "workspace:*",
"@llamaindex/readers": "workspace:*",
"llamaindex": "workspace:*"
}
+24
View File
@@ -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);
});
});