Skip to content

Latest commit

 

History

History

020-functions-dotnet

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

OpenAI API Function Calling

Introduction

This example introduces you to the concept of function calling:

  • How to make your application's functions available to the OpenAI API.
  • Processing function calls
    • Handling arguments
    • Returning results
  • Error handling

Exercises

  • Make yourself familiar with the code in FunctionCallingDotNet.

  • Try the following prompts in a coversation and try to understand what OpenAI does and why:

    I am going to visit Carolyn Farino tomorrow. Tell me something about her and the products that she usually buys.
    Did she ever buy a headset?
    Give me a table by year and month of her revenues.
  • Think about an application of function calling that would be relevant to your business or personal life.

  • Try to ask a question that the OpenAI API cannot answer with the given functions. What happens?

Advanced Exercises

  • Add a new function to the application and try to design a prompt that makes ChatGPT call this function.
    • Example: Return details about order header and order details for a given customer and product.
  • Quite hard: Use streaming to receive messages from the OpenAI API step by step.

More Sample Data

Note that data in the AdventureWorksLT sample is quite limited by default. Here is a script that you can use as a basis to generate more data (of course it was generated by GitHub Copilot 😉):

DECLARE @SalesOrderID int = 100000;
DECLARE @ProductID int;
DECLARE @ListPrice money;
DECLARE @OrderDate datetime;
DECLARE @DueDate datetime;
DECLARE @SubTotal money;
DECLARE @TaxAmt money;
DECLARE @Freight money = 5;

DECLARE @CustomerID int;
DECLARE CustomerCursor CURSOR FOR
SELECT CustomerID FROM SalesLT.Customer;

OPEN CustomerCursor;

FETCH NEXT FROM CustomerCursor INTO @CustomerID;

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @i int = 0;
    WHILE @i < 10
    BEGIN
        -- Select a random product
        SELECT TOP 1 @ProductID = ProductID, @ListPrice = ListPrice FROM SalesLT.Product ORDER BY NEWID();

        -- Generate a random date in 2023
        SET @OrderDate = DATEADD(DAY, (ABS(CHECKSUM(NEWID())) % 365), '2023-01-01');
        SET @DueDate = DATEADD(DAY, 5, @OrderDate);

        -- Calculate SubTotal and TaxAmt
        SET @SubTotal = @ListPrice;
        SET @TaxAmt = @SubTotal * 0.1;

        -- Insert into SalesOrderHeader
        INSERT INTO SalesLT.SalesOrderHeader
        (SalesOrderID, RevisionNumber, OrderDate, DueDate, Status, OnlineOrderFlag, CustomerID, ShipMethod, SubTotal, TaxAmt, Freight)
        VALUES
        (@SalesOrderID, 0, @OrderDate, @DueDate, 1, 0, @CustomerID, 'Courier', @SubTotal, @TaxAmt, @Freight);

        -- Insert into SalesOrderDetail
        INSERT INTO SalesLT.SalesOrderDetail
        (SalesOrderID, OrderQty, ProductID, UnitPrice, UnitPriceDiscount)
        VALUES
        (@SalesOrderID, 1, @ProductID, @ListPrice, 0);

        SET @SalesOrderID = @SalesOrderID + 1;
        SET @i = @i + 1;
    END;

    FETCH NEXT FROM CustomerCursor INTO @CustomerID;
END;

CLOSE CustomerCursor;
DEALLOCATE CustomerCursor;