Skip to content

Commit

Permalink
Merge pull request #31 from Sdddell/main
Browse files Browse the repository at this point in the history
add instruction api
  • Loading branch information
Cambio ML authored May 22, 2024
2 parents baa7184 + 699f823 commit ceb087b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
30 changes: 30 additions & 0 deletions any_parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
CAMBIO_PARSE_URL = (
"https://qreije6m7l.execute-api.us-west-2.amazonaws.com/v1/cambio_api/parse"
)
COMBIO_INSTRUCT_URL = (
"https://qreije6m7l.execute-api.us-west-2.amazonaws.com/v1/cambio_api/instruction"
)


class AnyParser:
def __init__(self, apiKey) -> None:
self._uploadurl = CAMBIO_UPLOAD_URL
self._extracturl = CAMBIO_EXTRACT_URL
self._parseurl = CAMBIO_PARSE_URL
self._instructurl = COMBIO_INSTRUCT_URL
self._request_header = {"x-api-key": apiKey}

def setAPIKey(self, apiKey):
Expand All @@ -33,6 +37,13 @@ def parse(self, file_path, prompt="", mode="advanced"):
result = self._request_info_extraction(user_id, job_id, s3_key, mode, prompt)
return json.loads(result)["result"]

def instruct(self, file_path, prompt="", mode="advanced"):
user_id, job_id, s3_key = self._request_and_upload_by_apiKey(file_path)
result = self._request_instruction_extraction(
user_id, job_id, s3_key, mode, prompt
)
return json.loads(result)["result"]

def _error_handler(self, response):
if response.status_code == 403:
raise Exception("Invalid API Key")
Expand Down Expand Up @@ -96,3 +107,22 @@ def _request_info_extraction(self, user_id, job_id, s3_key, mode, prompt=""):
return response.text

self._error_handler(response)

def _request_instruction_extraction(self, user_id, job_id, s3_key, mode, prompt=""):
if mode not in ["advanced", "basic"]:
raise ValueError("Invalid mode. Choose either 'advanced' or 'basic'.")
payload = {
"userId": user_id,
"jobId": job_id,
"fileKey": s3_key,
"user_prompt": prompt,
"use_textract": "True" if mode == "advanced" else "False",
}
response = requests.post(
self._instructurl, headers=self._request_header, json=payload
)

if response.status_code == 200:
return response.text

self._error_handler(response)
20 changes: 19 additions & 1 deletion any_parser_base.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
UPLOAD_URL="https://qreije6m7l.execute-api.us-west-2.amazonaws.com/v1/cambio_api/upload"
EXTRACT_URL="https://qreije6m7l.execute-api.us-west-2.amazonaws.com/v1/cambio_api/extract"
PARSE_URL="https://qreije6m7l.execute-api.us-west-2.amazonaws.com/v1/cambio_api/parse"
INSTRUCT_URL="https://qreije6m7l.execute-api.us-west-2.amazonaws.com/v1/cambio_api/instruction"

uid="null"
jid="null"
Expand Down Expand Up @@ -85,4 +86,21 @@ parse() {
"$PARSE_URL")

result=$(echo "$response" | jq -r '.result')
}
}

instruct() {
local payload='{
"userId": "'"$uid"'",
"jobId": "'"$jid"'",
"fileKey": "'"$s3_key"'",
"user_prompt": "'"$prompt"'",
"use_textract": "'"$textract"'"
}'

local response=$(curl -s -X POST \
-H "x-api-key: $apiKey" \
-d "$payload" \
"$INSTRUCT_URL")

result=$(echo "$response" | jq -r '.result')
}
12 changes: 11 additions & 1 deletion extract_parse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source any_parser_base.sh

if [ "$#" -lt 3 ]; then
echo "Error: Missing arguments
Usage: $0 <api_key> <job type: extract | parse> <file path> <prompt for parse (optional, default="")> <parse mode (optional, default=basic): basic | advanced>"
Usage: $0 <api_key> <job type: extract | parse | instruct> <file path> <prompt for parse (optional, default="")> <parse mode (optional, default=basic): basic | advanced>"
exit 1
fi

Expand All @@ -24,6 +24,16 @@ elif [ "$func" == "parse" ]; then
fi
upload
parse
elif [ "$func" == "instruct" ]; then
prompt="$4"
mode="$5"
if [ -z "$mode" ] || [ "$mode" == "" ] || [ "$mode" == "advanced" ]; then
textract="True"
else
textract="False"
fi
upload
instruct
fi

echo "$result"

0 comments on commit ceb087b

Please sign in to comment.