Reach out
← Back to Cookbook

arize phoenix tracing

Details

File: third_party/Phoenix/arize_phoenix_tracing.ipynb

Type: Jupyter Notebook

Use Cases: Tracing

Integrations: Phoenix, Arize

Content

Notebook content (JSON format):

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center>\n",
    "    <p style=\"text-align:center\">\n",
    "        <img alt=\"phoenix logo\" src=\"https://storage.googleapis.com/arize-phoenix-assets/assets/phoenix-logo-light.svg\" width=\"200\"/>\n",
    "        <br>\n",
    "        <a href=\"https://docs.arize.com/phoenix/\">Docs</a>\n",
    "        |\n",
    "        <a href=\"https://github.com/Arize-ai/phoenix\">GitHub</a>\n",
    "        |\n",
    "        <a href=\"https://join.slack.com/t/arize-ai/shared_invite/zt-1px8dcmlf-fmThhDFD_V_48oU7ALan4Q\">Community</a>\n",
    "    </p>\n",
    "</center>\n",
    "<h1 align=\"center\"> Structured Data Extraction Service Observability with Mistral AI and Phoenix </h1>\n",
    "\n",
    "In this tutorial, you will:\n",
    "\n",
    "- Use Mistral's [tool calling feature](https://docs.mistral.ai/guides/function-calling/) to perform structured data extraction: the task of transforming unstructured input (e.g., user requests in natural language) into structured format (e.g., tabular format),\n",
    "- Instrument your Mistral AI client to record trace data in [OpenInference tracing](https://github.com/Arize-ai/openinference) format,\n",
    "- Inspect the traces and spans of your application to visualize your trace data,\n",
    "\n",
    "## Background\n",
    "\n",
    "One powerful feature of the Mistral AI chat completions API is tool calling, wherein a user describes the signature and arguments of one or more tools to the Mistral AI API via a JSON Schema and natural language descriptions, and the LLM decides when to use each tool and provides argument values depending on the context of the conversation. In addition to its primary purpose of integrating function inputs and outputs into a sequence of chat messages, tool calling is also useful for structured data extraction, since you can specify a \"function\" that describes the desired format of your structured output. Structured data extraction is useful for a variety of purposes, including ETL or as input to another machine learning model such as a recommender system.\n",
    "\n",
    "While it's possible to produce structured output without using tool calling via careful prompting, tool calling is more reliable at producing output that conforms to a particular format. For more details on Mistral AI's tool calling API, see the [Mistral AI documentation](https://docs.mistral.ai/guides/function-calling/).\n",
    "\n",
    "Let's get started!\n",
    "\n",
    "â„šī¸ This notebook requires a Mistral AI API key."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Install Dependencies and Import Libraries\n",
    "\n",
    "Install dependencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q arize-phoenix jsonschema openinference-instrumentation-mistralai\n",
    "!pip install -qU mistralai "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "from typing import Any, Dict\n",
    "\n",
    "import pandas as pd\n",
    "import phoenix as px\n",
    "from phoenix.otel import register\n",
    "from mistralai import Mistral\n",
    "from mistralai.models import UserMessage, SystemMessage\n",
    "from openinference.instrumentation.mistralai import MistralAIInstrumentor\n",
    "\n",
    "pd.set_option(\"display.max_colwidth\", None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Configure Your Mistral API Key\n",
    "\n",
    "Set your Mistral API key if it is not already set as an environment variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not (api_key := os.getenv(\"MISTRAL_API_KEY\")):\n",
    "    api_key = getpass(\"🔑 Enter your Mistral AI API key: \")\n",
    "client = Mistral(api_key=api_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Run Phoenix in the Background\n",
    "\n",
    "Launch Phoenix as a background session to collect the trace data emitted by your instrumented Mistral client. For details on how to self-host Phoenix or connect to a remote Phoenix instance, see the [Phoenix documentation](https://docs.arize.com/phoenix/quickstart)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session = px.launch_app()\n",
    "tracer_provider = register()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Instrument Your Mistral Client\n",
    "\n",
    "Instrument your Mistral client with a tracer that emits telemetry data in OpenInference format. [OpenInference](https://github.com/Arize-ai/openinference) is an open standard for capturing and storing LLM application traces that enables LLM applications to seamlessly integrate with LLM observability solutions such as Phoenix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MistralAIInstrumentor().instrument(tracer_provider=tracer_provider)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Extract Your Structured Data\n",
    "\n",
    "We'll extract structured data from the following list of ten travel requests."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "travel_requests = [\n",
    "    \"Can you recommend a luxury hotel in Tokyo with a view of Mount Fuji for a romantic honeymoon?\",\n",
    "    \"I'm looking for a mid-range hotel in London with easy access to public transportation for a solo backpacking trip. Any suggestions?\",\n",
    "    \"I need a budget-friendly hotel in San Francisco close to the Golden Gate Bridge for a family vacation. What do you recommend?\",\n",
    "    \"Can you help me find a boutique hotel in New York City with a rooftop bar for a cultural exploration trip?\",\n",
    "    \"I'm planning a business trip to Tokyo and I need a hotel near the financial district. What options are available?\",\n",
    "    \"I'm traveling to London for a solo vacation and I want to stay in a trendy neighborhood with great shopping and dining options. Any recommendations for hotels?\",\n",
    "    \"I'm searching for a luxury beachfront resort in San Francisco for a relaxing family vacation. Can you suggest any options?\",\n",
    "    \"I need a mid-range hotel in New York City with a fitness center and conference facilities for a business trip. Any suggestions?\",\n",
    "    \"I'm looking for a budget-friendly hotel in Tokyo with easy access to public transportation for a backpacking trip. What do you recommend?\",\n",
    "    \"I'm planning a honeymoon in London and I want a luxurious hotel with a spa and romantic atmosphere. Can you suggest some options?\",\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Mistral AI API uses [JSON Schema](https://json-schema.org/) and natural language descriptions to specify the signature of a function to call. In this case, we'll describe a function to record the following attributes of the unstructured text input:\n",
    "\n",
    "- **location:** The desired destination,\n",
    "- **budget_level:** A categorical budget preference,\n",
    "- **purpose:** The purpose of the trip.\n",
    "\n",
    "The use of JSON Schema enables us to define the type of each field in the output and even enumerate valid values in the case of categorical outputs. Mistral AI tool calling can thus be used for tasks that might previously have been performed by named-entity recognition (NER) and/ or classification models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters_schema = {\n",
    "    \"type\": \"object\",\n",
    "    \"properties\": {\n",
    "        \"location\": {\n",
    "            \"type\": \"string\",\n",
    "            \"description\": 'The desired destination location. Use city, state, and country format when possible. If no destination is provided, return \"unstated\".',\n",
    "        },\n",
    "        \"budget_level\": {\n",
    "            \"type\": \"string\",\n",
    "            \"enum\": [\"low\", \"medium\", \"high\", \"not_stated\"],\n",
    "            \"description\": 'The desired budget level. If no budget level is provided, return \"not_stated\".',\n",
    "        },\n",
    "        \"purpose\": {\n",
    "            \"type\": \"string\",\n",
    "            \"enum\": [\"business\", \"pleasure\", \"other\", \"non_stated\"],\n",
    "            \"description\": 'The purpose of the trip. If no purpose is provided, return \"not_stated\".',\n",
    "        },\n",
    "    },\n",
    "    \"required\": [\"location\", \"budget_level\", \"purpose\"],\n",
    "}\n",
    "function_schema = {\n",
    "    \"name\": \"record_travel_request_attributes\",\n",
    "    \"description\": \"Records the attributes of a travel request\",\n",
    "    \"parameters\": parameters_schema,\n",
    "}\n",
    "tool_schema = {\n",
    "    \"type\": \"function\",\n",
    "    \"function\": function_schema,\n",
    "}\n",
    "system_message = \"You are an assistant that parses and records the attributes of a user's travel request.\"\n",
    "\n",
    "\n",
    "def extract_raw_travel_request_attributes_string(\n",
    "    travel_request: str,\n",
    "    tool_schema: Dict[str, Any],\n",
    "    system_message: str,\n",
    "    client: Mistral,\n",
    "    model: str = \"mistral-large-latest\",\n",
    ") -> str:\n",
    "    chat_completion = client.chat.complete(\n",
    "        model=model,\n",
    "        messages=[\n",
    "            SystemMessage(role=\"system\", content=system_message),\n",
    "            UserMessage(role=\"user\", content=travel_request),\n",
    "        ],\n",
    "        tools=[tool_schema],\n",
    "        # By default, the LLM will choose whether or not to use a tool given the conversation context.\n",
    "        # The line below forces the LLM to use a tool so that the output conforms to the schema.\n",
    "        # tool_choice=ToolChoice.any,\n",
    "    )\n",
    "    return chat_completion.choices[0].message.tool_calls[0].function.arguments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the extractions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_travel_attributes_column = []\n",
    "for travel_request in travel_requests:\n",
    "    print(\"Travel request:\")\n",
    "    print(\"==============\")\n",
    "    print(travel_request)\n",
    "    print()\n",
    "    raw_travel_attributes = extract_raw_travel_request_attributes_string(\n",
    "        travel_request, tool_schema, system_message, client\n",
    "    )\n",
    "    raw_travel_attributes_column.append(raw_travel_attributes)\n",
    "    print(\"Raw Travel Attributes:\")\n",
    "    print(\"=====================\")\n",
    "    print(raw_travel_attributes)\n",
    "    print()\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. View traces in Phoenix\n",
    "\n",
    "You should now be able to view traces in Phoenix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"Current Phoenix URL: {session.url}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Traces](./images/traces-1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Export Your Trace Data\n",
    "\n",
    "Your OpenInference trace data is collected by Phoenix and can be exported to a pandas dataframe for further analysis and evaluation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trace_df = px.Client().get_spans_dataframe()\n",
    "trace_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Recap\n",
    "\n",
    "Congrats! In this tutorial, you:\n",
    "\n",
    "- Built a service to perform structured data extraction on unstructured text using Mistral AI tool calling\n",
    "- Instrumented your service with an OpenInference tracer\n",
    "- Examined your telemetry data in Phoenix"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "envName=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.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}