Claude 3 added

This commit is contained in:
Mayank Aamseek
2024-04-17 13:03:06 +05:30
parent 3b792cb7c1
commit 3cc0518dcb
+51 -14
View File
@@ -214,7 +214,8 @@ if st.session_state.user_info['mail'] in user_list:
# st.write(df)
# st.write(len(contract_list))
# contract_list = [contract for contract in contract_list if contract.rsplit('/',1)[1].replace(' MU','').replace('_MU','').replace('.txt','') in list(field_values['Document_Name'])]
contract_list = [contract for contract in contract_list if contract.rsplit('/',1)[1].replace('.txt','.pdf') in list(field_values['Document_Name'])]
contract_list = [contract.replace('.txt','.pdf') for contract in contract_list]
contract_list = [contract for contract in contract_list if contract.rsplit('/',1)[1] in list(field_values['Document_Name'])]
# st.write(len(contract_list))
if contract_count == 'All':
@@ -247,8 +248,8 @@ if st.session_state.user_info['mail'] in user_list:
with llm_row[0]:
st.write("**Langauge Model**")
with llm_row[1]:
llm_selected = st.selectbox('Langauge Model',('Claude 2', 'Claude Instant', 'Llama 2 Chat 70B'
, 'Titan Text Express'), index=1, label_visibility = "collapsed")
llm_selected = st.selectbox('Langauge Model',('Claude 2', 'Claude 3 - Haiku', 'Claude 3 - Sonnet', 'Claude Instant'
, 'Llama 2 Chat 70B', 'Titan Text Express'), index=3, label_visibility = "collapsed")
st.write("**Prompt**")
if mode == 'Multiple fields':
@@ -276,6 +277,7 @@ if st.session_state.user_info['mail'] in user_list:
# # , column_name+'_PG': 'Original Page Number'}, inplace=True)
field_values.rename(columns={'Document_Name': 'Contract Name'}, inplace=True)
field_values = field_values.drop_duplicates(subset='Contract Name', keep="first").sort_values('Contract Name')
field_values = field_values[field_values['Contract Name'].isin([contract.rsplit('/',1)[1] for contract in contract_list])]
# Setup bedrock
bedrock_runtime = boto3.client(
@@ -313,7 +315,7 @@ if st.session_state.user_info['mail'] in user_list:
for contract in contract_list:
# with open(os.path.join(SOURCE_DIRECTORY, contract[:-4]+'.txt'), 'r') as infile:
# context = infile.read()
data = s3_client.get_object(Bucket=bucket, Key=contract)
data = s3_client.get_object(Bucket=bucket, Key=contract.replace('.pdf','.txt'))
contents = data['Body'].read()
context = contents.decode("utf-8")
# st.write(question_with_schema)
@@ -357,7 +359,7 @@ if st.session_state.user_info['mail'] in user_list:
body=json.dumps(payload)
model_id="meta.llama2-70b-chat-v1"
elif llm_selected in ['Claude Instant', 'Claude 2']:
elif llm_selected in ['Claude Instant', 'Claude 2', 'Claude 3 - Haiku', 'Claude 3 - Sonnet']:
if llm_selected == 'Claude Instant':
context = context[:175000]
@@ -370,18 +372,49 @@ if st.session_state.user_info['mail'] in user_list:
Question: {question_with_schema}
Assistant: Answer in JSON format: {{"""
body = json.dumps(
{"prompt": anthropic.HUMAN_PROMPT + prompt_data + anthropic.AI_PROMPT,
"max_tokens_to_sample": 2048,
"temperature":0.0,
"top_p":1,
"top_k":250,
"stop_sequences":[anthropic.HUMAN_PROMPT]
})
if llm_selected == "Claude 2":
model_id = "anthropic.claude-v2:1"
body = json.dumps(
{"prompt": anthropic.HUMAN_PROMPT + prompt_data + anthropic.AI_PROMPT,
"max_tokens_to_sample": 2048,
"temperature":0.0,
"top_p":1,
"top_k":250,
"stop_sequences":[anthropic.HUMAN_PROMPT]
})
elif llm_selected in ["Claude 3 - Haiku", "Claude 3 - Sonnet"]:
if llm_selected == "Claude 3 - Haiku":
model_id = 'anthropic.claude-3-haiku-20240307-v1:0'
else:
model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
body = json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text":anthropic.HUMAN_PROMPT + prompt_data + anthropic.AI_PROMPT
}
]
}
],
"temperature": 0.0
}
)
else:
model_id = "anthropic.claude-instant-v1"
body = json.dumps(
{"prompt": anthropic.HUMAN_PROMPT + prompt_data + anthropic.AI_PROMPT,
"max_tokens_to_sample": 2048,
"temperature":0.0,
"top_p":1,
"top_k":250,
"stop_sequences":[anthropic.HUMAN_PROMPT]
})
try:
response = bedrock_runtime.invoke_model(
@@ -398,6 +431,8 @@ if st.session_state.user_info['mail'] in user_list:
response_text = response_body['generation']
elif llm_selected in ['Claude Instant', 'Claude 2']:
response_text = response_body['completion']
elif llm_selected in ['Claude 3 - Haiku', 'Claude 3 - Sonnet']:
response_text = response_body['content'][0]['text']
except:
response_text = "failed"
@@ -504,7 +539,7 @@ if st.session_state.user_info['mail'] in user_list:
answer_list = [answer if "None" not in str(answer) else " " for answer in answer_list]
answer_list = [answer if "Not specified in the contract" not in str(answer) else " " for answer in answer_list]
answer_list = [answer if "Not applicable" not in str(answer) else " " for answer in answer_list]
elif llm_selected in ['Claude 2', 'Claude Instant']:
elif llm_selected in ['Claude 2','Claude 3 - Haiku', 'Claude 3 - Sonnet', 'Claude Instant']:
answer_list = [answer if "do not have" not in str(answer) else " " for answer in answer_list]
answer_list = [answer if "do not see" not in str(answer) else " " for answer in answer_list]
answer_list = [answer if "does not specify" not in str(answer) else " " for answer in answer_list]
@@ -566,6 +601,8 @@ if st.session_state.user_info['mail'] in user_list:
field_values_1['Contract Name'] = document_name
field_values_2 = pd.concat([field_values_2, field_values_1], ignore_index = True)
# st.write(df)
# st.write(field_values_2)
df = pd.merge(df, field_values_2, how ='right', on =['Contract Name', 'SF_DB_COL_NAME'])
if mode == 'Multiple fields':
df = df[df['SF_DB_COL_NAME'].isin(list(field_prompt_mapping.keys()))]