Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genai: design for automatic function calling #75

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

jba
Copy link
Collaborator

@jba jba commented Mar 25, 2024

This PR is a suggested design for automatic function calling in Go.
There is no implementation yet.

  • Add a Function field to FunctionDeclaration. Populating this
    field enables the function to be automatically called. If you
    set the field manually, you must set schema manually as well.

  • Add NewCallableFunctionDeclaration. This takes a function and
    both infers its schema and populates the entire FunctionDefinition.

This design supports three use cases:

  1. Manual function calling: set schema but not function.
  2. Auto function calling, user schema: set schema and function.
  3. Auto function calling, inferred schema: call NewCallableFunctionDeclaration

But it doesn't support:

  1. Manual function calling, inferred schema

except in the roundabout way of using NewCallableFunctionDeclaration
and then setting the Function field to nil.

This PR is a suggested design for automatic function calling in Go.
There is no implementation yet.

- Add a Function field to FunctionDeclaration. Populating this
  field enables the function to be automatically called. If you
  set the field manually, you must set schema manually as well.

- Add NewCallableFunctionDeclaration. This takes a function and
  both infers its schema and populates the entire FunctionDefinition.

This design supports three use cases:

1. Manual function calling: set schema but not function.
2. Auto function calling, user schema: set schema and function.
3. Auto function calling, inferred schema: call NewCallableFunctionDeclaration

But it doesn't support:

4. Manual function calling, inferred schema

except in the roundabout way of using NewCallableFunctionDeclaration
and then setting the Function field to nil.
@jba jba requested a review from eliben March 25, 2024 20:58
@jba jba marked this pull request as draft March 25, 2024 20:58
Copy link
Member

@eliben eliben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the design! It'd be nice to start the review with some sort of high-level documentation/example explaining how users use this. We'd have to include this documentation somewhere anyway, and it should probably just be in the docstrings so it renders to pkg.go.dev

panic(err)
}

// Use the FunctionDeclaration to populate Model.Tools.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example should probably be extended to show this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started to do that. I can finish when I have an implementation.

"github.com/google/generative-ai-go/internal/support"
)

// A Tool is a piece of code that enables the system to interact with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's Tool in file functions.go but talking about functions - do we just use the two interchangeably? Aligning with how the Python SDK names things?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both OpenAI and Gemini group functions into "tools." The Python SDK follows that, and we do too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so maybe we should structure it like that too - tool is the high level concept, and functions are one of the tools available. So for example the Tool type would be in tools.go, not functions.go?

Also, is containment the right paradigm here? Is a function a kind of tool, or do tools contain functions and potentially other stuff?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed the file.
In the protos, a Tool contains FunctionDeclarations. Python keeps that structure.

}
}

// NewCallableFunctionDeclaration creates a [FunctionDeclaration] from a Go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the significance of "Callable" here? Are there also non-callable function declarations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean "automatically callable." As in, the client will invoke the function for you, instead of returning a FunctionCall Part and having you provide the result. (The automatic calling isn't implemented yet.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe removing the "Callable" can help make this a bit shorter without loss of meaning? After all, the type is FunctionDeclaration, so NewFunctionDeclaration for the constructor sounds logical

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After all, the type is FunctionDeclaration, so NewFunctionDeclaration for the constructor sounds logical

True, but usually a function named NewFoo does about the same thing as &Foo{...}. Here the New function also makes the function automatically callable.

@jba
Copy link
Collaborator Author

jba commented Mar 26, 2024

I added a section to the package doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants