Learn how to integrate and use OpenAI’s API with Galileo’s wrapper client
When working with OpenAI’s API, it’s important to set up your environment and client correctly to ensure secure and efficient API calls. This guide shows you how to create a basic integration using Galileo’s OpenAI client wrapper.
The Galileo OpenAI wrapper currently only supports the synchronous chat completions API.
# Galileo Environment Variables# Your Galileo API keyGALILEO_API_KEY=your-galileo-api-key# Your Galileo project nameGALILEO_PROJECT="your-galileo-project-name"# The name of the log stream you want to use for loggingGALILEO_LOG_STREAM="your-galileo-log-stream"# Provide the console url below if you are using a# custom deployment, and not using the free tier, or app.galileo.ai.# This will look something like “console.galileo.yourcompany.com”.# GALILEO_CONSOLE_URL="your-galileo-console-url"# OpenAI propertiesOPENAI_API_KEY="your-openai-api-key"# Optional. The base URL of your OpenAI deployment. # Leave this commented out if you are using the default OpenAI API.# OPENAI_BASE_URL="your-openai-base-url-here"# Optional. Your OpenAI organization.# OPENAI_ORGANIZATION="your-openai-organization-here"
This assumes you are using a free Galileo account. If you are using a custom deployment, then you will also need to add the URL of your Galileo Console:
.env
Copy
Ask AI
GALILEO_CONSOLE_URL=your-Galileo-console-URL
2
Install Required Dependencies
Install the required dependencies for your app. If you are using Python, create a virtual environment using your preferred method, then install dependencies inside that environment:
Copy
Ask AI
pip install "galileo[openai]" python-dotenv
3
Initialize the Client
Import required packages
Load environment variables
Create OpenAI client instance with Galileo wrapper
4
Create a Chat Completion
Structure your prompt
Call the API using the client
Process and use the response
Copy
Ask AI
import osfrom galileo.openai import openaifrom dotenv import load_dotenvload_dotenv()client = openai.OpenAI( api_key=os.environ.get("OPENAI_API_KEY"), organization=os.environ.get("OPENAI_ORGANIZATION"))prompt = "Explain the following topic succinctly: Newton's First Law"response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": prompt}],)print(response.choices[0].message.content.strip())
Your logging is now set up! You are ready to configure metrics for your project.