Skip to content

Commit

Permalink
Function calling hint
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jul 29, 2023
1 parent 8f5276c commit 1195ca9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
8 changes: 5 additions & 3 deletions OpenAI.Chat.pas
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ TChatMessageBuild = record
/// </summary>
property Tag: string read FTag write FTag;
class function Create(Role: TMessageRole; const Content: string; const Name: string = ''): TChatMessageBuild; static;
//Help functions
class function User(const Content: string; const Name: string = ''): TChatMessageBuild; static;
class function System(const Content: string; const Name: string = ''): TChatMessageBuild; static;
class function Assistant(const Content: string; const Name: string = ''): TChatMessageBuild; static;
Expand Down Expand Up @@ -140,9 +141,10 @@ TChatParams = class(TJSONParam)
/// </summary>
function Functions(const Value: TArray<IChatFunction>): TChatParams;
/// <summary>
/// Controls how the model responds to function calls.
/// "none" is the default when no functions are present.
/// "auto" is the default if functions are present
/// Controls how the model responds to function calls. "none" means the model does not call a function,
/// and responds to the end-user. "auto" means the model can pick between an end-user or calling a function.
/// Specifying a particular function via {"name":\ "my_function"} forces the model to call that function.
/// "none" is the default when no functions are present. "auto" is the default if functions are present.
/// </summary>
function FunctionCall(const Value: TFunctionCall): TChatParams;
/// <summary>
Expand Down
Binary file removed OpenAL-GPT3.png
Binary file not shown.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,57 @@ end;

Review [Images Documentation](https://platform.openai.com/docs/api-reference/images) for more info.

### Function calling
In an API call, you can describe functions to gpt-3.5-turbo-0613 and gpt-4-0613, and have the model intelligently choose to output a JSON object containing arguments to call those functions. The Chat Completions API does not call the function; instead, the model generates JSON that you can use to call the function in your code.

The latest models (gpt-3.5-turbo-0613 and gpt-4-0613) have been fine-tuned to both detect when a function should to be called (depending on the input) and to respond with JSON that adheres to the function signature. With this capability also comes potential risks. We strongly recommend building in user confirmation flows before taking actions that impact the world on behalf of users (sending an email, posting something online, making a purchase, etc).

```Pascal
var Chat := OpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Functions(Funcs); //list of functions (TArray<IChatFunction>)
Params.FunctionCall(TFunctionCall.Auto);
Params.Messages([TChatMessageBuild.User(Text)]);
Params.MaxTokens(1024);
end);
try
for var Choice in Chat.Choices do
if Choice.FinishReason = TFinishReason.FunctionCall then
ProcFunction(Choice.Message.FunctionCall) // execute function (send result to chat, and continue)
else
MemoChat.Lines.Add(Choice.Message.Content);
finally
Chat.Free;
end;
...
procedure ProcFunction(Func: TChatFunctionCall);
begin
var FuncResult := Execute(Func.Name, Func.Arguments); //execute function and get result (json)
var Chat := OpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Functions(Funcs); //list of functions (TArray<IChatFunction>)
Params.FunctionCall(TFunctionCall.Auto);
Params.Messages([ //need all history
TChatMessageBuild.User(Text),
TChatMessageBuild.NewAsistantFunc(Func.Name, Func.Arguments),
TChatMessageBuild.Func(FuncResult, Func.Name)]);
Params.MaxTokens(1024);
end);
try
for var Choice in Chat.Choices do
MemoChat.Lines.Add(Choice.Message.Content);
finally
Chat.Free;
end;
end;
```

Review [Functions Documentation](https://platform.openai.com/docs/guides/gpt/function-calling) for more info.

### Errors

```Pascal
Expand Down

0 comments on commit 1195ca9

Please sign in to comment.