Skip to content

Commit 304796e

Browse files
feat: support setting maxConcurrentChunks for Generic OpenAI embedder (#2655)
* exposes `maxConcurrentChunks` parameter for the generic openai embedder through configuration. This allows setting a batch size for endpoints which don't support the default of 500 * Update new field to new UI make getting to ensure proper type and format --------- Co-authored-by: timothycarambat <[email protected]>
1 parent b66017d commit 304796e

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

docker/.env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ GID='1000'
164164
# EMBEDDING_MODEL_MAX_CHUNK_LENGTH=8192
165165
# EMBEDDING_BASE_PATH='http://127.0.0.1:4000'
166166
# GENERIC_OPEN_AI_EMBEDDING_API_KEY='sk-123abc'
167+
# GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS=500
167168

168169
###########################################
169170
######## Vector Database Selection ########
@@ -299,4 +300,4 @@ GID='1000'
299300

300301
# Enable simple SSO passthrough to pre-authenticate users from a third party service.
301302
# See https://docs.anythingllm.com/configuration#simple-sso-passthrough for more information.
302-
# SIMPLE_SSO_ENABLED=1
303+
# SIMPLE_SSO_ENABLED=1

frontend/src/components/EmbeddingSelection/GenericOpenAiOptions/index.jsx

+44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import React, { useState } from "react";
2+
import { CaretDown, CaretUp } from "@phosphor-icons/react";
3+
14
export default function GenericOpenAiEmbeddingOptions({ settings }) {
5+
const [showAdvancedControls, setShowAdvancedControls] = useState(false);
26
return (
37
<div className="w-full flex flex-col gap-y-7">
48
<div className="w-full flex items-center gap-[36px] mt-1.5 flex-wrap">
@@ -69,6 +73,46 @@ export default function GenericOpenAiEmbeddingOptions({ settings }) {
6973
/>
7074
</div>
7175
</div>
76+
<div className="flex justify-start mt-4">
77+
<button
78+
onClick={(e) => {
79+
e.preventDefault();
80+
setShowAdvancedControls(!showAdvancedControls);
81+
}}
82+
className="text-white hover:text-white/70 flex items-center text-sm"
83+
>
84+
{showAdvancedControls ? "Hide" : "Show"} advanced settings
85+
{showAdvancedControls ? (
86+
<CaretUp size={14} className="ml-1" />
87+
) : (
88+
<CaretDown size={14} className="ml-1" />
89+
)}
90+
</button>
91+
</div>
92+
<div hidden={!showAdvancedControls}>
93+
<div className="w-full flex items-start gap-4">
94+
<div className="flex flex-col w-60">
95+
<div className="flex flex-col gap-y-1 mb-4">
96+
<label className="text-white text-sm font-semibold flex items-center gap-x-2">
97+
Max concurrent Chunks
98+
<p className="!text-xs !italic !font-thin">optional</p>
99+
</label>
100+
</div>
101+
<input
102+
type="number"
103+
name="GenericOpenAiEmbeddingMaxConcurrentChunks"
104+
className="bg-theme-settings-input-bg text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
105+
placeholder="500"
106+
min={1}
107+
onScroll={(e) => e.target.blur()}
108+
defaultValue={settings?.GenericOpenAiEmbeddingMaxConcurrentChunks}
109+
required={false}
110+
autoComplete="off"
111+
spellCheck={false}
112+
/>
113+
</div>
114+
</div>
115+
</div>
72116
</div>
73117
);
74118
}

server/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ SIG_SALT='salt' # Please generate random string at least 32 chars long.
154154
# EMBEDDING_MODEL_MAX_CHUNK_LENGTH=8192
155155
# EMBEDDING_BASE_PATH='http://127.0.0.1:4000'
156156
# GENERIC_OPEN_AI_EMBEDDING_API_KEY='sk-123abc'
157+
# GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS=500
157158

158159
###########################################
159160
######## Vector Database Selection ########

server/models/systemSettings.js

+2
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ const SystemSettings = {
193193
process.env.EMBEDDING_MODEL_MAX_CHUNK_LENGTH,
194194
GenericOpenAiEmbeddingApiKey:
195195
!!process.env.GENERIC_OPEN_AI_EMBEDDING_API_KEY,
196+
GenericOpenAiEmbeddingMaxConcurrentChunks:
197+
process.env.GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS || 500,
196198

197199
// --------------------------------------------------------
198200
// VectorDB Provider Selection Settings & Configs

server/utils/EmbeddingEngines/genericOpenAi/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@ class GenericOpenAiEmbedder {
1414
});
1515
this.model = process.env.EMBEDDING_MODEL_PREF ?? null;
1616

17-
// Limit of how many strings we can process in a single pass to stay with resource or network limits
18-
this.maxConcurrentChunks = 500;
19-
17+
// this.maxConcurrentChunks is delegated to the getter below.
2018
// Refer to your specific model and provider you use this class with to determine a valid maxChunkLength
2119
this.embeddingMaxChunkLength = 8_191;
2220
}
2321

22+
/**
23+
* returns the `GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS` env variable as a number
24+
* or 500 if the env variable is not set or is not a number.
25+
* @returns {number}
26+
*/
27+
get maxConcurrentChunks() {
28+
if (!process.env.GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS)
29+
return 500;
30+
if (
31+
isNaN(Number(process.env.GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS))
32+
)
33+
return 500;
34+
return Number(process.env.GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS);
35+
}
36+
2437
async embedTextInput(textInput) {
2538
const result = await this.embedChunks(
2639
Array.isArray(textInput) ? textInput : [textInput]

server/utils/helpers/updateENV.js

+4
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ const KEY_MAPPING = {
267267
envKey: "GENERIC_OPEN_AI_EMBEDDING_API_KEY",
268268
checks: [],
269269
},
270+
GenericOpenAiEmbeddingMaxConcurrentChunks: {
271+
envKey: "GENERIC_OPEN_AI_EMBEDDING_MAX_CONCURRENT_CHUNKS",
272+
checks: [nonZero],
273+
},
270274

271275
// Vector Database Selection Settings
272276
VectorDB: {

0 commit comments

Comments
 (0)