Skip to content

Instantly share code, notes, and snippets.

View DennisTraub's full-sized avatar

Dennis Traub DennisTraub

View GitHub Profile
@DennisTraub
DennisTraub / ReasoningWithConverseStream.java
Last active February 25, 2025 12:20
This example demonstrates how to use the Amazon Nova foundation models with Amazon Bedrock to generate streaming text responses.
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient;
import software.amazon.awssdk.services.bedrockruntime.model.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
/**
User:
I want to create a meta prompt for a large-language model like yourself to help me generate effective purpose-built prompts for specific use-cases. Here's my first draft:
<PROMPT>
You are an experienced prompt engineer with a very good understanding of how to use a Large-Language model like OpenAI GPT or Anthropic Claude. You know how important it is to help the model reduce hallucinations and produce effective output. You are aware of the important techniques and common pitfalls that have been discovered in the practice of prompt-engineering, and will use your own reasoning to anticipate and prevent further challenges and issues. One of your strengths is to help the model understand that the output must not sound like it has been written by an AI. Your prompts will instruct the model to take its time to review and revise its own output before returning it to the user.
</PROMPT>
Your first task is to improve or rethink the prompt to optimize it for its intended purpose. Create five variations, then t
@DennisTraub
DennisTraub / AmazonBedrockWithCSharp.csx
Created May 31, 2024 14:34
No Python? No Problem! Generative AI for the rest of us -- Today's snippet: Meta Llama 3 with C#/.NET on Amazon Bedrock
using Amazon;
using Amazon.BedrockRuntime;
using Amazon.BedrockRuntime.Model;
using System;
// Create a Bedrock Runtime client in the AWS Region of your choice.
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1);
// Specify the model ID. We're using Llama 3 today, but it can be any
// other text-based model on Amazon Bedrock.
@DennisTraub
DennisTraub / invoke_agent.js
Last active March 13, 2024 10:21
Invoke Bedrock Agent with Response stream
export const invokeBedrockAgent = async (prompt, sessionId) => {
const region = "AWS_REGION";
const client = new BedrockAgentRuntimeClient({ region: region });
const agentId = "AGENT_ID";
const agentAliasId = "AGENT_ALIAS_ID";
sessionId = sessionId || crypto.randomUUID().replace(/-/g, "")
try {
@DennisTraub
DennisTraub / claude_3_multimodal.py
Last active May 9, 2025 07:07
Image analysis with Claude 3 Sonnet on Amazon Bedrock
import base64
import boto3
import json
# Initialize the Amazon Bedrock runtime client
client = self.client or boto3.client(service_name="bedrock-runtime", region_name="us-east-1")
# Encode the image using base64
with open("path/to/you/image", "rb") as image_file:
base64_image_data = base64.b64encode(image_file.read()).decode("utf8")
import java.nio.charset.StandardCharsets;
import org.json.JSONObject;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest;
import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse;
public class Main {
import boto3
sqs = boto3.client('sqs')
sns = boto3.client('sns')
# Replace with your SNS topic ARN
sns_topic_arn = 'YOUR_SNS_TOPIC_ARN'
def lambda_handler(event, context):
# Receive a message from the SQS queue
import boto3
# Set up SQS resource with AWS credentials
sqs = boto3.resource(
'sqs',
aws_access_key_id='YOUR_AWS_ACCESS_KEY_ID',
aws_secret_access_key='YOUR_AWS_SECRET_ACCESS_KEY',
region_name='YOUR_AWS_REGION'
)
import json
import random
print('Fulfillment service')
def lambda_handler(event, context):
print('----------')
if 'EventSource' in event['Records'][0] and event['Records'][0]['EventSource'] == 'aws:sns':
print('Processing SNS message...')
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ce:GetCostAndUsage",
"ce:GetCostAndUsageWithResources",
"ce:GetCostForecast"
],