---
title: "OpenAI Integration"
description: "Learn how to use OpenAI with Pezzo."
---

## Using OpenAI With Pezzo

Ensure that you have the latest version of the Pezzo Client installed, as well as the OpenAI NPM package.

<Tabs>
  <Tab title="Node.js">
    <CodeGroup>
      ```bash npm
      npm i @pezzo/client openai
      ```
      ```bash yarn
      yarn add @pezzo/client openai
      ```
      ```bash pnpm
      pnpm add @pezzo/client openai
      ```
    </CodeGroup>
  </Tab>
  <Tab title="Python">
    <CodeGroup>
      ```bash pip
      pip install pezzo
      ``` 
      ```bash poetry
      poetry add pezzo
      ```
    </CodeGroup>
  </Tab>
</Tabs>

### Initialize Pezzo and PezzoOpenAI

Learn more about how to initialize the Pezzo Client:
- [Node.js](/client/pezzo-client-node)
- [Python](/client/pezzo-client-python)

### Making Requests to OpenAI

#### Option 1: With Prompt Management (Recommended)

We recommend you to manage your AI prompts through Pezzo. This allows you to easily manage your prompts, and keep track of your AI requests. [Click here to learn about Prompt Management in Pezzo](platform/prompt-management).

Below is an example of how you can use Pezzo to retrieve a prompt, and then use it to make a request to OpenAI.

<Tabs>
  <Tab title="Node.js">
```ts
  // Fech prompt from Pezzo
  const prompt = await pezzo.getPrompt("PromptName");

  // Provide the prompt as-is to OpenAI
  const response = await openai.chat.completions.create(prompt);

  // Or you can override specific properties if you wish
  const response = await openai.chat.completions.create({
    ...prompt,
    model: "gpt-4",
  });
```
  </Tab>
  <Tab title="Python">
```py
  from pezzo.client import pezzo
  from pezzo.openai import openai

  # Fetch prompt from Pezzo
  prompt = pezzo.get_prompt("PromptName")

  # Provide the prompt to OpenAI
  response = openai.ChatCompletion.create(
    pezzo_prompt=prompt
  )

  # You can override specific properties if you wish
  response = openai.ChatCompletion.create(
    pezzo_prompt=prompt,
    model="gpt-4"
  )
```
  </Tab>
</Tabs>

Congratulations! You've about to benefit from seamless prompt version management and request tracking. Your request will now be visible in the **Requests** page of your Pezzo project.

#### Option 2: Without Prompt Management

If you don't want to manage your prompts through Pezzo, you can still use Pezzo to make requests to OpenAI and benefit from Pezzo's [Observability features](platform/observability/overview).

You will consume the make request to the OpenAI exactly as you normally would. The only difference is that you will use the `PezzoOpenAI` instance we created above. Here is an example:

<Tabs>
  <Tab title="Node.js">
```ts
const response = await openai.chat.completions.create({
  model: "gpt-3.5-turbo",
  temperature: 0,
  messages: [
    {
      role: "user",
      content: "Hey, how are you doing?",
    },
  ],
});
```
  </Tab>
  <Tab title="Python">
```py
from pezzo.client import pezzo
from pezzo.openai import openai

response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  temperature=0,
  messages=[
    {
      "role": "user",
      "content": "Hey, how are you doing?",
    }
  ]
)
```
  </Tab>
</Tabs>

You should now be able to see your request in the **Requests** page of your Pezzo project.

### Additional Capabilities

The Pezzo client enhances your developer experience by providing additional functionality to the OpenAI API. This is done through the second argument of the `createChatCompletion` method.

#### Variables

You can specify variables that will be interpolated by the Pezzo client before sending the request to OpenAI. This is useful if you want to use the same prompt for multiple requests, but with different variables.

<Tabs>
  <Tab title="Node.js">
```ts
const response = await openai.chat.completions.create(..., {
  variables: {
    age: 22,
    country: "France"
  }
});
```
  </Tab>
  <Tab title="Python">
```py
response = openai.ChatCompletion.create(
  ...,
  pezzo_options={
    "variables": {
      "age": 22,
      "country": "France"
    }
  }
)
```
  </Tab>
</Tabs>

Notice the variables in the prompt. The Pezzo client will replace them with the values you specified in the `variables` object.

#### Custom Properties

You can also specify custom properties that will be sent to Pezzo. This is useful if you want to add additional information to your request, such as the user ID, or the request ID. This information will be visible in the **Requests** page of your Pezzo project, and you will be able to filter requests based on these properties.

<Tabs>
  <Tab title="Node.js">
```ts
const response = await openai.chat.completions.create({
  ...
}, {
  properties: {
    userId: "some-user-id",
    traceId: "some-trace-id"
  }
});
```
  </Tab>
  <Tab title="Python">
```py
response = await openai.ChatCompletion.create(
  ...,
  pezzo_options={
    "properties": {
      "userId": "some-user-id",
      "traceId": "some-trace-id"
    }
  }
)
```
  </Tab>
</Tabs>


#### Request Caching

Utilizing request caching can sometimes save up to 90% on your API costs and execution time. You can enable cache by setting `cache` to `true` in the second argument of the `createChatCompletion` method.

<Tabs>
  <Tab title="Node.js">
```ts
const response = await openai.chat.completions.create({
  ...
}, {
  cache: true
});
```
  </Tab>
  <Tab title="Python">
```py
response = await openai.ChatCompletion.create(
  ...,
  pezzo_options={
    "cache": True
  }
)
```
  </Tab>
</Tabs>

To learn more, visit the [Request Caching](/client/request-caching) page.