Skip to content

Commit 4e94f52

Browse files
authored
Create genai.py
Adding genai.py file in util folder
1 parent 874700d commit 4e94f52

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

AWS/util/genai.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import boto3
2+
import json
3+
import ipywidgets as widgets
4+
from IPython.display import display
5+
6+
# Setup Model
7+
my_session = boto3.session.Session()
8+
my_region = my_session.region_name
9+
bedrock = boto3.client(
10+
service_name='bedrock-runtime',
11+
region_name=my_region,
12+
)
13+
def genai(prompt):
14+
# Define model ID
15+
model_id = "meta.llama3-8b-instruct-v1:0"
16+
17+
# Create request body
18+
body = json.dumps({
19+
"prompt": prompt,
20+
})
21+
22+
# Send request to Bedrock
23+
response = bedrock.invoke_model(
24+
modelId=model_id,
25+
body=body
26+
)
27+
28+
# Process the response
29+
response_body = json.loads(response['body'].read())
30+
generated_text = response_body['generation']
31+
generated_text = generated_text.lstrip('?').strip()
32+
return generated_text
33+
34+
# Create widgets
35+
prompt_input = widgets.Text(description="Prompt:")
36+
submit_button = widgets.Button(description="Submit")
37+
output_area = widgets.Output()
38+
39+
# Define button click event
40+
def on_button_click(b):
41+
with output_area:
42+
output_area.clear_output()
43+
print("Generating response...")
44+
response = genai(prompt_input.value)
45+
print("Response:")
46+
print(response)
47+
48+
# Attach the event to the button
49+
submit_button.on_click(on_button_click)
50+
51+
def display_widgets():
52+
"""
53+
Function to display the widgets in the notebook.
54+
Call this function to show the interactive prompt interface.
55+
"""
56+
display(prompt_input, submit_button, output_area)

0 commit comments

Comments
 (0)