Skip to content

Should FunctionCall be part of the Expression?  #124

@caoer

Description

@caoer

The following schema is introduced in #20.

// A program consists of a sequence of expressions that are evaluated in order.
export type Program = {
    "@steps": Expression[];
}

// An expression is a JSON value, a function call, or a reference to the result of a preceding expression.
export type Expression = JsonValue | FunctionCall | ResultReference;

// A JSON value is a string, a number, a boolean, null, an object, or an array. Function calls and result
// references can be nested in objects and arrays.
export type JsonValue = string | number | boolean | null | { [x: string]: Expression } | Expression[];

// A function call specifices a function name and a list of argument expressions. Arguments may contain
// nested function calls and result references.
export type FunctionCall = {
    // Name of the function
    "@func": string;
    // Arguments for the function
    "@args": Expression[];
};

// A result reference represents the value of an expression from a preceding step.
export type ResultReference = {
    // Index of the previous expression in the "@steps" array
    "@ref": number;
};

However, I don't think FunctionCall should be part of Expression type. Because { [x: string]: Expression } is part of JsonValue. The output of gpt model is "@steps": Expression[], the execution order is determined. Because when we allow FunctionCall in an object-like value, the execution order is undetermined. Example:

with FunctionCall

{
  "@steps": [
    {
      "@func": "func1",
      "@args": [
        {
          "a": {
            "@func": "func2",
            "args": []
          },
          "b": {
            "@func": "func3",
            "args": []
          }
        }
      ]
    }
  ]
}

we don't know the execution order of func2 vs func3

with Reference

{
  "@steps": [
    {
      "@func": "func3",
      "args": []
    },
    {
      "@func": "func2",
      "args": []
    },
    {
      "@func": "func1",
      "@args": [
        {
          "a": {
            "@ref": 0
          },
          "b": {
            "@ref": 1
          }
        }
      ]
    }
  ]
}

func3 is executed first, then func2.


I'm not sure in what scenarios the gpt model can produce @steps with reference to FunctionCall, in the tests I run, only ResultReference is produced. Is gpt model able to understand the difference between reference to the result of previous functioncall and execute every times it is evaluated?

I think it would be 'safer' to design the generic schema as deterministic as possible. For example, in a financial application, the execution orders matters for the state mutation for user's funds.

therefore, a more strict schema version is:

export type Program = {
    "@steps": FunctionCall[];
}

export type Expression = JsonValue | ResultReference;

export type JsonValue = string | number | boolean | null | { [x: string]: Expression } | Expression[];

export type FunctionCall = {
    "@func": string;
    "@args": Expression[];
};

export type ResultReference = {
    "@ref": number;
};

@ahejlsberg @steveluc does it make sense? Have you seen any example with including FunctionCall in Expression?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions