" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
"</a>\n",
"\n",
"**Note:** you will need an [OpenAI API key](https://platform.openai.com/account/api-keys) to run this colab.\n",
"\n",
"Use the W&B OpenAI integration to monitor OpenAI API calls and understand how your projects and teams are leveraging LLMs.\n",
"In this example, we'll generate templated Weave Boards: LLM usage monitoring dashboards which you can explore and customize from the UI.\n",
"\n",
"* automatically track LLM usage and aggregate useful metrics like cost, latency and throughput across your projects/teams\n",
"* dynamically query and derive insights from the logs of all your OpenAI API calls\n",
"* iterate visually to slice, aggregate, and explore your data; customize panels to focus on interesting patterns; share progress more easily with your team through an interactive dashboard\n",
"# Step 1: Configure data streaming and storage in W&B\n",
"\n",
"Set WB_ENTITY to your wandb username or team name. Log in to W&B and navigate to Home Page at [wandb.ai/home](https://wandb.ai/home) to see valid options under your \"Profile\" and \"Teams\" in the left sidebar."
"To start monitoring OpenAI API usage, call `init_monitor(<stream>)`, where `<stream>` has the form `<wandb_team_or_user>/<wandb_project>/<stream_name>`. The stream records and stores all the OpenAI API calls.\n",
"\n",
"Running this cell will print out a link to view the current project in the Weave UI."
"Click on the link above to preview the data stream, then click \"OpenAI Monitor Board\" in the right sidebar to create a Weave Board for this data stream.\n",
"# Step 4: Explore & understand your LLM usage\n",
"\n",
"To save your work, rename the board by clicking on the autogenerated name at the top of the page. To share your board, click \\\"Publish\\\" in the top right.\n",
"To visualize your work in real-time as you iterate, you can:\n",
"* keep the Board open in a separate tab and refresh to view the latest data\n",
"* rename the Board for easier reference at any point and \\\"Publish\\\" that version to share a link with others\n",
"* find previously saved Boards by navigating to the relevant W&B entity and W&B project name from weave.wandb.ai\n",
"* or open a new instance of a Board template to start fresh with all the data accumulated so far\n",
"\n",
"\n",
"Next we'll illustrate a few ways you could track OpenAI API calls. There are many more possibilities depending on your use case, and we can't wait to see what you create from these starter templates.\n",
"\n",
"# Examples\n",
"\n",
"## Example 0: Log a prompt and its completion\n",
"\n",
"Monitor a ChatCompletion request and print the corresponding response, extracting only the text of the completion."
"## Example 1: Track relevant parameters as attributes\n",
"\n",
"Factor out parameters of interest and track them as attributes on the logged record.\n",
"Here we track the \"system prompt\" separately from the \"prompt template\" and the \"equation\" parameter. This time we'll print the full structured response from the ChatCompletion call."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7930e774",
"metadata": {
"id": "7930e774"
},
"outputs": [],
"source": [
"system_prompt = \"you always write in bullet points\"\n",
"prompt_template = 'solve the following equation step by step: {equation}'\n",
"## Example 3: Structure prompt engineering experiments\n",
"\n",
"Here we compare a few toy options for the system prompt, user question, and intended audience. Try your own experiments and see if any interesting insights emerge as you explore in the Board and group by different parameters."