Guides
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.
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.
1import { OpenAIAdapter } from '@commercegateway/commerce-gateway/openai';2 3const adapter = new OpenAIAdapter({4 backends: { products: myBackend },5 tools: ['search_products', 'get_product_details'],6});Pass the generated tools to the OpenAI chat completions API:
1const response = await openai.chat.completions.create({2 model: 'gpt-4-turbo',3 messages: [{ role: 'user', content: 'Do you have any blue shirts?' }],4 tools: adapter.getOpenAITools(), "cmt">// Automatically generated5});When the model decides to call a tool, use the gateway to execute it and return the result:
1if (response.choices[0].message.tool_calls) {2 const toolCall = response.choices[0].message.tool_calls[0];3 const result = await adapter.executeTool(toolCall);4 5 "cmt">// Send result back to OpenAI6 const finalResponse = await openai.chat.completions.create({7 model: 'gpt-4-turbo',8 messages: [9 ...messages,10 response.choices[0].message,11 {12 role: 'tool',13 tool_call_id: toolCall.id,14 content: JSON.stringify(result),15 },16 ],17 });18}If you want to create a "Store Assistant" GPT on the OpenAI Store:
https://your-gateway.com/openapi.json.gpt-4-turbo or newer for the most reliable tool calling.