Skip to content

Commit 4ae0248

Browse files
committed
Function calling on Azure.
Note that this only works with the right models. We're currently testing on gpt-35-turbo-16k-0613.
1 parent 5d2fd99 commit 4ae0248

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

azureChat.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
% This parameter requires API version 2023-12-01-preview.
1515
%
1616
% API Version - A list of API versions to use for this operation.
17-
% Default value is 2023-05-15.
17+
% Default value is 2024-02-01.
1818
%
1919
% Temperature - Temperature value for controlling the randomness
2020
% of the output. Default value is 1.
@@ -207,7 +207,7 @@ function mustBeValidFunctionCall(this, functionCall)
207207
if ~isempty(this.Tools)
208208
toolChoice = "auto";
209209
end
210-
elseif ToolChoice ~= "auto"
210+
elseif toolChoice ~= "auto"
211211
% if toolChoice is not empty, then it must be in the format
212212
% {"type": "function", "function": {"name": "my_function"}}
213213
toolChoice = struct("type","function","function",struct("name",toolChoice));

tests/tazureChat.m

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function seedFixesResult(testCase)
4848
testCase.verifyEqual(response1,response2);
4949
end
5050

51-
function createOpenAIChatWithStreamFunc(testCase)
51+
function createAzureChatWithStreamFunc(testCase)
5252
testCase.assumeTrue(isenv("AZURE_OPENAI_API_KEY"),"end-to-end test requires environment variables AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and AZURE_OPENAI_DEPLOYMENT.");
5353
function seen = sf(str)
5454
persistent data;
@@ -69,6 +69,27 @@ function createOpenAIChatWithStreamFunc(testCase)
6969
testCase.verifyGreaterThan(numel(sf("")), 1);
7070
end
7171

72+
function generateWithTools(testCase)
73+
import matlab.unittest.constraints.HasField
74+
75+
f = openAIFunction("getCurrentWeather", "Get the current weather in a given location");
76+
f = addParameter(f, "location", type="string", description="The city and country, optionally state. E.g., San Francisco, CA, USA");
77+
f = addParameter(f, "unit", type="string", enum=["Kelvin","Celsius"], RequiredParameter=false);
78+
79+
chat = azureChat(getenv("AZURE_OPENAI_ENDPOINT"), getenv("AZURE_OPENAI_DEPLOYMENT"), ...
80+
Tools=f);
81+
82+
prompt = "What's the weather like in San Francisco, Tokyo, and Paris?";
83+
[~, response] = generate(chat, prompt, ToolChoice="getCurrentWeather");
84+
85+
testCase.assertThat(response, HasField("tool_calls"));
86+
testCase.assertEqual(response.tool_calls.type,'function');
87+
testCase.assertEqual(response.tool_calls.function.name,'getCurrentWeather');
88+
data = testCase.verifyWarningFree( ...
89+
@() jsondecode(response.tool_calls.function.arguments));
90+
testCase.verifyThat(data,HasField("location"));
91+
end
92+
7293
function errorsWhenPassingToolChoiceWithEmptyTools(testCase)
7394
chat = azureChat(getenv("AZURE_OPENAI_ENDPOINT"), getenv("AZURE_OPENAI_DEPLOYMENT"), APIKey="this-is-not-a-real-key");
7495
testCase.verifyError(@()generate(chat,"input", ToolChoice="bla"), "llms:mustSetFunctionsForCall");

0 commit comments

Comments
 (0)