|
| 1 | +--- |
| 2 | +title: OpenAI |
| 3 | +description: "AgentOps provides first class support for OpenAI's GPT family of models" |
| 4 | +--- |
| 5 | + |
| 6 | +import CodeTooltip from '/snippets/add-code-tooltip.mdx' |
| 7 | +import EnvTooltip from '/snippets/add-env-tooltip.mdx' |
| 8 | + |
| 9 | +<Note> |
| 10 | +This is a living integration. Should you need any added functionality, message us on [Discord](https://discord.gg/UgJyyxx7uc)! |
| 11 | +</Note> |
| 12 | + |
| 13 | +<Card title="OpenAI" icon="robot" href="https://www.openai.com"> |
| 14 | + First class support for GPT family of models |
| 15 | +</Card> |
| 16 | + |
| 17 | +<Steps> |
| 18 | + <Step title="Install the AgentOps SDK"> |
| 19 | + <CodeGroup> |
| 20 | + ```bash pip |
| 21 | + pip install agentops |
| 22 | + ``` |
| 23 | + ```bash poetry |
| 24 | + poetry add agentops |
| 25 | + ``` |
| 26 | + </CodeGroup> |
| 27 | + </Step> |
| 28 | + <Step title="Install the OpenAI SDK"> |
| 29 | + <Note> |
| 30 | + `openai<1.0.0` has limited support while `openai>=1.0.0` is continuously supported. |
| 31 | + </Note> |
| 32 | + <CodeGroup> |
| 33 | + ```bash pip |
| 34 | + pip install openai |
| 35 | + ``` |
| 36 | + ```bash poetry |
| 37 | + poetry add openai |
| 38 | + ``` |
| 39 | + </CodeGroup> |
| 40 | + |
| 41 | + To install `openai<1.0.0`, use the following: |
| 42 | + <CodeGroup> |
| 43 | + ```bash pip |
| 44 | + pip install "openai<1.0.0" |
| 45 | + ``` |
| 46 | + ```bash poetry |
| 47 | + poetry add "openai<1.0.0" |
| 48 | + ``` |
| 49 | + </CodeGroup> |
| 50 | + </Step> |
| 51 | + <Step title="Add 3 lines of code"> |
| 52 | + <CodeTooltip/> |
| 53 | + <span className="api-key-container"> |
| 54 | + <CodeGroup> |
| 55 | + ```python python |
| 56 | + import agentops |
| 57 | + from openai import OpenAI |
| 58 | + |
| 59 | + agentops.init(<INSERT YOUR API KEY HERE>) |
| 60 | + client = OpenAI() |
| 61 | + ... |
| 62 | + # End of program (e.g. main.py) |
| 63 | + agentops.end_session("Success") # Success|Fail|Indeterminate |
| 64 | + ``` |
| 65 | + </CodeGroup> |
| 66 | + </span> |
| 67 | + <EnvTooltip /> |
| 68 | + <span className="api-key-container"> |
| 69 | + <CodeGroup> |
| 70 | + ```python .env |
| 71 | + AGENTOPS_API_KEY=<YOUR API KEY> |
| 72 | + OPENAI_API_KEY=<YOUR OPENAI API KEY> |
| 73 | + ``` |
| 74 | + </CodeGroup> |
| 75 | + Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration) |
| 76 | + </span> |
| 77 | + </Step> |
| 78 | + <Step title="Run your Agent"> |
| 79 | + Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️ |
| 80 | + <Tip> |
| 81 | + After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard |
| 82 | + </Tip> |
| 83 | + <div/> |
| 84 | + <Frame type="glass" caption="Clickable link to session"> |
| 85 | + <img height="200" src="https://raw.githubusercontent.com/AgentOps-AI/agentops/refs/heads/main/docs/images/external/app_screenshots/session-replay.png?raw=true" /> |
| 86 | + </Frame> |
| 87 | + </Step> |
| 88 | +</Steps> |
| 89 | + |
| 90 | +## Full Examples |
| 91 | + |
| 92 | +<CodeGroup> |
| 93 | + ```python sync |
| 94 | + from openai import OpenAI |
| 95 | + import agentops |
| 96 | + |
| 97 | + agentops.init(<INSERT YOUR API KEY HERE>) |
| 98 | + client = OpenAI() |
| 99 | + |
| 100 | + response = client.chat.completions.create(( |
| 101 | + model="gpt-4o-mini", |
| 102 | + messages=[{ |
| 103 | + "role": "user", |
| 104 | + "content": "Write a haiku about AI and humans working together" |
| 105 | + }] |
| 106 | + ) |
| 107 | + |
| 108 | + print(response.choices[0].message.content) |
| 109 | + agentops.end_session('Success') |
| 110 | + ``` |
| 111 | + |
| 112 | + ```python async |
| 113 | + from openai import AsyncOpenAI |
| 114 | + import agentops |
| 115 | + import asyncio |
| 116 | + |
| 117 | + async def main(): |
| 118 | + agentops.init(<INSERT YOUR API KEY HERE>) |
| 119 | + client = AsyncOpenAI() |
| 120 | + |
| 121 | + response = await client.chat.completions.create( |
| 122 | + model="gpt-4o-mini", |
| 123 | + messages=[{ |
| 124 | + "role": "user", |
| 125 | + "content": "Write a haiku about AI and humans working together" |
| 126 | + }] |
| 127 | + ) |
| 128 | + |
| 129 | + print(response.choices[0].message.content) |
| 130 | + agentops.end_session('Success') |
| 131 | + |
| 132 | + asyncio.run(main()) |
| 133 | + ``` |
| 134 | + |
| 135 | +</CodeGroup> |
| 136 | + |
| 137 | +### Streaming examples |
| 138 | + |
| 139 | +<CodeGroup> |
| 140 | + ```python sync |
| 141 | + from openai import OpenAI |
| 142 | + import agentops |
| 143 | + |
| 144 | + agentops.init(<INSERT YOUR API KEY HERE>) |
| 145 | + client = OpenAI() |
| 146 | + |
| 147 | + stream = client.chat.completions.create( |
| 148 | + model="gpt-4o-mini", |
| 149 | + stream=True, |
| 150 | + messages=[{ |
| 151 | + "role": "user", |
| 152 | + "content": "Write a haiku about AI and humans working together" |
| 153 | + }], |
| 154 | + ) |
| 155 | + |
| 156 | + for chunk in stream: |
| 157 | + print(chunk.choices[0].delta.content or "", end="") |
| 158 | + |
| 159 | + agentops.end_session('Success') |
| 160 | + ``` |
| 161 | + |
| 162 | + ```python async |
| 163 | + from openai import AsyncOpenAI |
| 164 | + import agentops |
| 165 | + import asyncio |
| 166 | + |
| 167 | + async def main(): |
| 168 | + agentops.init(<INSERT YOUR API KEY HERE>) |
| 169 | + client = AsyncOpenAI() |
| 170 | + |
| 171 | + stream = await client.chat.completions.create( |
| 172 | + model="gpt-4o-mini", |
| 173 | + stream=True, |
| 174 | + messages=[{ |
| 175 | + "role": "user", |
| 176 | + "content": "Write a haiku about AI and humans working together" |
| 177 | + }], |
| 178 | + ) |
| 179 | + |
| 180 | + async for chunk in stream: |
| 181 | + print(chunk.choices[0].delta.content or "", end="") |
| 182 | + |
| 183 | + agentops.end_session('Success') |
| 184 | + |
| 185 | + asyncio.run(main()) |
| 186 | + ``` |
| 187 | + |
| 188 | +</CodeGroup> |
| 189 | + |
| 190 | +<script type="module" src="/scripts/github_stars.js"></script> |
| 191 | +<script type="module" src="/scripts/link_to_api_button.js"></script> |
| 192 | +<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script> |
| 193 | +<script type="module" src="/scripts/button_heartbeat_animation.js"></script> |
| 194 | +<script type="css" src="/styles/styles.css"></script> |
| 195 | +<script type="module" src="/scripts/adjust_api_dynamically.js"></script> |
0 commit comments