Function calling, how to query information from a previous function result

Hello, I have a situation that I can’t figure out how to solve.

I have a function that gets the sales of a period, it does a fetch to an api and returns an array of data that will be shown to the user.

    {
      type: "function",
      function: {
        name: "get_sales",
        description: "Get the sales",
        strict: true,
        parameters: {
          type: "object",
          properties: {
            company: {
              type: ["string"],
              description: "The company name. Available: Fravega",
            },
            date_from: {
              type: ["string"],
              description: "The starting date of the period, always assuming the first day of the month in question.",
            },
            date_to: {
              type: ["string"],
              description: "The end date of the period, always assuming the last day of the month in question.",
            },
          },
          additionalProperties: false,
          required: ["company", "date_from", "date_to"],
        },
      },
    },

This returns an array of sales when the function is executed, and is sent to the user as a table image. example [{category: “TV”, total: 500}]

I can not think of a way to make a query on the above data, or how is the best way or the common one.

Example case: How much is the total of the TVs category?

I need to identify that the user is referring to the previous dataset, but I can’t find the right way to do it.

Any help is appreciated!

This is the pattern of a user input that calls a function:

user: show me tv sales totals
assistant: functions.get_sales{“company”:“widgets”, …

the function performs its computations and calculations and actions, then returns a value that the AI can act on:

user: show me tv sales totals for widgets
assistant: functions.get_sales{“company”:“widgets”, …
tool:get_sales: SQL query auto-written returned: {TV: 500}
assistant: The widgets company sold 500, good job!

If you are giving an image, an actual picture, directly in the user interface, then I assume you are returning the function call output and the function return to the AI. Even just a message “image displayed in user interface”? This is the expected pattern of using functions.

Of course, if all the data in in an image the AI never received - how is going answer anything now or in the future? How would it know that the function even got called if you don’t build the accurate chat history?

Thus, the best practice would be to return “the user was shown an image with this data in their UI: {table}”

Hi J, thanks for your help, I thought about the way you are suggesting but the problem I have with that, is that the data that can be returned can be very large, like an array of 800 records, adding that to the history I don’t think is the best way, it could limit the information, but it is not what I am looking for. is there any way to handle this?

The idea is that the user can ask a question based on the previous dataset, but keep in mind that the dataset can be large.

Thank you very much

Let’s “implement” an alternate strategy…

Function return: “the user was shown an image with 150 results. You now have a tool that can query these result data fields most recently produced.”

tool = '{
  "name": "poll_getsales_results",
  "description": "Retrieve and sort sales results from last get_sales tool run on specified criteria.",
  "strict": true,
  "parameters": {
    "type": "object",
    "required": [
      "sort",
      "sort_field",
      "max_results"
    ],
    "properties": {
      "sort": {
        "type": "string",
        "description": "Sort order of the results",
        "enum": [
          "asc",
          "desc"
        ]
      },
      "sort_field": {
        "type": "string",
        "description": "Field to sort the results by",
        "enum": [
          "item",
          "count",
          "price"
        ]
      },
      "max_results": {
        "type": "number",
        "description": "Maximum number of results to return"
      }
    },
    "additionalProperties": false
  }
}'

Searching the results could have more query-like inputs for the AI to write also.