Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

Support context-window size (#208)

Gayathric605fa7b873e
diff --git a/backend/adapter_processor/constants.py b/backend/adapter_processor/constants.py
index 50aa8f5b5..196166066 100644
--- a/backend/adapter_processor/constants.py
+++ b/backend/adapter_processor/constants.py
@@ -24,3 +24,5 @@ class AdapterKeys:
"Configuration with this ID already exists. "
"Please try with a different ID"
)
+ ADAPTER_CREATED_BY = "created_by_email"
+ ADAPTER_CONTEXT_WINDOW_SIZE = "context_window_size"
diff --git a/backend/adapter_processor/serializers.py b/backend/adapter_processor/serializers.py
index cb50ee86e..162119815 100644
--- a/backend/adapter_processor/serializers.py
+++ b/backend/adapter_processor/serializers.py
@@ -8,7 +8,9 @@ from cryptography.fernet import Fernet
from django.conf import settings
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
+from unstract.adapters.adapterkit import Adapterkit
from unstract.adapters.constants import Common as common
+from unstract.adapters.llm.llm_adapter import LLMAdapter
from backend.constants import FieldLengthConstants as FLC
from backend.serializers import AuditSerializer
@@ -70,11 +72,24 @@ class AdapterInstanceSerializer(BaseAdapterSerializer):
adapter_metadata = json.loads(
f.decrypt(bytes(instance.adapter_metadata_b).decode("utf-8"))
)
+ # Get the adapter_instance
+ adapter_class = Adapterkit().get_adapter_class_by_adapter_id(
+ instance.adapter_id
+ )
+ adapter_instance = adapter_class(adapter_metadata)
+ # If adapter_instance is a LLM send
+ # additional parameter of context_window
+ if isinstance(adapter_instance, LLMAdapter):
+ adapter_metadata[AdapterKeys.ADAPTER_CONTEXT_WINDOW_SIZE] = (
+ adapter_instance.get_context_window_size()
+ )
+
rep[AdapterKeys.ADAPTER_METADATA] = adapter_metadata
+
rep[common.ICON] = AdapterProcessor.get_adapter_data_with_key(
instance.adapter_id, common.ICON
)
- rep["created_by_email"] = instance.created_by.email
+ rep[AdapterKeys.ADAPTER_CREATED_BY] = instance.created_by.email
return rep
diff --git a/frontend/src/components/custom-tools/add-llm-profile/AddLlmProfile.jsx b/frontend/src/components/custom-tools/add-llm-profile/AddLlmProfile.jsx
index bb2a15acd..72f9612ed 100644
--- a/frontend/src/components/custom-tools/add-llm-profile/AddLlmProfile.jsx
+++ b/frontend/src/components/custom-tools/add-llm-profile/AddLlmProfile.jsx
@@ -147,6 +147,9 @@ function AddLlmProfile({
}
}, [formDetails]);
+ const [tokenSize, setTokenSize] = useState(0);
+ const [maxTokenSize, setMaxTokenSize] = useState(0);
+
const validateEmptyOrWhitespace = (_, value) => {
if (value && value.trim() === "") {
return Promise.reject(new Error("Please enter a non-whitespace value"));
@@ -346,6 +349,44 @@ function AddLlmProfile({
return <CaretRightOutlined rotate={isActive ? 90 : 0} />;
};
+ const handleLlmChangeForTokens = async (value) => {
+ if (!value) {
+ return null;
+ }
+ const requestOptions = {
+ method: "GET",
+ url: `/api/v1/unstract/mock_org/adapter/` + value,
+ };
+
+ axiosPrivate(requestOptions)
+ .then((res) => {
+ const data = res?.data;
+ const contextWindowSize = data.adapter_metadata.context_window_size;
+ const chunkSize = form.getFieldValue("chunk_size");
+ setTokenSize(chunkSize > 0 ? calcTokenSize(chunkSize) : 0);
+ setMaxTokenSize(contextWindowSize);
+ })
+ .catch((err) => {
+ setAlertDetails(
+ handleException(
+ err,
+ "Failed to get the dropdown list for LLM Adaptors"
+ )
+ );
+ });
+ };
+
+ const handleChunkSizeChange = async (event) => {
+ const value = event.target.value;
+ const tokenSize = calcTokenSize(value);
+ setTokenSize(tokenSize);
+ };
+
+ function calcTokenSize(chunkSize) {
+ const tokenSize = (chunkSize / 4 / 1024).toFixed(1);
+ return tokenSize;
+ }
+
return (
<div className="settings-body-pad-top">
<Form
@@ -398,13 +439,24 @@ function AddLlmProfile({
}
help={getBackendErrorDetail("llm", backendErrors)}
>
- <Select options={llmItems} />
+ <Select
+ options={llmItems}
+ onSelect={handleLlmChangeForTokens}
+ />
</Form.Item>
</Col>
<Col span={1} />
<Col span={8}>
<Form.Item
- label="Chunk Size"
+ label={
+ <>
+ Chunk Size
+ <Typography.Text type="secondary">
+ {" "}
+ (Set to 0 if documents are small)
+ </Typography.Text>
+ </>
+ }
name="chunk_size"
rules={[
{
@@ -418,8 +470,9 @@ function AddLlmProfile({
: ""
}
help={getBackendErrorDetail("chunk_size", backendErrors)}
+ extra={`~= ${tokenSize}k tokens, Max: ${maxTokenSize}`}
>
- <Input type="number" />
+ <Input type="number" onChange={handleChunkSizeChange} />
</Form.Item>
</Col>
</Row>