Skip to content

Commit

Permalink
feaat: Updated qdrant client to 1.9.1, bumped sdk to 0.25.0 (#46)
Browse files Browse the repository at this point in the history
Updated qdrant client to 1.9.1, bumped sdk to 0.25.0
  • Loading branch information
chandrasekharan-zipstack authored May 7, 2024
1 parent 7048dbd commit c6f6485
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 404 deletions.
715 changes: 356 additions & 359 deletions pdm.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ dependencies = [
"python-magic~=0.4.27",
"python-dotenv==1.0.0",
# LLM Triad
"unstract-adapters~=0.12.1",
"unstract-adapters~=0.14.0",
"llama-index==0.10.28",
"tiktoken~=0.4.0",
"transformers==4.37.0",
# Error handling, remove after moving it to adapters
"openai~=1.21.2"
]
readme = "README.md"
urls = { Homepage = "https://unstract.com", "Release notes" = "https://github.com/Zipstack/unstract-sdk/releases", Source = "https://github.com/Zipstack/unstract-sdk" }
Expand Down
2 changes: 1 addition & 1 deletion src/unstract/sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.24.1"
__version__ = "0.25.0"


def get_sdk_version():
Expand Down
11 changes: 4 additions & 7 deletions src/unstract/sdk/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from unstract.sdk.adapters import ToolAdapter
from unstract.sdk.constants import LogLevel
from unstract.sdk.exceptions import SdkError
from unstract.sdk.exceptions import SdkError, ToolEmbeddingError
from unstract.sdk.tool.base import BaseTool


Expand Down Expand Up @@ -32,23 +32,20 @@ def get_embedding(self, adapter_instance_id: str) -> BaseEmbedding:
embedding_adapter_id = embedding_config_data.get(Common.ADAPTER_ID)
if embedding_adapter_id not in self.embedding_adapters:
raise SdkError(
f"Embedding adapter not supported : "
f"{embedding_adapter_id}"
f"Embedding adapter not supported : " f"{embedding_adapter_id}"
)

embedding_adapter = self.embedding_adapters[embedding_adapter_id][
Common.METADATA
][Common.ADAPTER]
embedding_metadata = embedding_config_data.get(
Common.ADAPTER_METADATA
)
embedding_metadata = embedding_config_data.get(Common.ADAPTER_METADATA)
embedding_adapter_class = embedding_adapter(embedding_metadata)
return embedding_adapter_class.get_embedding_instance()
except Exception as e:
self.tool.stream_log(
log=f"Error getting embedding: {e}", level=LogLevel.ERROR
)
raise SdkError(f"Error getting embedding instance: {e}")
raise ToolEmbeddingError(f"Error getting embedding instance: {e}")

def get_embedding_length(self, embedding: BaseEmbedding) -> int:
embedding_list = embedding._get_text_embedding(self.__TEST_SNIPPET)
Expand Down
16 changes: 16 additions & 0 deletions src/unstract/sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ def __init__(self, message: str = ""):
if "404" in message:
message = "Index not found. Please check vector db adapter settings."
super().__init__(message)


class ToolLLMError(SdkError):
DEFAULT_MESSAGE = "Error ocurred related to LLM"


class ToolEmbeddingError(SdkError):
DEFAULT_MESSAGE = "Error ocurred related to embedding"


class ToolVectorDBError(SdkError):
DEFAULT_MESSAGE = "Error ocurred related to vector DB"


class RateLimitError(SdkError):
DEFAULT_MESSAGE = "Running into rate limit errors, please try again later"
3 changes: 3 additions & 0 deletions src/unstract/sdk/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ def index_file(
}
)

if not extracted_text:
raise IndexingError("No text available to index")

# Check if chunking is required
documents = []
for item in full_text:
Expand Down
53 changes: 25 additions & 28 deletions src/unstract/sdk/llm.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import logging
import re
import time
from typing import Any, Optional

from llama_index.core.llms import LLM, CompletionResponse
from openai import APIError as OpenAIAPIError
from openai import RateLimitError as OpenAIRateLimitError
from unstract.adapters.constants import Common
from unstract.adapters.llm import adapters
from unstract.adapters.llm.llm_adapter import LLMAdapter

from unstract.sdk.adapters import ToolAdapter
from unstract.sdk.constants import LogLevel
from unstract.sdk.exceptions import SdkError
from unstract.sdk.exceptions import RateLimitError, SdkError, ToolLLMError
from unstract.sdk.tool.base import BaseTool
from unstract.sdk.utils.callback_manager import (
CallbackManager as UNCallbackManager,
)
from unstract.sdk.utils.callback_manager import CallbackManager as UNCallbackManager

logger = logging.getLogger(__name__)


class ToolLLM:
"""Class to handle LLMs for Unstract Tools."""

json_regex = re.compile(r"\{(?:.|\n)*\}")
json_regex = re.compile(r"\[(?:.|\n)*\]|\{(?:.|\n)*\}")

def __init__(self, tool: BaseTool):
"""ToolLLM constructor.
Expand Down Expand Up @@ -62,21 +61,21 @@ def run_completion(
"run_id",
]:
new_kwargs.pop(key, None)
for i in range(retries):
try:
response: CompletionResponse = llm.complete(
prompt, **new_kwargs
)
match = cls.json_regex.search(response.text)
if match:
response.text = match.group(0)
return {"response": response}

except Exception as e:
if i == retries - 1:
raise e
time.sleep(5)
return None

try:
response: CompletionResponse = llm.complete(prompt, **new_kwargs)
match = cls.json_regex.search(response.text)
if match:
response.text = match.group(0)
return {"response": response}
# TODO: Handle for all LLM providers
except OpenAIAPIError as e:
msg = e.message
if hasattr(e, "body") and "message" in e.body:
msg = e.body["message"]
if isinstance(e, OpenAIRateLimitError):
raise RateLimitError(msg)
raise ToolLLMError(msg) from e

def get_llm(self, adapter_instance_id: str) -> LLM:
"""Returns the LLM object for the tool.
Expand All @@ -91,13 +90,11 @@ def get_llm(self, adapter_instance_id: str) -> LLM:
)
llm_adapter_id = llm_config_data.get(Common.ADAPTER_ID)
if llm_adapter_id not in self.llm_adapters:
raise SdkError(
f"LLM adapter not supported : " f"{llm_adapter_id}"
)
raise SdkError(f"LLM adapter not supported : " f"{llm_adapter_id}")

llm_adapter = self.llm_adapters[llm_adapter_id][
Common.METADATA
][Common.ADAPTER]
llm_adapter = self.llm_adapters[llm_adapter_id][Common.METADATA][
Common.ADAPTER
]
llm_metadata = llm_config_data.get(Common.ADAPTER_METADATA)
llm_adapter_class: LLMAdapter = llm_adapter(llm_metadata)
llm_instance: LLM = llm_adapter_class.get_llm_instance()
Expand All @@ -106,7 +103,7 @@ def get_llm(self, adapter_instance_id: str) -> LLM:
self.tool.stream_log(
log=f"Unable to get llm instance: {e}", level=LogLevel.ERROR
)
raise SdkError(f"Error getting llm instance: {e}")
raise ToolLLMError(f"Error getting llm instance: {e}")

def get_max_tokens(self, reserved_for_output: int = 0) -> int:
"""Returns the maximum number of tokens that can be used for the LLM.
Expand Down
12 changes: 4 additions & 8 deletions src/unstract/sdk/vector_db.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import logging
from typing import Union

from llama_index.core.vector_stores.types import (
BasePydanticVectorStore,
VectorStore,
)
from llama_index.core.vector_stores.types import BasePydanticVectorStore, VectorStore
from unstract.adapters.constants import Common
from unstract.adapters.vectordb import adapters
from unstract.adapters.vectordb.constants import VectorDbConstants

from unstract.sdk.adapters import ToolAdapter
from unstract.sdk.constants import LogLevel, ToolEnv
from unstract.sdk.exceptions import SdkError
from unstract.sdk.exceptions import SdkError, ToolVectorDBError
from unstract.sdk.platform import PlatformHelper
from unstract.sdk.tool.base import BaseTool

Expand Down Expand Up @@ -58,8 +55,7 @@ def get_vector_db(
vector_db_adapter_id = vector_db_config.get(Common.ADAPTER_ID)
if vector_db_adapter_id not in self.vector_db_adapters:
raise SdkError(
f"VectorDB adapter not supported : "
f"{vector_db_adapter_id}"
f"VectorDB adapter not supported : " f"{vector_db_adapter_id}"
)

vector_db_adapter = self.vector_db_adapters[vector_db_adapter_id][
Expand All @@ -81,4 +77,4 @@ def get_vector_db(
log=f"Unable to get vector_db {adapter_instance_id}: {e}",
level=LogLevel.ERROR,
)
raise SdkError(f"Error getting vectorDB instance: {e}")
raise ToolVectorDBError(f"Error getting vectorDB instance: {e}")

0 comments on commit c6f6485

Please sign in to comment.