Skip to content

Instantly share code, notes, and snippets.

View JGalego's full-sized avatar
:fishsticks:
dependable.critical-ai.dev

João Galego JGalego

:fishsticks:
dependable.critical-ai.dev
View GitHub Profile
@JGalego
JGalego / agent_smith.py
Created July 16, 2024 11:27
🕶️ ChatGPT-like application for demoing OpenAI-compatible proxies
"""
ChatGPT-like application for demoing OpenAI-compatible proxies.
Adapted from https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps
"""
import os
import streamlit as st
from openai import OpenAI
@JGalego
JGalego / bedrock_raw.py
Last active August 8, 2024 14:48
Amazon Bedrock... let's go raw! 🥩
# pylint: disable=line-too-long
"""
## Amazon Bedrock... let's go raw! 🥩
Your mission (if you choose to accept it) is to create a bedrock.invokeModel request from the ground up.
In a nutshell, we have to prepare the request, SigV4-sign it and then send it to the Bedrock Runtime endpoint.
Sounds fun? Then, let's get started...
@JGalego
JGalego / bedrock_curl.sh
Last active August 7, 2024 21:11
Call the Amazon Bedrock Runtime API using cURL and AWS Signature V4
#!/bin/bash
[[ -n "${AWS_ACCESS_KEY_ID}" ]] ||
{ echo "AWS_ACCESS_KEY_ID is required" >&2; exit 1; }
[[ -n "${AWS_SECRET_ACCESS_KEY}" ]] ||
{ echo "AWS_SECRET_ACCESS_KEY is required" >&2; exit 1; }
[[ -n "${AWS_SESSION_TOKEN}" ]] ||
{ echo "AWS_SESSION_TOKEN is required" >&2; exit 1; }
# The AWS region
@JGalego
JGalego / bedrock_boto3_sigv4.py
Created August 5, 2024 08:13
Call Amazon Bedrock with AWS SigV4 (requests/botocore)
# pylint: disable=invalid-name
"""
Call Amazon Bedrock via Boto3 with AWS SigV4
"""
import os
import logging
import http.client as http_client
@JGalego
JGalego / bedrock_chess.py
Last active August 14, 2024 09:10
Play chess with Amazon Bedrock and Autogen ♟️
# pylint: disable=global-statement,invalid-name
r"""
Play chess with Amazon Bedrock
_:_
'-.-'
() __.'.__
.-:--:-. |_______|
() \____/ \=====/
/\ {====} )___(
@JGalego
JGalego / bag_guardrails.py
Last active August 29, 2024 13:40
Bedrock Access Gateway with Guardrails ✋
# pylint: disable=invalid-name,line-too-long,missing-function-docstring,redefined-outer-name
"""
Demo: Bedrock Access Gateway with Guardrails
Adapted from
https://aws.amazon.com/blogs/aws/guardrails-for-amazon-bedrock-can-now-detect-hallucinations-and-safeguard-apps-built-using-custom-or-third-party-fms/
"""
import os
import boto3
@JGalego
JGalego / modality_gap.py
Last active August 27, 2024 00:38
Exploring the modality gap with Amazon Bedrock
# pylint: disable=redefined-outer-name
"""
Exploring the modality gap with Amazon Bedrock
References:
https://jina.ai/news/the-what-and-why-of-text-image-modality-gap-in-clip-models/
"""
import base64
import json
@JGalego
JGalego / modality_gap_app.py
Last active August 27, 2024 14:19
Dash app to explore the modality gap 🧭
# pylint: disable=redefined-outer-name
r"""
Explore the modality gap with Amazon Bedrock 🧭⛰️
_ . , . .
* / \_ * / \_ _ * * /\'__ *
/ \ / \, (( . _/ / \ *'.
. /\/\ /\/ :' __ \_ ` _^/ ^/ `--.
/ \/ \ _/ \-'\ * /.' ^_ \_ .'\ *
/\ .- `. \/ \ /==~=-=~=-=-;. _/ \ -. `_/ \
@JGalego
JGalego / test_llama_stack.sh
Created August 28, 2024 16:42
Testing Llama Stack 🦙
# 0a. Download, install and activate Miniconda
# https://docs.anaconda.com/miniconda/
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
~/miniconda3/bin/conda init bash
source ~/.bashrc
# 0b. Install Llama Stack
@JGalego
JGalego / titan_love.py
Last active September 17, 2024 13:33
Titan Love 🔱💗
"""
Sends love to Amazon Titan for Embeddings 💖
and gets a bunch of numbers in return 🔢
"""
import json
import boto3
# Initialize Bedrock Runtime client
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime.html