1. Create the necessary clients
2. Fetch the prompt from Freeplay
Freeplay will format your messages and model parameters for the provider you have configured on the prompt template version3. Make the LLM call
Key off the provider name from the formatted prompt and route to the proper provider4. Record to Freeplay
Record the interaction back to FreeplayExamples
Copy
Ask AI
from freeplay import Freeplay, RecordPayload, ResponseInfo, CallInfo, SessionInfo
from openai import OpenAI
from anthropic import Anthropic
import os
import time
fp_client = Freeplay(
freeplay_api_key=os.getenv("FREEPLAY_API_KEY"),
api_base=os.getenv("FREEPLAY_URL"),
)
project_id=os.getenv("FREEPLAY_PROJECT_ID")
openai_client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)
anthropic_client = Anthropic(
api_key=os.getenv("ANTHROPIC_API_KEY"),
)
user_question = "What is the capital of France?"
# Fetch the prompt from freeplay
prompt_vars = {"question": user_question}
formatted_prompt = fp_client.prompts.get_formatted(
project_id="5688ebaf-7f22-4d5d-b9bb-bc715c8faabb",
template_name="BasicTriviaBot",
environment="dev",
variables=prompt_vars,
)
# make the llm call using the provider configured on the prompt
# Freeplay will format the prompt and model parameters for the given provider
start = time.time()
if formatted_prompt.prompt_info.provider == "openai":
response = openai_client.chat.completions.create(
model=formatted_prompt.prompt_info.model,
messages=formatted_prompt.llm_prompt,
**formatted_prompt.prompt_info.model_parameters
)
msg = response.choices[0].message
answer = msg.content
elif formatted_prompt.prompt_info.provider == "anthropic":
response = anthropic_client.messages.create(
model=formatted_prompt.prompt_info.model,
system=formatted_prompt.system_content or NotGiven(), # Freeplay splits out the system prompt from messages for Anthropic
messages=formatted_prompt.llm_prompt,
**formatted_prompt.prompt_info.model_parameters
)
msg = response
answer = msg.content[0].text
else:
raise ValueError(f"Unsupported provider: {formatted_prompt.prompt_info.provider}")
end = time.time()
# record to Freeplay
session = fp_client.sessions.create()
fp_client.recordings.create(
RecordPayload(
project_id=project_id,
all_messages=all_messages,
tool_schema=formatted_prompt.tool_schema,
session_info=session.session_info,
inputs=test_case.variables,
prompt_version_info=formatted_prompt.prompt_info,
call_info=CallInfo.from_prompt_info(formatted_prompt.prompt_info, start, end),
response_info=ResponseInfo(is_complete=completion.choices[0].finish_reason == 'stop'),
)
)
print(f'Question {user_question} \n Answer {answer}')

