Skip to content

Commit

Permalink
feat: Updated SDK and adapters, prompt service error handling improvm…
Browse files Browse the repository at this point in the history
…ents (#320)

* Updated SDK and adapters, prompt service error handling improvments

* Updated tool registry for SDK 0.25.0
  • Loading branch information
chandrasekharan-zipstack committed May 7, 2024
1 parent a578389 commit 213b5c3
Show file tree
Hide file tree
Showing 18 changed files with 726 additions and 675 deletions.
405 changes: 203 additions & 202 deletions backend/pdm.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ dependencies = [
"python-socketio==5.9.0", # For log_events
"social-auth-app-django==5.3.0", # For OAuth
"social-auth-core==4.4.2", # For OAuth
"unstract-sdk~=0.24.0",
"unstract-adapters~=0.12.1",
"unstract-sdk~=0.25.0",
"unstract-adapters~=0.14.0",
# ! IMPORTANT!
# Indirect local dependencies usually need to be added in their own projects
# as: https://pdm-project.org/latest/usage/dependency/#local-dependencies.
Expand Down
4 changes: 2 additions & 2 deletions backend/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ PROMPT_PORT=3003
PROMPT_STUDIO_FILE_PATH=/app/prompt-studio-data

# Structure Tool
STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.18"
STRUCTURE_TOOL_IMAGE_URL="docker:unstract/tool-structure:0.0.19"
STRUCTURE_TOOL_IMAGE_NAME="unstract/tool-structure"
STRUCTURE_TOOL_IMAGE_TAG="0.0.18"
STRUCTURE_TOOL_IMAGE_TAG="0.0.19"

# Feature Flags
EVALUATION_SERVER_IP=localhost
Expand Down
405 changes: 203 additions & 202 deletions pdm.lock

Large diffs are not rendered by default.

493 changes: 247 additions & 246 deletions prompt-service/pdm.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion prompt-service/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies = [
"flask~=3.0",
"llama-index==0.10.28",
"python-dotenv==1.0.0",
"unstract-sdk~=0.24.0",
"unstract-sdk~=0.25.0",
"redis>=5.0.3",
"unstract-core @ file:///${PROJECT_ROOT}/../unstract/core",
]
Expand Down
1 change: 1 addition & 0 deletions prompt-service/src/unstract/prompt_service/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ class RunLevel(Enum):

RUN = "RUN"
EVAL = "EVAL"
CHALLENGE = "CHALLENGE"
11 changes: 11 additions & 0 deletions prompt-service/src/unstract/prompt_service/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ErrorResponse:

class APIError(HTTPException):
code = 500
message = DEFAULT_ERR_MESSAGE

def __init__(
self,
Expand All @@ -42,3 +43,13 @@ def to_dict(self):

def __str__(self):
return str(self.message)


class NoPayloadError(APIError):
code = 400
message = "Bad Request / No payload"


class RateLimitError(APIError):
code = 429
message = "Running into rate limit errors, please try again later"
46 changes: 41 additions & 5 deletions prompt-service/src/unstract/prompt_service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
from unstract.prompt_service.authentication_middleware import AuthenticationMiddleware
from unstract.prompt_service.constants import PromptServiceContants as PSKeys
from unstract.prompt_service.constants import RunLevel
from unstract.prompt_service.exceptions import APIError, ErrorResponse
from unstract.prompt_service.exceptions import (
APIError,
ErrorResponse,
NoPayloadError,
RateLimitError,
)
from unstract.prompt_service.helper import EnvLoader, plugin_loader
from unstract.prompt_service.prompt_ide_base_tool import PromptServiceBaseTool
from unstract.sdk.constants import LogLevel
from unstract.sdk.embedding import ToolEmbedding
from unstract.sdk.exceptions import RateLimitError as SdkRateLimitError
from unstract.sdk.exceptions import SdkError
from unstract.sdk.index import ToolIndex
from unstract.sdk.llm import ToolLLM
Expand Down Expand Up @@ -159,17 +165,15 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
@app.route(
"/answer-prompt",
endpoint="answer_prompt",
methods=["POST", "GET", "DELETE"],
methods=["POST"],
)
@authentication_middleware
def prompt_processor() -> Any:
result: dict[str, Any] = {}
platform_key = AuthenticationMiddleware.get_token_from_auth_header(request)
if request.method == "POST":
payload: dict[Any, Any] = request.json
if not payload:
result["error"] = "Bad Request / No payload"
return result, 400
raise NoPayloadError()
outputs = payload.get(PSKeys.OUTPUTS)
tool_id: str = payload.get(PSKeys.TOOL_ID, "")
file_hash = payload.get(PSKeys.FILE_HASH)
Expand Down Expand Up @@ -302,6 +306,14 @@ def prompt_processor() -> Any:
msg,
)
raise APIError(message=msg)
# TODO: Use vectorDB name when available
_publish_log(
log_events_id,
{"tool_id": tool_id, "prompt_key": prompt_name, "doc_name": doc_name},
LogLevel.DEBUG,
RunLevel.RUN,
"Fetched context from vector DB",
)

assertion_failed = False
answer = "yes"
Expand Down Expand Up @@ -522,6 +534,17 @@ def prompt_processor() -> Any:
challenge_plugin: dict[str, Any] = plugins.get("challenge", {})
try:
if challenge_plugin:
_publish_log(
log_events_id,
{
"tool_id": tool_id,
"prompt_key": prompt_name,
"doc_name": doc_name,
},
LogLevel.INFO,
RunLevel.CHALLENGE,
"Challenging response",
)
tool_settings: dict[str, Any] = {
PSKeys.PREAMBLE: output[PSKeys.PREAMBLE],
PSKeys.POSTAMBLE: output[PSKeys.POSTAMBLE],
Expand Down Expand Up @@ -549,6 +572,17 @@ def prompt_processor() -> Any:
app.logger.error(
"Failed to challenge prompt %s: %s", output["name"], str(e)
)
_publish_log(
log_events_id,
{
"tool_id": tool_id,
"prompt_key": prompt_name,
"doc_name": doc_name,
},
LogLevel.ERROR,
RunLevel.CHALLENGE,
"Error while challenging response",
)

#
# Evaluate the prompt.
Expand Down Expand Up @@ -743,6 +777,8 @@ def run_completion(
answer: str = completion[PSKeys.RESPONSE].text
return answer
# TODO: Catch and handle specific exception here
except SdkRateLimitError as e:
raise RateLimitError(f"Rate limit error. {str(e)}") from e
except Exception as e:
app.logger.info(f"Error fetching response for prompt: {e}.")
# TODO: Publish this error as a FE update
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ hook-check-django-migrations = [
"psycopg2-binary==2.9.9",
"python-dotenv==1.0.0",
"python-magic==0.4.27",
"unstract-sdk~=0.24.0",
"unstract-adapters~=0.12.1",
"unstract-sdk~=0.25.0",
"unstract-adapters~=0.14.0",
"-e unstract-connectors @ file:///${PROJECT_ROOT}/unstract/connectors",
"-e unstract-core @ file:///${PROJECT_ROOT}/unstract/core",
"-e unstract-flags @ file:///${PROJECT_ROOT}/unstract/flags",
Expand Down
2 changes: 1 addition & 1 deletion tools/classifier/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Add your dependencies here

# Required for all unstract tools
unstract-sdk~=0.24.0
unstract-sdk~=0.25.0
2 changes: 1 addition & 1 deletion tools/classifier/src/config/properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"schemaVersion": "0.0.1",
"displayName": "File Classifier",
"functionName": "classify",
"toolVersion": "0.0.15",
"toolVersion": "0.0.16",
"description": "Classifies a file into a bin based on its contents",
"input": {
"description": "File to be classified"
Expand Down
2 changes: 1 addition & 1 deletion tools/structure/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Add your dependencies here

# Required for all unstract tools
unstract-sdk~=0.24.0
unstract-sdk~=0.25.0
2 changes: 1 addition & 1 deletion tools/structure/src/config/properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"schemaVersion": "0.0.1",
"displayName": "Structure Tool",
"functionName": "structure_tool",
"toolVersion": "0.0.18",
"toolVersion": "0.0.19",
"description": "This is a template tool which can answer set of input prompts designed in the Prompt Studio",
"input": {
"description": "File that needs to be indexed and parsed for answers"
Expand Down
2 changes: 1 addition & 1 deletion tools/text_extractor/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Add your dependencies here

# Required for all unstract tools
unstract-sdk~=0.24.0
unstract-sdk~=0.25.0
2 changes: 1 addition & 1 deletion tools/text_extractor/src/config/properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"schemaVersion": "0.0.1",
"displayName": "Text Extractor",
"functionName": "text_extractor",
"toolVersion": "0.0.14",
"toolVersion": "0.0.15",
"description": "The Text Extractor is a powerful tool designed to convert documents to its text form or Extract texts from documents",
"input": {
"description": "Document"
Expand Down
2 changes: 1 addition & 1 deletion unstract/tool-registry/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"docker~=6.1.3",
"jsonschema~=4.18.2",
"PyYAML~=6.0.1",
"unstract-adapters~=0.12.1",
"unstract-adapters~=0.14.0",
# ! IMPORTANT!
# Local dependencies usually need to be added as:
# https://pdm-project.org/latest/usage/dependency/#local-dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"schemaVersion": "0.0.1",
"displayName": "File Classifier",
"functionName": "classify",
"toolVersion": "0.0.15",
"toolVersion": "0.0.16",
"description": "Classifies a file into a bin based on its contents",
"input": {
"description": "File to be classified"
Expand Down Expand Up @@ -139,17 +139,17 @@
}
},
"icon": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<svg\n enable-background=\"new 0 0 20 20\"\n height=\"48\"\n viewBox=\"0 0 20 20\"\n width=\"48\"\n fill=\"#000000\"\n version=\"1.1\"\n id=\"svg8109\"\n sodipodi:docname=\"folder_copy_black_48dp.svg\"\n xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n xmlns=\"http://www.w3.org/2000/svg\"\n xmlns:svg=\"http://www.w3.org/2000/svg\">\n <defs\n id=\"defs8113\" />\n <sodipodi:namedview\n id=\"namedview8111\"\n pagecolor=\"#ffffff\"\n bordercolor=\"#000000\"\n borderopacity=\"0.25\"\n inkscape:showpageshadow=\"2\"\n inkscape:pageopacity=\"0.0\"\n inkscape:pagecheckerboard=\"0\"\n inkscape:deskcolor=\"#d1d1d1\"\n showgrid=\"false\" />\n <g\n id=\"g8099\">\n <rect\n fill=\"none\"\n height=\"20\"\n width=\"20\"\n x=\"0\"\n id=\"rect8097\"\n y=\"0\" />\n </g>\n <g\n id=\"g8107\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <g\n id=\"g8105\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <path\n d=\"M 2.5,5 H 1 V 15.5 C 1,16.33 1.67,17 2.5,17 H 15.68 V 15.5 H 2.5 Z\"\n id=\"path8101\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n <path\n d=\"M 16.5,4 H 11 L 9,2 H 5.5 C 4.67,2 4,2.67 4,3.5 v 9 C 4,13.33 4.67,14 5.5,14 h 11 c 0.83,0 1.5,-0.67 1.5,-1.5 v -7 C 18,4.67 17.33,4 16.5,4 Z m 0,8.5 h -11 v -9 h 2.88 l 2,2 h 6.12 z\"\n id=\"path8103\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n </g>\n </g>\n</svg>\n",
"image_url": "docker:unstract/tool-classifier:0.0.15",
"image_url": "docker:unstract/tool-classifier:0.0.16",
"image_name": "unstract/tool-classifier",
"image_tag": "0.0.15"
"image_tag": "0.0.16"
},
"text_extractor": {
"tool_uid": "text_extractor",
"properties": {
"schemaVersion": "0.0.1",
"displayName": "Text Extractor",
"functionName": "text_extractor",
"toolVersion": "0.0.14",
"toolVersion": "0.0.15",
"description": "The Text Extractor is a powerful tool designed to convert documents to its text form or Extract texts from documents",
"input": {
"description": "Document"
Expand Down Expand Up @@ -224,8 +224,8 @@
}
},
"icon": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<svg\n enable-background=\"new 0 0 20 20\"\n height=\"48\"\n viewBox=\"0 0 20 20\"\n width=\"48\"\n fill=\"#000000\"\n version=\"1.1\"\n id=\"svg8109\"\n sodipodi:docname=\"folder_copy_black_48dp.svg\"\n xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"\n xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\"\n xmlns=\"http://www.w3.org/2000/svg\"\n xmlns:svg=\"http://www.w3.org/2000/svg\">\n <defs\n id=\"defs8113\" />\n <sodipodi:namedview\n id=\"namedview8111\"\n pagecolor=\"#ffffff\"\n bordercolor=\"#000000\"\n borderopacity=\"0.25\"\n inkscape:showpageshadow=\"2\"\n inkscape:pageopacity=\"0.0\"\n inkscape:pagecheckerboard=\"0\"\n inkscape:deskcolor=\"#d1d1d1\"\n showgrid=\"false\" />\n <g\n id=\"g8099\">\n <rect\n fill=\"none\"\n height=\"20\"\n width=\"20\"\n x=\"0\"\n id=\"rect8097\"\n y=\"0\" />\n </g>\n <g\n id=\"g8107\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <g\n id=\"g8105\"\n style=\"fill:#ff4d6d;fill-opacity:1\">\n <path\n d=\"M 2.5,5 H 1 V 15.5 C 1,16.33 1.67,17 2.5,17 H 15.68 V 15.5 H 2.5 Z\"\n id=\"path8101\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n <path\n d=\"M 16.5,4 H 11 L 9,2 H 5.5 C 4.67,2 4,2.67 4,3.5 v 9 C 4,13.33 4.67,14 5.5,14 h 11 c 0.83,0 1.5,-0.67 1.5,-1.5 v -7 C 18,4.67 17.33,4 16.5,4 Z m 0,8.5 h -11 v -9 h 2.88 l 2,2 h 6.12 z\"\n id=\"path8103\"\n style=\"fill:#ff4d6d;fill-opacity:1\" />\n </g>\n </g>\n</svg>\n",
"image_url": "docker:unstract/tool-text-extractor:0.0.14",
"image_url": "docker:unstract/tool-text-extractor:0.0.15",
"image_name": "unstract/tool-text-extractor",
"image_tag": "0.0.14"
"image_tag": "0.0.15"
}
}

0 comments on commit 213b5c3

Please sign in to comment.