Building an AI-Powered Product Discovery Tool using Function Calling

Rehmat Sayany
3 min readJan 22, 2025

--

Function Calling AI Agent

Introduction

In this blog, we’ll walk through building an AI-powered Product Discovery Tool that enables non-technical business users to retrieve critical product information across various data sources using natural language. The tool leverages OpenAI’s function calling capabilities to execute backend queries, providing seamless access to complex data.

Overview of the Tool

This tool is designed to solve key challenges faced by business teams:

  1. Accessing Stock Availability: Quickly check available stock for a specific product variant.
  2. Localized Product Details: Retrieve market-specific product details, such as localized titles or configurations.
  3. Product Metadata: Extract additional metadata like shipping size, SKUs, and more.

System Architecture

Here’s a high-level overview of how the tool works:

AI Agent For Product Discovery

Implementation

Core Code

Here’s how the tool is implemented using JavaScript and OpenAI’s API.

1. Function Definitions

We define a function to handle specific backend queries, such as fetching product details. you can define as many functions you want but remember to add description as model will use this description to know which function call need to be done.

const tools = [
{
name: "get_product_details",
description: "Fetch product details including available stock, title, and handle based on variant ID.",
parameters: {
type: "object",
properties: {
variantId: { type: "string", description: "The ID of the product variant" },
environment: { type: "string", description: "The environment (e.g., 'main-prod')" },
country: { type: "string", description: "The country code (e.g., 'DE')" },
},
required: ["variantId", "environment", "country"],
},
}
];

2. Fetching Product Details

The fetchProductDetailfunction interacts with Rest API to fetch product details

const fetchProductDetail = async (variantId, environment, country) => {
// Simulated Rest API call
return data;
};

3. Calling OpenAI’s Chat Completion API

The callOpenAIChat function processes user queries and executes the appropriate backend function.

async function callOpenAIChat(userMessage, model = "gpt-3.5-turbo") {
try {
const messages = [
{ role: "system", content: "You are a helpful assistant for Product Discovery." },
{ role: "user", content: userMessage },
];

const response = await openai.chat.completions.create({
model,
messages,
tools,
});
if (response.choices[0].message.tool_calls) {
const toolCall = response.choices[0].message.tool_calls[0];
const args = JSON.parse(toolCall.function.arguments);
let result;

if (toolCall.function.name === "get_product_details") {
const {variantId, environment, country } = args;
result = await fetchProductDetail(variantId, environment, country);
}
messages.push(
response.choices[0].message,
{ role: "tool", tool_call_id: toolCall.id, content: JSON.stringify(result) }
);
const finalResponse = await openai.chat.completions.create({
model,
messages,
tools,
});
return finalResponse.choices[0].message.content;
}
} catch (error) {
console.error("Error calling OpenAI API:", error);
throw new Error("Failed to get a response from OpenAI.");
}
}

Example Usage

// 1st Example usage
(async () => {
const userMessage = "What's the shipping size of this Product Variant 123 in Germany?";
const response = await callOpenAIChat(userMessage);
console.log("AI Response:", response);
})();

// AI Response: The shipping size of the variant with ID 123 in Germany is 2 cubic meters (2MH).


// 2nd Example usage
(async () => {
const userMessage = "What's the avaialble quantity for this product variant 123 , country is netherland ?";
const response = await callOpenAIChat(userMessage, "gpt-3.5-turbo");
console.log("AI Response:", response);
})();
// AI Response: The available quantity for the product "X Furniture Design" in the Netherlands is 52.

Summary of Function Calling Flow:

  1. Define the function like get_product_details in the tools array.
  2. The user sends a query to the AI model, asking for something that requires external data.
  3. AI processes the query and determines it needs to call an external function. In the above case, the Model determines that it need to call get_product_details function.
  4. Model sends a response with a function call (including the function name and arguments).
  5. You extract the arguments and call the appropriate function (e.g. getProductDetails.
  6. Return the function result to OpenAI and send it as part of the final response.
  7. AI generates the final response, including its reasoning and the function result.

Conclusion

This tool simplifies data retrieval for non-technical users by combining natural language processing with function calling. It integrates with backend systems to execute complex queries like fetching product details, stock availability, and SKUs. The result is a powerful and intuitive interface that enhances productivity and decision-making.

--

--

Rehmat Sayany
Rehmat Sayany

Written by Rehmat Sayany

Full Stack developer @westwing passionate about NodeJS, TypeScript, React JS and AWS.

No responses yet