Skip to content

Instantly share code, notes, and snippets.

@rlunar
Created July 31, 2024 13:58
Show Gist options
  • Select an option

  • Save rlunar/19e596722f15961b88dcebc9a28b0f5b to your computer and use it in GitHub Desktop.

Select an option

Save rlunar/19e596722f15961b88dcebc9a28b0f5b to your computer and use it in GitHub Desktop.
Amazon Bedrock Chatbot with History
import boto3
import json
import pprint
# Create a client for the 'bedrock' service
bedrock = boto3.client(service_name='bedrock', region_name='us-east-1')
# Create a separate client for the 'bedrock-runtime' service
bedrock_runtime = boto3.client(service_name='bedrock-runtime', region_name='us-east-1')
history = []
def get_configuration(prompt: str):
return json.dumps({
"inputText": prompt,
"textGenerationConfig": {
"maxTokenCount": 4096,
"stopSequences": [],
"temperature": 0,
"topP": 1
}
})
def get_history():
return "\n".join(history)
print(
"Bot: Hello! I am a chatbot. I can help you with anything you want to talk about."
)
pp = pprint.PrettyPrinter(depth=4)
while True:
user_input = input("User: ")
history.append(f"User: {user_input}")
prompt = get_history()
# pp.pprint(prompt)
if user_input.lower() == "exit":
break
response = bedrock_runtime.invoke_model(
body=get_configuration(prompt),
modelId="amazon.titan-text-express-v1",
accept="application/json",
contentType="application/json")
response_body = json.loads(response.get('body').read())
outputText = response_body.get('results')[0].get('outputText').strip()
history.append(f"Bot: {outputText}")
pp.pprint(outputText)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment