The Prompt Playground will automatically be available through a message if a Prompt instance is passed to that message.

Chainlit integrations will take care of this for you if the framework you are using supports it.

Step 1: Create a Prompt Instance

For this example we are going to create a completion with OpenAI chat API and create a Prompt reflecting the API call made to OpenAI.

Chainlit supports other LLM providers and let’s you implement custom ones. Learn more here.

from openai import AsyncOpenAI
import chainlit as cl
from chainlit.prompt import Prompt, PromptMessage
from chainlit.playground.providers import ChatOpenAI

client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")

template = "Hello, {name}!"
inputs = {"name": "John"}

settings = {
    "model": "gpt-3.5-turbo",
    "temperature": 0,
    # ... more settings
}

@cl.on_chat_start
async def start():
    # Create the Chainlit Prompt instance
    prompt = Prompt(
        provider=ChatOpenAI.id,
        inputs=inputs,
        settings=settings,
        messages=[
          PromptMessage(
            template=template,
            formatted=template.format(**inputs),
            role="assistant")
          ]
    )

    # Make the call to OpenAI
    response = await client.chat.completions.create(
        messages=[m.to_openai() for m in prompt.messages], **settings
    )

    prompt.completion = response.choices[0].message.content

Check the Prompt API reference to learn about all the attributes and the difference between chat and non chat modes.

Every attribute of the Prompt class is optional. The Prompt Playground adapts to the available data.

Step 2: Pass the Prompt to a Message

Once you have created the Prompt instance, you can pass it to a Message instance and send it.

Here is an example of sending a Message instance with a Prompt:

await cl.Message(
    content="The content of the message is not important.",
    prompt=prompt,
).send()

Step 3: Open the Prompt Playground

Once the message is sent, a new button will be available beneath it. Clicking on that button will open the Prompt Playground in the context of that message.

Open the Prompt Playground