Skip to content

Commit

Permalink
feat(func): support for function calling in OpenAI API (#149)
Browse files Browse the repository at this point in the history
- Add support for function calling in OpenAI API
- Add support for parallel function calls in certain models
- Allow function calling in the following models: gpt-4, gpt-4-turbo-preview, gpt-4-0125-preview, gpt-4-1106-preview, gpt-4-0613, gpt-3.5-turbo, gpt-3.5-turbo-0125, gpt-3.5-turbo-1106
- Add a check for the model version to allow function calling
- Change the client struct to include a new field for the model version
- Update the `allowFuncCall` function to check the model version and return true if the model supports function calling

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 22, 2024
1 parent 296d527 commit b3d2950
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,28 @@ func New(opts ...Option) (*Client, error) {

// allowFuncCall returns true if the model supports function calls.
// https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/function-calling
// Function calling is available in the 2023-07-01-preview API version and works with version 0613 of
// gpt-35-turbo, gpt-35-turbo-16k, gpt-4, and gpt-4-32k.
// https://platform.openai.com/docs/guides/function-calling/supported-models
// Not all model versions are trained with function calling data.
// Function calling is supported with the following models:
// gpt-4, gpt-4-turbo-preview, gpt-4-0125-preview, gpt-4-1106-preview, gpt-4-0613,
// gpt-3.5-turbo, gpt-3.5-turbo-0125, gpt-3.5-turbo-1106, and gpt-3.5-turbo-0613
// In addition, parallel function calls is supported on the following models:
// gpt-4-turbo-preview, gpt-4-0125-preview, gpt-4-1106-preview,
// gpt-3.5-turbo-0125, and gpt-3.5-turbo-1106
func (c *Client) allowFuncCall(cfg *config) bool {
if cfg.provider == AZURE && cfg.apiVersion == "2023-07-01-preview" {
return true
}

switch c.model {
case openai.GPT432K0613, openai.GPT40613,
openai.GPT3Dot5Turbo0613, openai.GPT3Dot5Turbo16K0613:
case openai.GPT4TurboPreview,
openai.GPT4Turbo0125,
openai.GPT4Turbo1106,
openai.GPT40613,
openai.GPT3Dot5Turbo,
openai.GPT3Dot5Turbo0125,
openai.GPT3Dot5Turbo0613,
openai.GPT3Dot5Turbo1106:
return true
default:
return false
Expand Down

0 comments on commit b3d2950

Please sign in to comment.