Spaces:
Sleeping
Sleeping
Create AWSClaude.py
Browse files- AWSClaude.py +124 -0
AWSClaude.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from firebase_admin import db
|
2 |
+
import json
|
3 |
+
import boto3
|
4 |
+
|
5 |
+
|
6 |
+
class AWSClaude:
|
7 |
+
def __init__(
|
8 |
+
self, llm, env, user_id, thread_id, stream_id, app_type, other_request_params
|
9 |
+
):
|
10 |
+
self.llm = llm
|
11 |
+
self.env = env
|
12 |
+
self.other_request_params = other_request_params
|
13 |
+
|
14 |
+
# RTDB init and params
|
15 |
+
self.user_id = user_id
|
16 |
+
self.thread_id = thread_id
|
17 |
+
self.stream_id = stream_id
|
18 |
+
self.app_type = app_type
|
19 |
+
|
20 |
+
# AWS Bedrock Auth
|
21 |
+
self.session = boto3.Session(
|
22 |
+
aws_access_key_id="AKIA6GBMA2X64YV2BLZJ",
|
23 |
+
aws_secret_access_key="pxEblW/t+kGzCjKm4lAQUk08KRgD4JM+ip4NfJGz",
|
24 |
+
)
|
25 |
+
|
26 |
+
|
27 |
+
if llm == "ClaudeOpus":
|
28 |
+
self.bedrock_runtime = self.session.client(
|
29 |
+
service_name="bedrock-runtime", region_name="us-west-2"
|
30 |
+
)
|
31 |
+
else:
|
32 |
+
self.bedrock_runtime = self.session.client(
|
33 |
+
service_name="bedrock-runtime", region_name="us-east-1"
|
34 |
+
)
|
35 |
+
|
36 |
+
def stream(self, response):
|
37 |
+
"""self.entry_ref.update(
|
38 |
+
{
|
39 |
+
"exec_status": True,
|
40 |
+
}
|
41 |
+
)"""
|
42 |
+
|
43 |
+
full_answer = ""
|
44 |
+
|
45 |
+
for event in response.get("body"):
|
46 |
+
try:
|
47 |
+
chunk = json.loads(event["chunk"]["bytes"])
|
48 |
+
if chunk["type"] == "content_block_delta" and "delta" in chunk.keys():
|
49 |
+
content_text = chunk["delta"]["text"]
|
50 |
+
|
51 |
+
full_answer += content_text
|
52 |
+
|
53 |
+
# print(content_text, end="")
|
54 |
+
|
55 |
+
# stream_ref = self.entry_ref.child("gpt_stream").child(
|
56 |
+
# self.stream_id
|
57 |
+
# ) # get into the child() node and use ".set"
|
58 |
+
# stream_ref.set(full_answer)
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Error occurred with the steam loop {type(e).__name__}, -- {e}")
|
62 |
+
print("chunk ---", chunk)
|
63 |
+
raise e
|
64 |
+
|
65 |
+
# Set 'exec_status' back to false after execution is complete
|
66 |
+
# self.entry_ref.update(
|
67 |
+
# {
|
68 |
+
# "exec_status": False,
|
69 |
+
# }
|
70 |
+
# )
|
71 |
+
|
72 |
+
return full_answer
|
73 |
+
|
74 |
+
def llm_select(self):
|
75 |
+
if self.llm == "ClaudeOpus":
|
76 |
+
return "anthropic.claude-3-opus-20240229-v1:0"
|
77 |
+
elif self.llm == "ClaudeSonnet":
|
78 |
+
return "anthropic.claude-3-sonnet-20240229-v1:0"
|
79 |
+
elif self.llm == "ClaudeHaiku":
|
80 |
+
return "anthropic.claude-3-haiku-20240307-v1:0"
|
81 |
+
|
82 |
+
def call_claude(self):
|
83 |
+
system_prompt = '"You are an expert Equity analyst, please don\'t explicitly mention that you are Claude, or from Anthropic, or an Equity analyst in your response"'
|
84 |
+
messages = self.other_request_params.get("messages", [])
|
85 |
+
combined_messages = " ".join([str(message) for message in messages])
|
86 |
+
prompt = combined_messages
|
87 |
+
max_tokens = self.other_request_params.get("max_tokens", 3500)
|
88 |
+
temperature = self.other_request_params.get("temperature", 0)
|
89 |
+
top_p = self.other_request_params.get("top_p", 1)
|
90 |
+
|
91 |
+
body = json.dumps(
|
92 |
+
{
|
93 |
+
"system": system_prompt,
|
94 |
+
"messages": [
|
95 |
+
{"role": "user", "content": [{"type": "text", "text": prompt}]}
|
96 |
+
],
|
97 |
+
"anthropic_version": "bedrock-2023-05-31",
|
98 |
+
"max_tokens": max_tokens,
|
99 |
+
"temperature": temperature,
|
100 |
+
"top_p": top_p,
|
101 |
+
}
|
102 |
+
)
|
103 |
+
|
104 |
+
llm_id = self.llm_select()
|
105 |
+
print("llm id --- ", llm_id)
|
106 |
+
|
107 |
+
response = self.bedrock_runtime.invoke_model_with_response_stream(
|
108 |
+
body=body,
|
109 |
+
modelId=llm_id,
|
110 |
+
accept="application/json",
|
111 |
+
contentType="application/json",
|
112 |
+
)
|
113 |
+
|
114 |
+
return response
|
115 |
+
|
116 |
+
def invoke(self):
|
117 |
+
response = self.call_claude()
|
118 |
+
output = self.stream(response)
|
119 |
+
return output
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|