This document provides a guide on how to use the OpenAI API within the Valohai platform.
Not Primary Usecase
Valohai is built for experimentation and the creation of innovative AI solutions. While we showcase the use of OpenAI API here, please note this is not the primary recommended use case for our platform.
Prerequisites
- OpenAI Account: Ensure you have an OpenAI account. similar services.
- Funding: Add funds to your OpenAI account to enable API access.
- API Token: Obtain an API token from OpenAI.
Configuration
Set Up Environment Variable: The API token should be set as an environment variable within Valohai as OPENAI_API_KEY
.
This can be done in two ways:
- Execution Environment Variable: Add the token when creating an execution from the UI under “Create Execution -> Environment Variables.” This variable will be available only for the execution where it was created.
- Project Environment Variable: For broader access, add it under “Project Settings -> Environment Variables” tab and check the “Secret” checkbox. This makes the variable available across all project executions.
Project Setup
Create a New Project: Initialize a new project on Valohai and define your execution step in the valohai.yaml
configuration file.
``` yaml
- step:
name: openai-test
image: python:3.9
command:
- pip install --upgrade openai
- python run-openai.py
```
Executing Your First API Call
Run the API Call: Use the following Python script run-openai.py
to make a call to the OpenAI API.
``` python
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
]
)
print(completion.choices[0].message)
```
You can get more examples of API calls in the OpenAI documentation.