Skip to main content

1. Configure your Environment

Configure environment variables and create your freeplay session

2. Create a Freeplay Session

Create a Freeplay Session that we will use to tie together all completions in the Chain

3. Run the first Completion

  • Fetch and format the first prompt
  • Call your LLM
  • Extract the needed results

4. Record the first Completion to Freeplay

  • Record to Freeplay

5. Run second Completion

  • Fetch and format second prompt using output from the first
  • Call your LLM provider
  • Record to Freeplay

Examples

import Freeplay, { getCallInfo, getSessionInfo } from "freeplay";
import OpenAI from "openai";
import * as dotenv from "dotenv";

// confgure neccesaary environment variables
let projectId = process.env["FREEPLAY_PROJECT_ID"];
let freeplayApiKey = process.env["FREEPLAY_KEY"];
let freeplayUrl = process.env["FREEPLAY_URL"];
let openaiApiKey = process.env["OPENAI_API_KEY"];
const openai = new OpenAI(process.env["OPENAI_API_KEY"]);

// instantiate freeplay client
const fpClient = new Freeplay({
    freeplayApiKey: freeplayApiKey,
    baseUrl: freeplayUrl,
});

// create a freeplay session
// we will tie all completions in the chain to this session
let session = fpClient.sessions.create({});

// fetch the first prompt
// generate an album title for a pop star
const prompt_varsA = {"pop_star": "Taylor Swift"};
const formattedPromptA = await fpClient.prompts.getFormatted({
    projectId: projectID,
    templateName: "album_bot",
    environment: "latest",
    variables: {"pop_star": "Taylor Swift"},
});

let start = new Date();
const chatCompletionA = await openai.chat.completions.create({
    messages: formattedPromptA.llmPrompt,
    model: formattedPromptA.promptInfo.model,
  	...formattedPrompt.promptInfo.modelParameters
});
let end = new Date();

// get the album name
let album_name = chatCompletionA.choices[0].message.content;

// record to freeplay
fpClient.recordings.create({
    allMessages: formattedPromptA.allMessages({
        role: chatCompletionA.choices[0].message.role,
        content: chatCompletionA.choices[0].message.content,
    }),
    inputs: prompt_varsA,
    sessionInfo: getSessionInfo(session), // tie the completion to the session
    promptInfo: formattedPromptA.promptInfo,
    callInfo: getCallInfo(formattedPromptA.promptInfo, start, end),
    responseInfo: {
        isComplete: "stop_sequence" === chatCompletionA.choices[0].stop
    }
});

// fetch the second prompt
// generate the song list for the album
const prompt_varsB = {"album_name": album_name, pop_star: "Taylor Swift"};
const formattedPromptB = await fpClient.prompts.getFormatted({
    projectId: projectID,
    templateName: "song_bot",
    environment: "latest",
    variables: prompt_varsB,
});

start = new Date();
const chatCompletionB = await openai.chat.completions.create({
    messages: formattedPromptB.llmPrompt,
    model: formattedPromptB.promptInfo.model,
  	...formattedPrompt.promptInfo.modelParameters
});
end = new Date();

// record to freeplay
fpClient.recordings.create({
		projectId,
    allMessages: formattedPromptB.allMessages({
        role: chatCompletionB.choices[0].message.role,
        content: chatCompletionB.choices[0].message.content,
    }),
    inputs: prompt_varsB,
    sessionInfo: getSessionInfo(session), // tie the completion to the session
    promptVersionInfo: formattedPromptB.promptInfo,
    callInfo: getCallInfo(formattedPromptB.promptInfo, start, end),
    responseInfo: {
        isComplete: "stop_sequence" === chatCompletionB.choices[0].stop
    }
});