Zum Hauptinhalt springen

Bedrock (boto3) SDK

Pass-through Endpunkte für Bedrock - rufen Sie den anbieterspezifischen Endpunkt im nativen Format auf (keine Übersetzung).

FeatureUnterstütztNotizen
KostenverfolgungSagen Sie uns Bescheid, wenn Sie dies benötigen
Protokollierungfunktioniert über alle Integrationen hinweg
EndbenutzerverfolgungSagen Sie uns Bescheid, wenn Sie dies benötigen
Streaming

Ersetzen Sie einfach https://bedrock-runtime.{aws_region_name}.amazonaws.com durch LITELLM_PROXY_BASE_URL/bedrock 🚀

Beispielverwendung

curl -X POST 'http://0.0.0.0:4000/bedrock/model/cohere.command-r-v1:0/converse' \
-H 'Authorization: Bearer anything' \
-H 'Content-Type: application/json' \
-d '{
"messages": [
{"role": "user",
"content": [{"text": "Hello"}]
}
]
}'

Unterstützt ALLE Bedrock Endpunkte (einschließlich Streaming).

Alle Bedrock Endpunkte anzeigen

Schnellstart

Rufen wir den Bedrock /converse Endpunkt auf

  1. AWS-Schlüssel zur Umgebung hinzufügen
export AWS_ACCESS_KEY_ID=""  # Access key
export AWS_SECRET_ACCESS_KEY="" # Secret access key
export AWS_REGION_NAME="" # us-east-1, us-east-2, us-west-1, us-west-2
  1. LiteLLM Proxy starten
litellm

# RUNNING on http://0.0.0.0:4000
  1. Testen Sie es!

Rufen wir den Bedrock Converse Endpunkt auf

curl -X POST 'http://0.0.0.0:4000/bedrock/model/cohere.command-r-v1:0/converse' \
-H 'Authorization: Bearer anything' \
-H 'Content-Type: application/json' \
-d '{
"messages": [
{"role": "user",
"content": [{"text": "Hello"}]
}
]
}'

Beispiele

Alles nach http://0.0.0.0:4000/bedrock wird als anbieterspezifische Route behandelt und entsprechend verarbeitet.

Wesentliche Änderungen

Ursprünglicher EndpunktErsetzen Sie durch
https://bedrock-runtime.{aws_region_name}.amazonaws.comhttp://0.0.0.0:4000/bedrock (LITELLM_PROXY_BASE_URL="http://0.0.0.0:4000")
AWS4-HMAC-SHA256..Bearer anything (verwenden Sie Bearer LITELLM_VIRTUAL_KEY, wenn virtuelle Schlüssel am Proxy eingerichtet sind)

Beispiel 1: Converse API

LiteLLM Proxy Aufruf

curl -X POST 'http://0.0.0.0:4000/bedrock/model/cohere.command-r-v1:0/converse' \
-H 'Authorization: Bearer sk-anything' \
-H 'Content-Type: application/json' \
-d '{
"messages": [
{"role": "user",
"content": [{"text": "Hello"}]
}
]
}'

Direkter Bedrock API-Aufruf

curl -X POST 'https://bedrock-runtime.us-west-2.amazonaws.com/model/cohere.command-r-v1:0/converse' \
-H 'Authorization: AWS4-HMAC-SHA256..' \
-H 'Content-Type: application/json' \
-d '{
"messages": [
{"role": "user",
"content": [{"text": "Hello"}]
}
]
}'

Beispiel 2: Guardrail anwenden

LiteLLM Proxy Aufruf

curl "http://0.0.0.0:4000/bedrock/guardrail/guardrailIdentifier/version/guardrailVersion/apply" \
-H 'Authorization: Bearer sk-anything' \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{"text": {"text": "Hello world"}}],
"source": "INPUT"
}'

Direkter Bedrock API-Aufruf

curl "https://bedrock-runtime.us-west-2.amazonaws.com/guardrail/guardrailIdentifier/version/guardrailVersion/apply" \
-H 'Authorization: AWS4-HMAC-SHA256..' \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{"text": {"text": "Hello world"}}],
"source": "INPUT"
}'

Beispiel 3: Wissensbasis abfragen

curl -X POST "http://0.0.0.0:4000/bedrock/knowledgebases/{knowledgeBaseId}/retrieve" \
-H 'Authorization: Bearer sk-anything' \
-H 'Content-Type: application/json' \
-d '{
"nextToken": "string",
"retrievalConfiguration": {
"vectorSearchConfiguration": {
"filter": { ... },
"numberOfResults": number,
"overrideSearchType": "string"
}
},
"retrievalQuery": {
"text": "string"
}
}'

Direkter Bedrock API-Aufruf

curl -X POST "https://bedrock-agent-runtime.us-west-2.amazonaws.com/knowledgebases/{knowledgeBaseId}/retrieve" \
-H 'Authorization: AWS4-HMAC-SHA256..' \
-H 'Content-Type: application/json' \
-d '{
"nextToken": "string",
"retrievalConfiguration": {
"vectorSearchConfiguration": {
"filter": { ... },
"numberOfResults": number,
"overrideSearchType": "string"
}
},
"retrievalQuery": {
"text": "string"
}
}'

Fortgeschritten – Verwendung mit virtuellen Schlüsseln

Voraussetzungen

Verwenden Sie dies, um zu vermeiden, Entwicklern die rohen AWS-Schlüssel zu geben, ihnen aber dennoch die Verwendung von AWS Bedrock-Endpunkten zu ermöglichen.

Verwendung

  1. Umgebung einrichten
export DATABASE_URL=""
export LITELLM_MASTER_KEY=""
export AWS_ACCESS_KEY_ID="" # Access key
export AWS_SECRET_ACCESS_KEY="" # Secret access key
export AWS_REGION_NAME="" # us-east-1, us-east-2, us-west-1, us-west-2
litellm

# RUNNING on http://0.0.0.0:4000
  1. Virtuellen Schlüssel generieren
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{}'

Erwartete Antwort

{
...
"key": "sk-1234ewknldferwedojwojw"
}
  1. Testen Sie es!
curl -X POST 'http://0.0.0.0:4000/bedrock/model/cohere.command-r-v1:0/converse' \
-H 'Authorization: Bearer sk-1234ewknldferwedojwojw' \
-H 'Content-Type: application/json' \
-d '{
"messages": [
{"role": "user",
"content": [{"text": "Hello"}]
}
]
}'

Erweitert - Bedrock Agents

Bedrock Agents über LiteLLM Proxy aufrufen

import os 
import boto3
from botocore.config import Config

# # Define your proxy endpoint
proxy_endpoint = "http://0.0.0.0:4000/bedrock" # 👈 your proxy base url

# # Create a Config object with the proxy
# Custom headers
custom_headers = {
'litellm_user_api_key': 'Bearer sk-1234', # 👈 your proxy api key
}


os.environ["AWS_ACCESS_KEY_ID"] = "my-fake-key-id"
os.environ["AWS_SECRET_ACCESS_KEY"] = "my-fake-access-key"


# Create the client
runtime_client = boto3.client(
service_name="bedrock-agent-runtime",
region_name="us-west-2",
endpoint_url=proxy_endpoint
)

# Custom header injection
def inject_custom_headers(request, **kwargs):
request.headers.update(custom_headers)

# Attach the event to inject custom headers before the request is sent
runtime_client.meta.events.register('before-send.*.*', inject_custom_headers)


response = runtime_client.invoke_agent(
agentId="L1RT58GYRW",
agentAliasId="MFPSBCXYTW",
sessionId="12345",
inputText="Who do you know?"
)

completion = ""

for event in response.get("completion"):
chunk = event["chunk"]
completion += chunk["bytes"].decode()

print(completion)