{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", " \"phoenix\n", "
\n", " Docs\n", " |\n", " GitHub\n", " |\n", " Community\n", "

\n", "
\n", "

Tracing Images with Phoenix

\n", "\n", "In this tutorial, you will:\n", "- Trace a toy application that uses images, and view those images in Phoenix.\n", "\n", "ℹ️ This notebook requires an OpenAI API key." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Install dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "pip install -q \"arize-phoenix>=4.29.0\" openinference-instrumentation-openai openai 'httpx<0.28'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Connect to Phoenix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if PHOENIX_API_KEY is present in the environment variables.\n", "# If it is, we'll use the cloud instance of Phoenix. If it's not, we'll start a local instance.\n", "# A third option is to connect to a docker or locally hosted instance.\n", "# See https://docs.arize.com/phoenix/setup/environments for more information.\n", "\n", "import os\n", "\n", "if \"PHOENIX_API_KEY\" in os.environ:\n", " os.environ[\"PHOENIX_CLIENT_HEADERS\"] = f\"api_key={os.environ['PHOENIX_API_KEY']}\"\n", " os.environ[\"PHOENIX_COLLECTOR_ENDPOINT\"] = \"https://app.phoenix.arize.com\"\n", "\n", "else:\n", " import phoenix as px\n", "\n", " px.launch_app().view()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from phoenix.otel import register\n", "\n", "tracer_provider = register()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Instrument OpenAI\n", "\n", "Phoenix's existing auto-instrumentors allow you to capture images by image url with OpenAI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from openinference.instrumentation.openai import OpenAIInstrumentor\n", "\n", "OpenAIInstrumentor().instrument(tracer_provider=tracer_provider, skip_dep_check=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also need to set the OpenAI API key in the environment variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import getpass\n", "\n", "if \"OPENAI_API_KEY\" not in os.environ:\n", " os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API key: \")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Include an image url in an OpenAI prompt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "\n", "client = OpenAI()\n", "\n", "response = client.chat.completions.create(\n", " model=\"gpt-4o\",\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": [\n", " {\"type\": \"text\", \"text\": \"What’s in this image?\"},\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg\",\n", " },\n", " },\n", " ],\n", " }\n", " ],\n", " max_tokens=300,\n", ")\n", "\n", "print(response.choices[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "\n", "client = OpenAI()\n", "\n", "response = client.chat.completions.create(\n", " model=\"gpt-4o\",\n", " messages=[\n", " {\n", " \"role\": \"user\",\n", " \"content\": [\n", " {\"type\": \"text\", \"text\": \"What do these images have in common?\"},\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Chicago_Bulls_and_New_Jersey_Nets%2C_March_28%2C_1991.jpg/640px-Chicago_Bulls_and_New_Jersey_Nets%2C_March_28%2C_1991.jpg\",\n", " },\n", " },\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/NBA_shot_clock.jpg/640px-NBA_shot_clock.jpg\",\n", " },\n", " },\n", " {\n", " \"type\": \"image_url\",\n", " \"image_url\": {\n", " \"url\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Kristaps_Porzingis_and_Kris_Humphries.jpg/640px-Kristaps_Porzingis_and_Kris_Humphries.jpg\",\n", " },\n", " },\n", " ],\n", " }\n", " ],\n", " max_tokens=300,\n", ")\n", "\n", "print(response.choices[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should now see the images in your Phoenix app!" ] } ], "metadata": { "kernelspec": { "display_name": "phoenix", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }