OpenAI API: Program AI-based chats, sales assistants & text adventures

Expansion of the OpenAI API functions through additional parameters

All videos of the tutorial OpenAI API: Program AI-based chats, sales assistants & text adventures

In today's tutorial, you will learn how to add another parameter to your function call to make your queries more targeted. We will focus on the function f products by type and integrate an additional parameter called priceRange . With this parameter, you can specify specific price categories for electric guitars to find the products that match your budget.

Main Takeaways

  • Using additional parameters, such as priceRange, allows you to have more control over your queries.
  • A Map structure is used to filter products into price groups (low, medium, high).
  • The function call is adjusted to accommodate the new parameters so that the AI can retrieve the desired information.

Step-by-Step Guide

First, you will modify the function f products by type to include the new parameter priceRange.

Extension of the OpenAI API functions through additional parameters

In the first step, you will define the Map containing the price groups. This Map is named as price range to products and includes the categories "low," "mid," and "high." For the "low" price, we have some products, ultimately two in the mid-price range, and a few in the high category.

You should update the price information accordingly. Instead of 899.99€, which may have been a bit high, you will now use, for example, 699.99€. This is a more realistic price estimate for beginner guitars.

Expansion of OpenAI API functions through additional parameters

Next, you will go to your function description for f products by type. You already have the parameter for the product type, and now you will add the new parameter priceRange as a String type.

Ensure the description for the new parameter is clear: "the range in which the price of the product falls should be one of low, mid, high." This helps the AI interpret the parameter correctly.

Expansion of OpenAI API functions through additional parameters

Now, it is time to adjust the method for handling your function call. The price range must be integrated into the logic that filters the products.

Expansion of the OpenAI API functions through additional parameters

You will implement a filtering criterion that ensures only the products within the specified price range are considered. To do this, you will check the product IDs in relation to the price category.

Expansion of OpenAI API functions through additional parameters

Only the products associated with the respective price range will then flow into the map function to extract the relevant product descriptions.

After verifying that the length of the found products is not zero, you will check if products are actually available in the specified price range. If not, you will return a response that informs the user that no products were found.

Expansion of the OpenAI API functions through additional parameters

If products are available, you can use a simpler method like join to format the return, instead of reduce, which simplifies the code.

Extension of the OpenAI API functions through additional parameters

To check if everything is working, enter a query like "Do you have a low budget guitar?" This should return you the cheapest available guitars.

Expansion of the OpenAI API functions with additional parameters

If you search for a "low budget eBay" product and find none, the AI will inform you that nothing is available at the moment. This demonstrates that your algorithm is working correctly.

Expansion of the OpenAI API functions with additional parameters

Also, check if there are products in the mid-price range. Ask the function for electric guitars in the mid-price range and ensure that the results meet your expectations.

Extension of the OpenAI API functions through additional parameters

When you finally ask for the most expensive guitar, the function should also return this correctly to you.

Expansion of the OpenAI API functions through additional parameters

After you have successfully completed these steps, you have now set up your first function call with a second parameter. This opens up many possibilities for future functions.

Expansion of the OpenAI API functions through additional parameters

You can also add more parameters, such as the number of pickups or pages, to create even more specific queries.

Expansion of OpenAI API functions through additional parameters

These adjustments are crucial to ensure that the AI outputs the results as you need them.

Summary

In this guide, you have learned how to integrate a priceRange parameter into your function calls and what to consider in the process. Although it is a simple parameter, it allows you to formulate more precise search queries and enhance the user experience.

Frequently Asked Questions

How do I add a new parameter to my function?You can specify a new parameter in the function definition and ensure that it is used correctly in the function's logic.

Why is it important to define price categories?Defining price categories allows for targeted selection of products and increases user satisfaction, as they receive product recommendations according to their budget.

How can I check the functional results?Test the function by making different inputs and observing the returns to ensure that the filtering function is working correctly.