Python Documentation for OpenAI Legacy SDK

Below is the documentation for integrating Reconify with OpenAI's Legacy SDK (prior to Nov 2023) via Python PIP module. For integration with the latest version, please view the current Python integration docs.

Currently we support Chat, Completion, and Image actions on OpenAI.

Get started - Create an account

The first step is to create an account at app.reconify.com.

Generate API and APP Keys

In the Reconify console, add an Application to your account. This will generate both an API_KEY and an APP_KEY which will be used in the code below to send data to Reconify.

Python Integration

The easiest way to get started is to use the PIP module.

Install the module

pip install reconify

Import the module

from reconify import reconifyOpenAILegacyHandler

Initialize the module

Prior to initializing the Reconify module, make sure to import and initialize the OpenAI module

import openai
openai.api_key = "YOUR_OPENAI_KEY"

Configure the instance of Reconify passing the OpenAI instance along with the Reconify API_KEY and APP_KEY created above.

reconifyOpenAILegacyHandler.config(openai,  
appKey = "YOUR_APP_KEY",
apiKey = "YOUR_API_KEY",
)

This is all that is needed for the basic integration. The module takes care of the rest when you call either openai.Completion.create or openai.ChatCompletion.create.

Optional initialization parameters

You can optionally turn on "debug" mode by passing in "debug = True" in the method above. This will print debug messages to the console.

You can also disable image tracking, by passing in "trackImages = False" in the method.

reconifyOpenAILegacyHandler.config(openai,  
appKey = "YOUR_APP_KEY",
apiKey = "YOUR_API_KEY",
debug = True,
)

Optional methods

You can optionally pass in a user object or session ID to be used in the analytics reporting. The session ID will be used to group interactions together in the same session transcript.

The user object should include a unique userId, the other fields are optional.

reconifyOpenAILegacyHandler.setUser ({
  "userId": "123",
  "isAuthenticated": 1,
  "firstName": "Francis",
  "lastName": "Smith",
  "email": "",
  "phone": "",
  "gender": "female"
});

The session ID is a simple string.

reconifyOpenAILegacyHandler.setSession('MySessionId');

Chat Example

import os
import openai
from reconify import reconifyOpenAILegacyHandler

openai.api_key = 'YOUR_OPENAI_KEY'

reconifyOpenAILegacyHandler.config(openai,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyOpenAILegacyHandler.setUser({
 "userId": "12345",
 "isAuthenticated": 1,
 "firstName": "Jim",
 "lastName": "Stand",
 "gender": "male"
})

response = openai.ChatCompletion.create(
 model="gpt-3.5-turbo",
 messages=[
   {"role": "system", "content": "You are an expert on commedians."},
   {"role": "user", "content": "Tell a joke about cats"},
 ],
 temperature=0,
)

Completion Example

import os
import openai
from reconify import reconifyOpenAILegacyHandler

openai.api_key = 'YOUR_OPENAI_KEY'

reconifyOpenAILegacyHandler.config(openai,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyOpenAILegacyHandler.setUser({
 "userId": "12345",
 "isAuthenticated": 1,
 "firstName": "Jim",
 "lastName": "Stand",
 "gender": "male"
})

response = openai.Completion.create(
 model="text-davinci-003",
 prompt="write a haiku about cats",
 max_tokens: 100,
 temperature=0,
)

Image Example

import os
import openai
from reconify import reconifyOpenAILegacyHandler

openai.api_key = 'YOUR_OPENAI_KEY'

reconifyOpenAILegacyHandler.config(openai,
 appKey = 'Your_App_Key',
 apiKey = 'Your_Api_Key'
)

reconifyOpenAILegacyHandler.setUser({
 "userId": "12345",
 "isAuthenticated": 1,
 "firstName": "Jim",
 "lastName": "Stand",
 "gender": "male"
})

response = openai.Image.create(
 prompt = "a cat on the moon",
 n = 1,
 size = "256x256",
 response_format = "url"
)