2024-10-28 23:39:36 +00:00
import json
import boto3
import pandas as pd
import numpy as np
from datetime import datetime
import os
import anthropic
import re
import logging
from urllib . parse import unquote_plus
from botocore . exceptions import ClientError
from botocore . config import Config
import traceback
import time
import sys
2024-12-05 22:46:41 +00:00
import warnings
from enums . delimiters import Delimiter
2024-12-30 21:41:21 +00:00
2024-10-28 23:39:36 +00:00
import claude_funcs
import dict_operations
import config
2024-11-13 17:08:19 +00:00
import utils
import prompts
2024-12-05 22:46:41 +00:00
from regex_patterns import PIPE_PATTERN , BACKTICK_PATTERN , TRIPLE_BACKTICK_PATTERN
2024-10-28 23:39:36 +00:00
def get_ac_answer ( prompt , filename , fields ) :
response_text = claude_funcs . invoke_claude (
prompt , config . MODEL_ID_CLAUDE35_SONNET , filename , 8192
)
response_text = response_text . replace ( " _DATE " , " _DT " )
# response_dict = dict_operations.secondary_string_to_dict(response_text, filename)
response_dict = json_parsing_search ( response_text , fields )
return response_dict
2024-12-30 21:41:21 +00:00
def extract_text_from_delimiters (
raw_output : str , delimiter : Delimiter , match_index : int = - 1
) - > str :
2024-12-05 22:46:41 +00:00
"""
Extracts a match enclosed in specified delimiters from the raw output.
This function searches for text enclosed by a specified delimiter in the given raw output string.
It returns the match specified by the `which_match` index. If no match is found, it returns " N/A " .
- raw_output (str): The raw output string to search within.
- which_match (int, optional): The index of the match to return. Defaults to -1, which returns the last match. If the index is out of range, the last match is returned.
- str: The extracted text or " N/A " if no match is found.
2024-12-30 21:41:21 +00:00
2024-12-05 22:46:41 +00:00
Raises:
- TypeError: If `raw_output` is not a string or `delimiter` is not a Delimiter enum value.
- ValueError: If an unsupported delimiter is provided.
Returns:
- str: The extracted answer or " N/A " if no match is found.
"""
if not isinstance ( raw_output , str ) :
2024-12-30 21:41:21 +00:00
raise TypeError (
" Expected a string for raw_output, got {0} . " . format (
type ( raw_output ) . __name__
)
)
2024-12-05 22:46:41 +00:00
if not isinstance ( delimiter , Delimiter ) :
2024-12-30 21:41:21 +00:00
raise TypeError (
" Expected a Delimiter enum value, got {0} . " . format ( type ( delimiter ) . __name__ )
)
2024-12-05 22:46:41 +00:00
# Define a pattern based on the delimiter type
if delimiter == Delimiter . PIPE :
pattern = PIPE_PATTERN
elif delimiter == Delimiter . BACKTICK :
pattern = BACKTICK_PATTERN
elif delimiter == Delimiter . TRIPLE_BACKTICK :
pattern = TRIPLE_BACKTICK_PATTERN
else :
raise ValueError ( " Unsupported delimiter. Use one of the Delimiter enum values. " )
2024-12-30 21:41:21 +00:00
2024-12-05 22:46:41 +00:00
# Find all matches based on the pattern
matches = re . findall ( pattern = pattern , string = raw_output )
2024-12-30 21:41:21 +00:00
2024-12-05 22:46:41 +00:00
if len ( matches ) == 0 :
return " N/A "
2024-12-30 21:41:21 +00:00
2024-12-05 22:46:41 +00:00
# Adjust for negative indices
if match_index < 0 :
match_index + = len ( matches )
2024-12-30 21:41:21 +00:00
2024-12-05 22:46:41 +00:00
if not 0 < = match_index < len ( matches ) :
warnings . warn ( " Index out of range. Returning the last match by default. " )
match_index = - 1
2024-12-30 21:41:21 +00:00
2024-12-05 22:46:41 +00:00
return matches [ match_index ]
2024-10-28 23:39:36 +00:00
def json_parsing_search ( response_text , field_list ) :
try :
if response_text . split ( " { " , 1 ) [ 1 ] . strip ( ) [ 0 ] == ' " ' :
response_text = " { " + response_text . split ( " { " , 1 ) [ 1 ]
else :
response_text = " { " + response_text
except :
response_text = " { " + response_text
if len ( response_text . split ( " } " , 1 ) ) > 1 :
if response_text . rsplit ( " } " , 1 ) [ 0 ] . strip ( ) [ - 1 ] == ' " ' :
response_text = response_text . rsplit ( " } " , 1 ) [ 0 ] + " } "
elif response_text . rsplit ( " } " , 1 ) [ 0 ] . strip ( ) [ - 1 ] == " } " :
response_text = response_text . rsplit ( " } " , 1 ) [ 0 ]
else :
response_text = response_text . rstrip ( " , " ) + " } "
field_l = [ ]
answer_l = [ ]
position_dict = { }
for f in field_list :
location = response_text . find ( ' " ' + f + ' " ' )
if location != - 1 :
position_dict [ location ] = f
field_list = list ( dict ( sorted ( position_dict . items ( ) ) ) . values ( ) )
for f in field_list :
if f in response_text :
field_l . append ( f )
value = response_text . split ( ' " ' + f + ' " ' ) [ 0 ]
response_text = response_text . split ( ' " ' + f + ' " ' ) [ 1 ]
if f != field_list [ 0 ] and f != field_list [ - 1 ] :
answer_l . append (
value . strip ( " \n " )
. strip ( ' " ' )
. strip ( " : " )
. strip ( " " )
. strip ( " \n " )
. strip ( " " )
. strip ( " , " )
. strip ( ' " ' )
)
elif f == field_list [ - 1 ] :
answer_l . append (
value . strip ( " \n " )
. strip ( ' " ' )
. strip ( " : " )
. strip ( " " )
. strip ( " \n " )
. strip ( " " )
. strip ( " , " )
. strip ( ' " ' )
)
answer_l . append (
response_text . strip ( " \n " )
. strip ( ' " ' )
. strip ( " : " )
. strip ( " " )
. strip ( " } " )
. strip ( " \n " )
. strip ( " " )
. strip ( ' " ' )
)
return dict ( zip ( field_l , answer_l ) )