Skip to content

Latest commit

 

History

History
223 lines (170 loc) · 7.08 KB

AnalyzeTextDataUsingParallelFunctionCallwithChatGPT.md

File metadata and controls

223 lines (170 loc) · 7.08 KB

Analyze Text Data Using Parallel Function Calls with ChatGPT™

To run the code shown on this page, open the MLX file in MATLAB®: mlx-scripts/AnalyzeTextDataUsingParallelFunctionCallwithChatGPT.mlx

This example shows how to detect multiple function calls in a single user prompt and use this to extract information from text data.

Function calls allow you to describe a function to ChatGPT in a structured way. When you pass a function to the model together with a prompt, the model detects how often the function needs to be called in the context of the prompt. If the function is called at least once, then the model creates a JSON object containing the function and argument to request.

This example contains four steps:

  • Create an unstructured text document containing fictional customer data.
  • Create a prompt, asking ChatGPT to extract information about different types of customers.
  • Create a function that extracts information about customers.
  • Combine the prompt and the function. Use ChatGPT to detect how many function calls are included in the prompt and to generate a JSON object with the function outputs.

To run this example, you need a valid API key from a paid OpenAI™ API account.

Extracting data from text

The customer record contains fictional information.

record = ["Customer John Doe, 35 years old. Email: [email protected]";
    "Jane Smith, age 28. Email address: [email protected]";
    "Customer named Alex Lee, 29, with email [email protected]";
    "Evelyn Carter, 32, email: [email protected]";
    "Jackson Briggs is 45 years old. Contact email: [email protected]";
    "Aria Patel, 27 years old. Email contact: [email protected]";
    "Liam Tanaka, aged 28. Email: [email protected]";
    "Sofia Russo, 24 years old, email: [email protected]"];

Define the function that extracts data from the customer record.

f = openAIFunction("extractCustomerData", "Extracts data from customer records");
f = addParameter(f, "name", type="string", description="customer name", RequiredParameter=true);
f = addParameter(f, "age", type="number", description="customer age");
f = addParameter(f, "email", type="string", description="customer email", RequiredParameter=true);

Create a message with the customer record and an instruction.

record = join(record);
messages = messageHistory;
messages = addUserMessage(messages,"Extract data from the record: " + record);

Create a chat object. Specify the model to be "gpt-4o-mini", which supports parallel function calls.

model = "gpt-4o-mini";
chat = openAIChat("You are an AI assistant designed to extract customer data.","ModelName",model,Tools=f);

Generate a response and extract the data.

[~, singleMessage, response] = generate(chat,messages);
if response.StatusCode == "OK"
    funcData = [singleMessage.tool_calls.function];
    extractedData = arrayfun(@(x) jsondecode(x.arguments), funcData);
    extractedData = struct2table(extractedData)
else
    response.Body.Data.error
end
name age email
1 'John Doe' 35 '[email protected]'
2 'Jane Smith' 28 '[email protected]'
3 'Alex Lee' 29 '[email protected]'
4 'Evelyn Carter' 32 '[email protected]'
5 'Jackson Briggs' 45 '[email protected]'
6 'Aria Patel' 27 '[email protected]'
7 'Liam Tanaka' 28 '[email protected]'
8 'Sofia Russo' 24 '[email protected]'

Calling an external function

In this example, use a local function searchCustomerData defined at the end of this script.

Create a message with the information you would like to get from the local function.

prompt = "Who are our customers under 30 and older than 27?";
messages = messageHistory;
messages = addUserMessage(messages,prompt);

Define the function that retrieves weather information for a given city based on the local function.

f = openAIFunction("searchCustomerData", "Get the customers who match the specified and age");
f = addParameter(f,"minAge",type="integer",description="The minimum customer age",RequiredParameter=true);
f = addParameter(f,"maxAge",type="integer",description="The maximum customer age",RequiredParameter=true);

Create a chat object.

model = "gpt-4o-mini";
chat = openAIChat("You are an AI assistant designed to search customer data.",ModelName=model,Tools=f);

Generate a response

[~, singleMessage, response] = generate(chat,messages);

Check if the response contains a request for tool calling.

if isfield(singleMessage,'tool_calls')
    tcalls = singleMessage.tool_calls
else
    response.Body.Data.error
end
tcalls = struct with fields:
          id: 'call_t4ECuPMLP5mCuRCDG8ZnyjDY'
        type: 'function'
    function: [1x1 struct]

And add the tool call to the messages.

messages = addResponseMessage(messages,singleMessage);

Call searchCustomerData function and add the results to the messages

messages = processToolCalls(extractedData,messages, tcalls);

Step 3: extend the conversation with the function result.

[txt, singleMessage, response] = generate(chat,messages);
if response.StatusCode == "OK"
    txt
else
    response.Body.Data.error
end
txt = 
    "Here are the customers who are under 30 and older than 27:
     
1. **Jane Smith**
        - Age: 28
        - Email: [email protected]
     
     2. **Alex Lee**
        - Age: 29
        - Email: [email protected]
     
     3. **Liam Tanaka**
        - Age: 28
        - Email: [email protected]"

Helper functions

Function that searches specific customers based on age range

function json = searchCustomerData(data, minAge, maxAge)
    result = data(data.age >= minAge & data.age <= maxAge,:);
    result = table2struct(result);
    json = jsonencode(result);
end

Function that uses the tool calls to execute the function and add the results to the messages

function msg = processToolCalls(data, msg, toolCalls)
    for ii = 1:numel(toolCalls)
        funcId = string(toolCalls(ii).id);
        funcName = string(toolCalls(ii).function.name);
        if funcName == "searchCustomerData"
            funcArgs = jsondecode(toolCalls(ii).function.arguments);
            keys = fieldnames(funcArgs);
            vals = cell(size(keys));
            if ismember(keys,["minAge","maxAge"])
                for jj = 1:numel(keys)
                     vals{jj} = funcArgs.(keys{jj});
                end             
                try
                    funcResult = searchCustomerData(data,vals{:});
                catch ME
                    error(ME.message)
                    return
                end
                msg = addToolMessage(msg, funcId, funcName, funcResult);
            else
                error("Unknown arguments provided: " + join(keys))
                return
            end
        else
            error("Unknown function called: " + funcName)
            return
        end
    end
end

Copyright 2024-2025 The MathWorks, Inc.