Skip to content

Commit 7613bec

Browse files
feat: Added max_tokens and timeout to all LLMs and few embedding adapters (#93)
* Added max_tokens and timeout to all LLMs and few embedding adapters * Updated Open AI to OpenAI
1 parent 88e27d7 commit 7613bec

File tree

41 files changed

+302
-171
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+302
-171
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Unstract Azure Open AI Embedding Adapter
1+
# Unstract Azure OpenAI Embedding Adapter
22

33
This package consists of the functionalities required to adapt with Azure OpenAI Embedding
44
Version supported

src/unstract/sdk/adapters/embedding/azure_open_ai/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "pdm.backend"
66
[project]
77
name = "unstract-azure-open-ai-embedding"
88
version = "0.0.1"
9-
description = "Azure Open AI Embedding"
9+
description = "Azure OpenAI Embedding"
1010
authors = [
1111
{name = "Zipstack Inc.", email = "[email protected]"},
1212
]

src/unstract/sdk/adapters/embedding/azure_open_ai/src/azure_open_ai.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Constants:
1717
AZURE_ENDPOINT = "azure_endpoint"
1818
DEPLOYMENT_NAME = "deployment_name"
1919
API_TYPE = "azure"
20+
TIMEOUT = "timeout"
21+
DEFAULT_TIMEOUT = 60
2022

2123

2224
class AzureOpenAI(EmbeddingAdapter):
@@ -56,6 +58,7 @@ def get_embedding_instance(self) -> BaseEmbedding:
5658
embedding_batch_size = EmbeddingHelper.get_embedding_batch_size(
5759
config=self.config
5860
)
61+
timeout = int(self.config.get(Constants.TIMEOUT, Constants.DEFAULT_TIMEOUT))
5962
embedding: BaseEmbedding = AzureOpenAIEmbedding(
6063
model=str(self.config.get(Constants.MODEL)),
6164
deployment_name=str(self.config.get(Constants.DEPLOYMENT_NAME)),
@@ -64,6 +67,7 @@ def get_embedding_instance(self) -> BaseEmbedding:
6467
azure_endpoint=str(self.config.get(Constants.AZURE_ENDPOINT)),
6568
embed_batch_size=embedding_batch_size,
6669
api_type=Constants.API_TYPE,
70+
timeout=timeout,
6771
)
6872
return embedding
6973
except Exception as e:

src/unstract/sdk/adapters/embedding/azure_open_ai/src/static/json_schema.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "Azure Open AI Embedding",
2+
"title": "Azure OpenAI Embedding",
33
"type": "object",
44
"required": [
55
"adapter_name",
@@ -28,13 +28,6 @@
2828
"default": "",
2929
"description": "Provide the name of the deployment you defined in Azure console"
3030
},
31-
"embed_batch_size": {
32-
"type": "number",
33-
"minimum": 0,
34-
"multipleOf": 1,
35-
"title": "Embedding Batch Size",
36-
"default": 5
37-
},
3831
"api_key": {
3932
"type": "string",
4033
"title": "API Key",
@@ -53,6 +46,21 @@
5346
"default": "",
5447
"format": "uri",
5548
"description": "Provide the Azure endpoint. Example: https://<your-deployment>.openai.azure.com/"
49+
},
50+
"embed_batch_size": {
51+
"type": "number",
52+
"minimum": 0,
53+
"multipleOf": 1,
54+
"title": "Embedding Batch Size",
55+
"default": 5
56+
},
57+
"timeout": {
58+
"type": "number",
59+
"minimum": 0,
60+
"multipleOf": 1,
61+
"title": "Timeout",
62+
"default": 60,
63+
"description": "Timeout for each request in seconds"
5664
}
5765
}
5866
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Unstract Open AI Embeddings
1+
# Unstract OpenAI Embeddings

src/unstract/sdk/adapters/embedding/open_ai/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "pdm.backend"
66
[project]
77
name = "unstract-open-ai-embedding"
88
version = "0.0.1"
9-
description = "Open AI Embedding"
9+
description = "OpenAI Embedding"
1010
authors = [
1111
{name = "Zipstack Inc.", email = "[email protected]"},
1212
]

src/unstract/sdk/adapters/embedding/open_ai/src/open_ai.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Constants:
1515
API_BASE_KEY = "api_base"
1616
ADAPTER_NAME = "adapter_name"
1717
API_TYPE = "openai"
18+
TIMEOUT = "timeout"
19+
DEFAULT_TIMEOUT = 60
1820

1921

2022
class OpenAI(EmbeddingAdapter):
@@ -51,12 +53,14 @@ def get_json_schema() -> str:
5153

5254
def get_embedding_instance(self) -> BaseEmbedding:
5355
try:
56+
timeout = int(self.config.get(Constants.TIMEOUT, Constants.DEFAULT_TIMEOUT))
5457
embedding: BaseEmbedding = OpenAIEmbedding(
5558
api_key=str(self.config.get(Constants.API_KEY)),
5659
api_base=str(
5760
self.config.get(Constants.API_BASE_KEY, Constants.API_BASE_VALUE)
5861
),
5962
api_type=Constants.API_TYPE,
63+
timeout=timeout,
6064
)
6165
return embedding
6266
except Exception as e:

src/unstract/sdk/adapters/embedding/open_ai/src/static/json_schema.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"title": "Open AI Embedding",
2+
"title": "OpenAI Embedding",
33
"type": "object",
44
"required": [
55
"adapter_name",
@@ -18,18 +18,26 @@
1818
"default": "",
1919
"format": "password"
2020
},
21+
"api_base": {
22+
"type": "string",
23+
"title": "API Base",
24+
"format": "uri",
25+
"default": "https://api.openai.com/v1/"
26+
},
2127
"embed_batch_size": {
2228
"type": "number",
2329
"minimum": 0,
2430
"multipleOf": 1,
2531
"title": "Embed Batch Size",
2632
"default": 10
2733
},
28-
"api_base": {
29-
"type": "string",
30-
"title": "API Base",
31-
"format": "uri",
32-
"default": "https://api.openai.com/v1/"
34+
"timeout": {
35+
"type": "number",
36+
"minimum": 0,
37+
"multipleOf": 1,
38+
"title": "Timeout",
39+
"default": 60,
40+
"description": "Timeout in seconds"
3341
}
3442
}
3543
}

src/unstract/sdk/adapters/llm/anthropic/src/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
"description": "Anthropic LLM adapter",
88
"is_active": True,
99
}
10+
11+
__all__ = ["AnthropicLLM"]

src/unstract/sdk/adapters/llm/anthropic/src/anthropic.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import os
22
from typing import Any
33

4+
from anthropic import APIError
45
from llama_index.core.llms import LLM
56
from llama_index.llms.anthropic import Anthropic
7+
from llama_index.llms.anthropic.base import DEFAULT_ANTHROPIC_MAX_TOKENS
68

7-
from unstract.sdk.adapters.exceptions import AdapterError
9+
from unstract.sdk.adapters.exceptions import AdapterError, LLMError
810
from unstract.sdk.adapters.llm.constants import LLMKeys
9-
from unstract.sdk.adapters.llm.helper import LLMHelper
1011
from unstract.sdk.adapters.llm.llm_adapter import LLMAdapter
1112

1213

1314
class Constants:
1415
MODEL = "model"
1516
API_KEY = "api_key"
1617
TIMEOUT = "timeout"
17-
MAX_RETIRES = "max_retries"
18+
MAX_RETRIES = "max_retries"
19+
MAX_TOKENS = "max_tokens"
1820

1921

2022
class AnthropicLLM(LLMAdapter):
@@ -50,6 +52,9 @@ def get_json_schema() -> str:
5052
return schema
5153

5254
def get_llm_instance(self) -> LLM:
55+
max_tokens = int(
56+
self.config.get(Constants.MAX_TOKENS, DEFAULT_ANTHROPIC_MAX_TOKENS)
57+
)
5358
try:
5459
llm: LLM = Anthropic(
5560
model=str(self.config.get(Constants.MODEL)),
@@ -58,15 +63,32 @@ def get_llm_instance(self) -> LLM:
5863
self.config.get(Constants.TIMEOUT, LLMKeys.DEFAULT_TIMEOUT)
5964
),
6065
max_retries=int(
61-
self.config.get(Constants.MAX_RETIRES, LLMKeys.DEFAULT_MAX_RETRIES)
66+
self.config.get(Constants.MAX_RETRIES, LLMKeys.DEFAULT_MAX_RETRIES)
6267
),
6368
temperature=0,
69+
max_tokens=max_tokens,
6470
)
6571
return llm
6672
except Exception as e:
6773
raise AdapterError(str(e))
6874

69-
def test_connection(self) -> bool:
70-
llm = self.get_llm_instance()
71-
test_result: bool = LLMHelper.test_llm_instance(llm=llm)
72-
return test_result
75+
@staticmethod
76+
def parse_llm_err(e: APIError) -> LLMError:
77+
"""Parse the error from Anthropic.
78+
79+
Helps parse errors from Anthropic and wraps with custom exception.
80+
81+
Args:
82+
e (AnthropicAPIError): Exception from Anthropic
83+
84+
Returns:
85+
LLMError: Error to be sent to the user
86+
"""
87+
msg = "Error from Anthropic. "
88+
if hasattr(e, "body"):
89+
if isinstance(e.body, dict) and "error" in e.body:
90+
err = e.body["error"]
91+
msg += err.get("message", e.message)
92+
else:
93+
msg += e.message
94+
return LLMError(msg)

0 commit comments

Comments
 (0)