OpenAI
Neatlogs offers seamless integration with OpenAI, allowing you to track and analyze all your OpenAI API calls automatically.
Installation
To get started with OpenAI, you'll need to install the package:
pip install neatlogs openai
Setting Up API Keys
Before using OpenAI with Neatlogs, you need to set up your API keys. You can obtain:
OPENAI_API_KEY
: From the OpenAI PlatformPROJECT_API_KEY
: From your Neatlogs Dashboard
Then to set them up, you can either export them as environment variables or set them in a .env
file:
OPENAI_API_KEY="your_openai_api_key_here"
Then load the environment variables in your Python code:
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
os.getenv("OPENAI_API_KEY")
Usage
Once you've set up your OpenAI integration, integrating Neatlogs takes just two lines of code:
import neatlogs
neatlogs.init(api_key="<YOUR_API_KEY>")
Examples
Here's a simple example of how to use OpenAI with Neatlogs:
import neatlogs
from openai import OpenAI
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Initialize Neatlogs
neatlogs.init(api_key=os.getenv("NEATLOGS_API_KEY"))
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Make a chat completion request
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
]
)
print(response.choices[0].message.content)
After that, every API call is automatically traced and visualized in Neatlogs, perfect for debugging, evaluating and collaborating.
For more information on OpenAI, check out their comprehensive documentation.