From 699f8238b3094d51896b22c819be553b6ba805cc Mon Sep 17 00:00:00 2001 From: Jingyi Date: Tue, 21 May 2024 20:10:22 +0000 Subject: [PATCH] add instruction api --- any_parser/base.py | 30 ++++++++++++++++++++++++++++++ any_parser_base.sh | 20 +++++++++++++++++++- extract_parse.sh | 12 +++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/any_parser/base.py b/any_parser/base.py index 6e82e9c..ec1eec2 100644 --- a/any_parser/base.py +++ b/any_parser/base.py @@ -11,6 +11,9 @@ 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: @@ -18,6 +21,7 @@ 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): @@ -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") @@ -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) diff --git a/any_parser_base.sh b/any_parser_base.sh index 4f1eeb7..165846f 100644 --- a/any_parser_base.sh +++ b/any_parser_base.sh @@ -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" @@ -85,4 +86,21 @@ parse() { "$PARSE_URL") result=$(echo "$response" | jq -r '.result') -} \ No newline at end of file +} + +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') +} diff --git a/extract_parse.sh b/extract_parse.sh index ac77d75..30c388c 100644 --- a/extract_parse.sh +++ b/extract_parse.sh @@ -3,7 +3,7 @@ source any_parser_base.sh if [ "$#" -lt 3 ]; then echo "Error: Missing arguments - Usage: $0 " + Usage: $0 " exit 1 fi @@ -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"