Skip to main content
The LLM Gateway makes it easy to expose your commerce capabilities as tools for OpenAI’s GPT models. This allows you to build custom GPTs or integrate shopping into your own ChatGPT-powered applications.

Overview

OpenAI uses “Function Calling” to allow the model to interact with external APIs. The gateway automatically converts your Zod-based tool definitions into the JSON Schema format that OpenAI expects.

Usage with OpenAI SDK

1. Setup the OpenAI Adapter

import { OpenAIAdapter } from '@betterdata/commerce-gateway/openai';

const adapter = new OpenAIAdapter({
  backends: { products: myBackend },
  tools: ['search_products', 'get_product_details'],
});

2. Generate Tool Definitions

Pass the generated tools to the OpenAI chat completions API:
const response = await openai.chat.completions.create({
  model: 'gpt-4-turbo',
  messages: [{ role: 'user', content: 'Do you have any blue shirts?' }],
  tools: adapter.getOpenAITools(), // Automatically generated
});

3. Handle Tool Calls

When the model decides to call a tool, use the gateway to execute it and return the result:
if (response.choices[0].message.tool_calls) {
  const toolCall = response.choices[0].message.tool_calls[0];
  const result = await adapter.executeTool(toolCall);
  
  // Send result back to OpenAI
  const finalResponse = await openai.chat.completions.create({
    model: 'gpt-4-turbo',
    messages: [
      ...messages,
      response.choices[0].message,
      {
        role: 'tool',
        tool_call_id: toolCall.id,
        content: JSON.stringify(result),
      },
    ],
  });
}

Creating a Custom GPT

If you want to create a “Store Assistant” GPT on the OpenAI Store:
  1. Deploy your LLM Gateway to a public URL (e.g., using Vercel).
  2. In the GPT Editor, go to Actions -> Create new action.
  3. Import the OpenAPI schema generated by the gateway at https://your-gateway.com/openapi.json.
  4. Configure your API key for authentication.

Best Practices

  • Model Selection: Use gpt-4-turbo or newer for the most reliable tool calling.
  • System Prompt: Give the model clear instructions on when and how to use the commerce tools.
  • Error Handling: OpenAI is sensitive to malformed tool responses. The gateway handles this by always returning structured JSON even on failure.