Skip to content

Instantly share code, notes, and snippets.

@si3mshady
Created April 28, 2024 18:17
Show Gist options
  • Save si3mshady/3611b8ac84d80cf055cadba28f72ef90 to your computer and use it in GitHub Desktop.
Save si3mshady/3611b8ac84d80cf055cadba28f72ef90 to your computer and use it in GitHub Desktop.
Flask web application that provides an API endpoint to invoke the Anthropic Claude 2 model using AWS Bedrock.
from flask import Flask, request, jsonify
import json
import boto3
app = Flask(__name__)
brt = boto3.client(service_name='bedrock-runtime')
@app.route('/invoke_claude', methods=['POST'])
def invoke_claude():
try:
prompt = request.get_json()['prompt']
response = invoke_claude_model(prompt)
return jsonify({'response': response})
except Exception as e:
print(f"Error invoking Claude: {str(e)}")
return jsonify({'error': str(e)}), 500
def invoke_claude_model(prompt):
"""
Invokes the Anthropic Claude 2 model to run an inference using the input
provided in the request body.
:param prompt: The prompt that you want Claude to complete.
:return: Inference response from the model.
"""
try:
# The different model providers have individual request and response formats.
# For the format, ranges, and default values for Anthropic Claude, refer to:
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
# Claude requires you to enclose the prompt as follows:
enclosed_prompt = "Human: " + prompt + "\n\nAssistant:"
body = {
"prompt": enclosed_prompt,
"max_tokens_to_sample": 200,
"temperature": 0.5,
"stop_sequences": ["\n\nHuman:"],
}
modelId = 'anthropic.claude-v2'
accept = 'application/json'
contentType = 'application/json'
response = brt.invoke_model(body=json.dumps(body), modelId=modelId, accept=accept, contentType=contentType)
response_body = json.loads(response["body"].read())
completion = response_body["completion"]
return completion
except Exception as e:
print(f"Couldn't invoke Anthropic Claude: {str(e)}")
raise
if __name__ == '__main__':
app.run(debug=True, port=5000)
# curl -X POST \
# http://localhost:5000/invoke_claude \
# -H 'Content-Type: application/json' \
# -d '{"prompt": "What is the capital of France?"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment