Skip to content

Commit 278be65

Browse files
author
ykriger-newrelic
authored
ADD openai_quickstart.ipynb (newrelic#66)
1 parent 000e0b8 commit 278be65

File tree

1 file changed

+290
-0
lines changed

1 file changed

+290
-0
lines changed

Diff for: examples/openai_quickstart.ipynb

+290
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "ulIeE05bfcd7"
7+
},
8+
"source": [
9+
"## Dependencies"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"colab": {
17+
"base_uri": "https://localhost:8080/"
18+
},
19+
"id": "cCfN5GfAc-nS",
20+
"outputId": "4636ccbe-a0eb-43ab-c1ac-04d38f74da33"
21+
},
22+
"outputs": [],
23+
"source": [
24+
"# Install the nr-openai-observability library\n",
25+
"!pip install -qqq git+https://github.com/newrelic/nr-openai-observability@staging"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {
31+
"id": "wWAEBIUWfjFx"
32+
},
33+
"source": [
34+
"## Variables"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"metadata": {
41+
"id": "mOSIwHMsfCVB"
42+
},
43+
"outputs": [],
44+
"source": [
45+
"# Common environment variables for configuring New Relic Python Agent\n",
46+
"# Only run this section if you did not update these values in your newrelic.ini file\n",
47+
"# or in your Jupyter environment.\n",
48+
"\n",
49+
"import os\n",
50+
"\n",
51+
"os.environ[\"NEW_RELIC_CUSTOM_INSIGHTS_EVENTS_MAX_ATTRIBUTE_VALUE\"] = \"4095\"\n",
52+
"\n",
53+
"# Apply recommended settings\n",
54+
"os.environ[\"NEW_RELIC_APPLICATION_LOGGING_ENABLED\"] = \"true\"\n",
55+
"os.environ[\"NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED\"] = \"true\"\n",
56+
"os.environ[\"NEW_RELIC_DISTRIBUTED_TRACING_ENABLED\"] = \"true\"\n",
57+
"\n",
58+
"\n",
59+
"os.environ[\"NEW_RELIC_APP_NAME\"] = \"openai-example\""
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {
65+
"id": "W3YbZNtAfpMb"
66+
},
67+
"source": [
68+
"## Secrets"
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {
74+
"id": "SikrLbYRgNZB"
75+
},
76+
"source": [
77+
"### [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."
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {
84+
"colab": {
85+
"base_uri": "https://localhost:8080/"
86+
},
87+
"id": "hTH2uRd_foh8",
88+
"outputId": "9b1f87dc-4c74-4596-867e-3393ffd17aa1"
89+
},
90+
"outputs": [],
91+
"source": [
92+
"from getpass import getpass\n",
93+
"\n",
94+
"os.environ[\"NEW_RELIC_LICENSE_KEY\"] = getpass(\"Please enter your New Relic NEW_RELIC_LICENSE_KEY:\")"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {
100+
"id": "_DT7tDPThJy9"
101+
},
102+
"source": [
103+
"### OpenAI API key can be created [here](https://platform.openai.com/api-keys)."
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {
110+
"colab": {
111+
"base_uri": "https://localhost:8080/"
112+
},
113+
"id": "fJspds9-g0VK",
114+
"outputId": "aa1be893-a46e-49b5-a8b1-8a951140d4f4"
115+
},
116+
"outputs": [],
117+
"source": [
118+
"os.environ[\"OPENAI_API_KEY\"] = getpass(\"Please enter your OPENAI_API_KEY:\")"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": null,
124+
"metadata": {
125+
"colab": {
126+
"base_uri": "https://localhost:8080/"
127+
},
128+
"id": "BqlpHijVfCVD",
129+
"outputId": "a5f942e2-f992-4a46-c5eb-c23ae4c12e43"
130+
},
131+
"outputs": [],
132+
"source": [
133+
"# Initialize New Relic Python Agent\n",
134+
"import newrelic.agent\n",
135+
"\n",
136+
"newrelic.agent.initialize()\n",
137+
"newrelic.agent.register_application(timeout=10)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {
144+
"id": "kXVDeGdnfCVD"
145+
},
146+
"outputs": [],
147+
"source": [
148+
"# Initialize New Relic additional OpenAI Monitoring\n",
149+
"# This step is only needed for the Limited Preview. This instrumentation\n",
150+
"# will be available directly in the New Relic Agent for GA.\n",
151+
"#\n",
152+
"# For this specific example, this block is *NOT* needed. This example\n",
153+
"# is using the OpenAI 1.x APIs and for that instrumentation, only\n",
154+
"# the New Relic Python Agent is needed.\n",
155+
"#\n",
156+
"#from nr_openai_observability import monitor\n",
157+
"\n",
158+
"#monitor.initialization()"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {
165+
"id": "sXOVl6AQfCVE"
166+
},
167+
"outputs": [],
168+
"source": [
169+
"# When not utilizing a web framework, we need a python function that can be\n",
170+
"# decorated so that the New Relic Python Agent will instrument the method call.\n",
171+
"\n",
172+
"@newrelic.agent.background_task()\n",
173+
"@newrelic.agent.function_trace(name=\"openai\")\n",
174+
"def invoke_openai():\n",
175+
" from openai import OpenAI\n",
176+
"\n",
177+
" # prompt is credited to https://prompts.chat/#act-as-a-math-teacher\n",
178+
" prompt = \"\"\"I want you to act as a math teacher.\n",
179+
" I will provide some mathematical equations or concepts,\n",
180+
" and it will be your job to explain them in easy-to-understand terms.\n",
181+
" This could include providing step-by-step instructions for solving a problem,\n",
182+
" demonstrating various techniques with visuals or suggesting online\n",
183+
" resources for further study.\"\"\"\n",
184+
" user_request = (\n",
185+
" 'My first request is “I need help understanding how probability works.\"'\n",
186+
" )\n",
187+
"\n",
188+
" client = OpenAI()\n",
189+
" completion = client.chat.completions.create(\n",
190+
" model=\"gpt-4\",\n",
191+
" max_tokens=500,\n",
192+
" messages=[\n",
193+
" {\n",
194+
" \"role\": \"system\",\n",
195+
" \"content\": prompt,\n",
196+
" },\n",
197+
" {\n",
198+
" \"role\": \"user\",\n",
199+
" \"content\": user_request,\n",
200+
" },\n",
201+
" ],\n",
202+
" )\n",
203+
" print(completion.choices[0].message.content)\n"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": null,
209+
"metadata": {
210+
"colab": {
211+
"base_uri": "https://localhost:8080/"
212+
},
213+
"id": "Itn1qLvjdEq-",
214+
"outputId": "db80e61d-b900-47cb-95ba-806ea736e12a"
215+
},
216+
"outputs": [],
217+
"source": [
218+
"# Make a call to an LLM to generate metrics for NewRelic connection\n",
219+
"# Invoking OpenAI for this question will take a little while as it is\n",
220+
"# running in a synchronous or blocking mode. Just give it a bit of time\n",
221+
"# to finish.\n",
222+
"\n",
223+
"import os\n",
224+
"\n",
225+
"import openai\n",
226+
"\n",
227+
"if os.getenv(\"OPENAI_API_KEY\") is None:\n",
228+
" print(\"You must set your OPENAI_API_KEY environment variable\")\n",
229+
"else:\n",
230+
" openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
231+
" invoke_openai()\n"
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": null,
237+
"metadata": {
238+
"id": "4TYgTjS1fCVE"
239+
},
240+
"outputs": [],
241+
"source": [
242+
"# Allow the New Relic agent to send final messages as part of shutdown\n",
243+
"# The agent by default can send data up to a minute later\n",
244+
"newrelic.agent.shutdown_agent(60)"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {
250+
"id": "qIsj-3RgiGi8"
251+
},
252+
"source": [
253+
"## [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"
254+
]
255+
},
256+
{
257+
"cell_type": "code",
258+
"execution_count": null,
259+
"metadata": {
260+
"id": "gO-L8l89kLsm"
261+
},
262+
"outputs": [],
263+
"source": []
264+
}
265+
],
266+
"metadata": {
267+
"colab": {
268+
"provenance": []
269+
},
270+
"kernelspec": {
271+
"display_name": "Python 3 (ipykernel)",
272+
"language": "python",
273+
"name": "python3"
274+
},
275+
"language_info": {
276+
"codemirror_mode": {
277+
"name": "ipython",
278+
"version": 3
279+
},
280+
"file_extension": ".py",
281+
"mimetype": "text/x-python",
282+
"name": "python",
283+
"nbconvert_exporter": "python",
284+
"pygments_lexer": "ipython3",
285+
"version": "3.11.7"
286+
}
287+
},
288+
"nbformat": 4,
289+
"nbformat_minor": 0
290+
}

0 commit comments

Comments
 (0)