Skip to content

Commit

Permalink
ADD openai_quickstart.ipynb (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykriger-newrelic authored Feb 22, 2024
1 parent 000e0b8 commit 278be65
Showing 1 changed file with 290 additions and 0 deletions.
290 changes: 290 additions & 0 deletions examples/openai_quickstart.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "ulIeE05bfcd7"
},
"source": [
"## Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cCfN5GfAc-nS",
"outputId": "4636ccbe-a0eb-43ab-c1ac-04d38f74da33"
},
"outputs": [],
"source": [
"# Install the nr-openai-observability library\n",
"!pip install -qqq git+https://github.com/newrelic/nr-openai-observability@staging"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wWAEBIUWfjFx"
},
"source": [
"## Variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "mOSIwHMsfCVB"
},
"outputs": [],
"source": [
"# Common environment variables for configuring New Relic Python Agent\n",
"# Only run this section if you did not update these values in your newrelic.ini file\n",
"# or in your Jupyter environment.\n",
"\n",
"import os\n",
"\n",
"os.environ[\"NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_ATTRIBUTE_VALUE\"] = \"4095\"\n",
"\n",
"# Apply recommended settings\n",
"os.environ[\"NEW_RELIC_APPLICATION_LOGGING_ENABLED\"] = \"true\"\n",
"os.environ[\"NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED\"] = \"true\"\n",
"os.environ[\"NEW_RELIC_DISTRIBUTED_TRACING_ENABLED\"] = \"true\"\n",
"\n",
"\n",
"os.environ[\"NEW_RELIC_APP_NAME\"] = \"openai-example\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "W3YbZNtAfpMb"
},
"source": [
"## Secrets"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SikrLbYRgNZB"
},
"source": [
"### [Get your License key](https://one.newrelic.com/launcher/api-keys-ui.api-keys-launcher) (also referenced as ingest - license) and set it as environment variable: NEW_RELIC_LICENSE_KEY. [Click here](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/#license-key) for more details and instructions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hTH2uRd_foh8",
"outputId": "9b1f87dc-4c74-4596-867e-3393ffd17aa1"
},
"outputs": [],
"source": [
"from getpass import getpass\n",
"\n",
"os.environ[\"NEW_RELIC_LICENSE_KEY\"] = getpass(\"Please enter your New Relic NEW_RELIC_LICENSE_KEY:\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_DT7tDPThJy9"
},
"source": [
"### OpenAI API key can be created [here](https://platform.openai.com/api-keys)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fJspds9-g0VK",
"outputId": "aa1be893-a46e-49b5-a8b1-8a951140d4f4"
},
"outputs": [],
"source": [
"os.environ[\"OPENAI_API_KEY\"] = getpass(\"Please enter your OPENAI_API_KEY:\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BqlpHijVfCVD",
"outputId": "a5f942e2-f992-4a46-c5eb-c23ae4c12e43"
},
"outputs": [],
"source": [
"# Initialize New Relic Python Agent\n",
"import newrelic.agent\n",
"\n",
"newrelic.agent.initialize()\n",
"newrelic.agent.register_application(timeout=10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kXVDeGdnfCVD"
},
"outputs": [],
"source": [
"# Initialize New Relic additional OpenAI Monitoring\n",
"# This step is only needed for the Limited Preview. This instrumentation\n",
"# will be available directly in the New Relic Agent for GA.\n",
"#\n",
"# For this specific example, this block is *NOT* needed. This example\n",
"# is using the OpenAI 1.x APIs and for that instrumentation, only\n",
"# the New Relic Python Agent is needed.\n",
"#\n",
"#from nr_openai_observability import monitor\n",
"\n",
"#monitor.initialization()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sXOVl6AQfCVE"
},
"outputs": [],
"source": [
"# When not utilizing a web framework, we need a python function that can be\n",
"# decorated so that the New Relic Python Agent will instrument the method call.\n",
"\n",
"@newrelic.agent.background_task()\n",
"@newrelic.agent.function_trace(name=\"openai\")\n",
"def invoke_openai():\n",
" from openai import OpenAI\n",
"\n",
" # prompt is credited to https://prompts.chat/#act-as-a-math-teacher\n",
" prompt = \"\"\"I want you to act as a math teacher.\n",
" I will provide some mathematical equations or concepts,\n",
" and it will be your job to explain them in easy-to-understand terms.\n",
" This could include providing step-by-step instructions for solving a problem,\n",
" demonstrating various techniques with visuals or suggesting online\n",
" resources for further study.\"\"\"\n",
" user_request = (\n",
" 'My first request is “I need help understanding how probability works.\"'\n",
" )\n",
"\n",
" client = OpenAI()\n",
" completion = client.chat.completions.create(\n",
" model=\"gpt-4\",\n",
" max_tokens=500,\n",
" messages=[\n",
" {\n",
" \"role\": \"system\",\n",
" \"content\": prompt,\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": user_request,\n",
" },\n",
" ],\n",
" )\n",
" print(completion.choices[0].message.content)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Itn1qLvjdEq-",
"outputId": "db80e61d-b900-47cb-95ba-806ea736e12a"
},
"outputs": [],
"source": [
"# Make a call to an LLM to generate metrics for NewRelic connection\n",
"# Invoking OpenAI for this question will take a little while as it is\n",
"# running in a synchronous or blocking mode. Just give it a bit of time\n",
"# to finish.\n",
"\n",
"import os\n",
"\n",
"import openai\n",
"\n",
"if os.getenv(\"OPENAI_API_KEY\") is None:\n",
" print(\"You must set your OPENAI_API_KEY environment variable\")\n",
"else:\n",
" openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
" invoke_openai()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "4TYgTjS1fCVE"
},
"outputs": [],
"source": [
"# Allow the New Relic agent to send final messages as part of shutdown\n",
"# The agent by default can send data up to a minute later\n",
"newrelic.agent.shutdown_agent(60)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qIsj-3RgiGi8"
},
"source": [
"## [Go here](https://one.newrelic.com/nr1-core?filters=%28domain%20%3D%20%27APM%27%20AND%20type%20%3D%20%27APPLICATION%27%29&state=c8e6f1ba-a8e5-b72c-374b-2c28c4e21ec8) to see your application in New Relic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gO-L8l89kLsm"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 278be65

Please sign in to comment.