Skip to content

Commit

Permalink
Stop warning in constructor for ResponseFormat="json"
Browse files Browse the repository at this point in the history
Since we now throw an error when `ResponseFormat="json"` is used without
the required `"json"` in the inputs, we can stop warning in the constructor.
With the previous state, users got a warning they could not avoid, even
in code doing everything correctly.
  • Loading branch information
ccreutzi committed Nov 13, 2024
1 parent 245e4b0 commit 2d0ec2d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 25 deletions.
3 changes: 0 additions & 3 deletions +llms/+openai/validateResponseFormat.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ function validateResponseFormat(format,model,messages)
error("llms:warningJsonInstruction", ...
llms.utils.errorMessageCatalog.getMessage("llms:warningJsonInstruction"))
end
else
warning("llms:warningJsonInstruction", ...
llms.utils.errorMessageCatalog.getMessage("llms:warningJsonInstruction"))
end
elseif requestsStructuredOutput(format)
if ~startsWith(model,"gpt-4o")
Expand Down
10 changes: 3 additions & 7 deletions examples/AnalyzeSentimentinTextUsingChatGPTinJSONMode.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ model = "gpt-3.5-turbo";
chat = openAIChat(systemPrompt, ModelName=model, ResponseFormat="json");
```

```matlabTextOutput
Warning: When using JSON mode, you must also prompt the model to produce JSON yourself via a system or user message.
```

Concatenate the prompt and input text and generate an answer with the model.

```matlab
Expand All @@ -67,7 +63,7 @@ Generate a response from the message.
end
```

Extract the data from the JSON ouput.
Extract the data from the JSON output.

```matlab
T = struct2table([scores{:}]);
Expand All @@ -78,10 +74,10 @@ T = movevars(T,"text","Before","sentiment")
|:--:|:--:|:--:|:--:|
|1|"I can't stand homework."|'negative'|0.9500|
|2|"This sucks. I'm bored."|'negative'|0.9000|
|3|"I can't wait for Halloween!!!"|'positive'|0.9500|
|3|"I can't wait for Halloween!!!"|'positive'|1|
|4|"I am neigher for or against the idea."|'neutral'|1|
|5|"My cat is adorable ❤️❤️"|'positive'|0.9500|
|6|"I hate chocolate"|'negative'|0.9000|
|6|"I hate chocolate"|'negative'|1|


*Copyright 2024 The MathWorks, Inc.*
Expand Down
Binary file not shown.
8 changes: 0 additions & 8 deletions tests/hopenAIChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,6 @@ function createChatWithStreamFunc(testCase)
testCase.verifyGreaterThan(numel(sf("")), 1);
end

function warningJSONResponseFormat(testCase)
chat = @() testCase.constructor("You are a useful assistant", ...
APIKey="this-is-not-a-real-key", ...
ResponseFormat="json");

testCase.verifyWarning(@()chat(), "llms:warningJsonInstruction");
end

function errorJSONResponseFormat(testCase)
testCase.verifyError( ...
@() generate(testCase.structuredModel,"create some address",ResponseFormat="json"), ...
Expand Down
19 changes: 15 additions & 4 deletions tests/tazureChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ function deploymentNotFound(testCase)
testCase.verifyError(@()azureChat, "llms:deploymentMustBeSpecified");
end

% open TODOs for azureChat
function warningJSONResponseFormat(testCase)
testCase.assumeFail("TODO for azureChat");
end
end
end

Expand All @@ -194,6 +190,21 @@ function warningJSONResponseFormat(testCase)
"SystemPrompt", {[]}, ...
"ResponseFormat", {"text"} ...
) ...
),...
"SomeSettings", struct( ...
"Input",{{"Temperature",1.23,"TopP",0.6,"TimeOut",120,"ResponseFormat","json"}}, ...
"ExpectedWarning", '', ...
"VerifyProperties", struct( ...
"Temperature", {1.23}, ...
"TopP", {0.6}, ...
"StopSequences", {string([])}, ...
"PresencePenalty", {0}, ...
"FrequencyPenalty", {0}, ...
"TimeOut", {120}, ...
"FunctionNames", {[]}, ...
"SystemPrompt", {[]}, ...
"ResponseFormat", {"json"} ...
) ...
));
end

Expand Down
3 changes: 1 addition & 2 deletions tests/texampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ function testAnalyzeScientificPapersUsingFunctionCalls(testCase)

function testAnalyzeSentimentinTextUsingChatGPTinJSONMode(testCase)
testCase.startCapture("AnalyzeSentimentinTextUsingChatGPTinJSONMode");
testCase.verifyWarning(@AnalyzeSentimentinTextUsingChatGPTinJSONMode,...
"llms:warningJsonInstruction");
AnalyzeSentimentinTextUsingChatGPTinJSONMode;
end

function testAnalyzeTextDataUsingParallelFunctionCallwithChatGPT(testCase)
Expand Down
2 changes: 1 addition & 1 deletion tests/topenAIChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function specialErrorForUnsupportedResponseFormat(testCase)
), ...
"ResponseFormat", struct( ...
"Input",{{"APIKey","this-is-not-a-real-key","ResponseFormat","json"}}, ...
"ExpectedWarning", "llms:warningJsonInstruction", ...
"ExpectedWarning", '', ...
"VerifyProperties", struct( ...
"Temperature", {1}, ...
"TopP", {1}, ...
Expand Down

0 comments on commit 2d0ec2d

Please sign in to comment.