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