json2matlab

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - Artificial Intelligence
Introduced first in SolidGeometry 5.5, Creation date: 2024-11-06, Last change: 2025-09-15

convertes a json reaction of an openAI-Chat into a matlab code line

Description

Robin Schregle used a switch structure with matlab code to implement the SG-Lib function calls. This requires the maintenance of the json code for functions f..... and in addition the code for the function calls in the matlab code (see SGlib_Fnctn_Calling_OpenAI). Tim Lueth's solution uses the JSON code of the answer directly to create the matlab code to be executed by eval.
This function is this decoding and evaluation of the matlab code, extracted from the json answer.


% AIchat = openAIChat("gpt-4o",Temperature=0, Tools=[f0,f1,f2,f3,f4,f5]); % SGlib fnctns f0..f5 as JSON
% AIhistory = messageHistory();
% AIhistory = addUserMessage(AIhistory, Userinp); % Add a user request as string
% [AItextansw, AIexecplan] = generate(AIchat, AIhistory); % generate the chat answer
% AIhistory = addResponseMessage(AIhistory, AIexecplan); % add the answer to the chat history
% SGlibcalls = AIexecplan.tool_calls; % find the appropriate SGlib calls in the answer
% SGfuncname = SGlibcalls(k).fnctn.name; % can be an array therefor use index k
% SGfuncargs = SGlibcalls(k).fnctn.arguments; % can be an array therefor use index k
% SGresult=
% SGresultname = SGfuncname + string(length(AIhistory.Messages)); % One possibility for naming
% Variables.(SGresultname) = SGResult; % create a list of results for referencing




See Also: jarvisChatSGLib , jsonplansequence , OpenAIrequest , OpenAIcostAPI

Example Illustration

 missing image of json2matlab(SGfuncname,SGfuncargs,Variables)

Syntax

[SGansw,cmdstr]=json2matlab(SGfuncname,SGfuncargs,Variables)

Input Parameter

SGfuncname: Name of the current SG function extracted from the AIexecplan
SGfuncargs: Struct containing the parameternames as fields and expected values extracted from the AIexecplan
Variables: History of already used SGresults as cell list of SGfuncnames with the call number

Output Parameter

SGansw: evaluated result from the matlab code extracted from the json answer.
cmdstr: the command string executed within json2matlab to create res




Copyright 2024-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth

Last html export of this page out of FM database by TL: 2025-09-21