Skip to main content

1. Configure Bedrock in Freeplay App

Configure Bedrock as a provider in your Freeplay account by going to Models -> Toggle on Bedrock -> Deploy a prompt with Bedrock

2. Import Anthropic Bedrock Client

Anthropic offers a specific Bedrock client in their SDK

3. Create a Bedrock Client

Instantiate your Bedrock client using the anthropic SDK

4. Fetch and Format Prompt

Retrieve a formatted prompt from freeplay

5. Call Bedrock Model

Call your Bedrock model

6. Record to Freeplay

Record the interaction back to freeplay

Examples

from freeplay import Freeplay, RecordPayload, CallInfo, ResponseInfo
from anthropic.lib.bedrock import AnthropicBedrock
from anthropic import NotGiven
import os
import time
from dotenv import load_dotenv

load_dotenv()
project_id = os.getenv("FREEPLAY_PROJECT_ID")
freeplay_key = os.getenv("FREEPLAY_KEY")
freeplay_url = os.getenv("FREEPLAY_URL")
aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID")
aws_secret_key = os.getenv("AWS_SECRET_KEY")

fpClient = Freeplay(
    api_base=freeplay_url,
    freeplay_api_key=freeplay_key
)

bedrockClient = AnthropicBedrock(
    aws_secret_key=aws_secret_key,
    aws_access_key=aws_access_key_id,
    aws_region="us-east-1"
)

# fetch the prompt
prompt_vars = {
    "pop_star": "Taylor Swift",
}
formatted_prompt = fpClient.prompts.get_formatted(
    project_id=project_id,
    template_name="album_bot",
    environment="latest",
    variables=prompt_vars
)

s = time.time()
response = bedrockClient.messages.create(
    model=formatted_prompt.prompt_info.model,
    system=formatted_prompt.system_content or NotGiven(),
    messages=formatted_prompt.llm_prompt,
    **formatted_prompt.prompt_info.model_parameters
)
e = time.time()

response_text = response.content[0].text

all_messages = formatted_prompt.all_messages(
    {"role": "assistant",
    "content": response_text}
)

session = fpClient.sessions.create()

payload = RecordPayload(
    project_id=project_id,
    all_messages=all_messages,
    inputs=prompt_vars,
    session_info=session,
    prompt_version_info=formatted_prompt.prompt_info,
    call_info=CallInfo.from_prompt_info(formatted_prompt.prompt_info, start_time=s, end_time=e),
    response_info=ResponseInfo(is_complete=True)
)
fpClient.recordings.create(payload)