AgentOps provides first class support for OpenAI’s GPT family of models
OpenAI is a leading provider of AI tools and services.
Explore the OpenAI API for more information.
1
Install the AgentOps SDK
pip install agentops
poetry add agentops
2
Install the OpenAI SDK
openai<1.0.0 has limited support while openai>=1.0.0 is continuously supported.
pip install openai
poetry add openai
To install openai<1.0.0, use the following:
pip install "openai<1.0.0"
poetry add "openai<1.0.0"
3
Add 3 lines of code
Make sure to call agentops.init before calling any openai, cohere, crew, etc models.
import agentopsfrom openai import OpenAIagentops.init(<INSERT YOUR API KEY HERE>)client = OpenAI()...# End of program (e.g. main.py)agentops.end_session("Success")
Set your API key as an .env variable for easy access.
AGENTOPS_API_KEY=<YOUR API KEY>OPENAI_API_KEY=<YOUR OPENAI API KEY>
from openai import OpenAIimport agentopsagentops.init(<INSERT YOUR API KEY HERE>)client = OpenAI()response = client.chat.completions.create( model="gpt-4o-mini", messages=[{ "role": "user", "content": "Write a haiku about AI and humans working together" }])print(response.choices[0].message.content)agentops.end_session('Success')
from openai import AsyncOpenAIimport agentopsimport asyncioasync def main(): agentops.init(<INSERT YOUR API KEY HERE>) client = AsyncOpenAI() response = await client.chat.completions.create( model="gpt-4o-mini", messages=[{ "role": "user", "content": "Write a haiku about AI and humans working together" }] ) print(response.choices[0].message.content) agentops.end_session('Success')asyncio.run(main())
from openai import OpenAIimport agentopsagentops.init(<INSERT YOUR API KEY HERE>)client = OpenAI()stream = client.chat.completions.create( model="gpt-4o-mini", stream=True, messages=[{ "role": "user", "content": "Write a haiku about AI and humans working together" }],)for chunk in stream: print(chunk.choices[0].delta.content or "", end="")agentops.end_session('Success')
from openai import AsyncOpenAIimport agentopsimport asyncioasync def main(): agentops.init(<INSERT YOUR API KEY HERE>) client = AsyncOpenAI() stream = await client.chat.completions.create( model="gpt-4o-mini", stream=True, messages=[{ "role": "user", "content": "Write a haiku about AI and humans working together" }], ) async for chunk in stream: print(chunk.choices[0].delta.content or "", end="") agentops.end_session('Success')asyncio.run(main())
Assistants example
You can find the example in the Assistants section.