matlab调用ChatGPT保姆级教程 | 您所在的位置:网站首页 › matlab中grid用法 › matlab调用ChatGPT保姆级教程 |
之前用matlab的appdesigner写了一个调用ChatGPT API的小工具,并作了上面的小视频分享在各个视频平台上,之后用不少伙伴找咱要这个代码。之前的分享的时候能调用的最高级模型也就是text-davinci-003,现在都已经更新到了gpt-3.5-turbo和gpt-3.5-turbo-0301。据说这个模型和现在大家用的那个官方网页版的效果非常接近,当然,咱是没有亲测过了。 新模型新气象,天下不会掉馅饼,调用新模型并不免费,而是按0.002美元/1000 tokens计费。 为了方便大家体验,新用户会有18美元的体验金,这个体验金是有效期的,在有效期之内才能。 想要调用gpt-3.5-turbo模型,首先就得要有一个可用的openapi账号并创建一个api-keys供调用时验证。 下面通过实例来看看如何在matlab中调用chatgpt的api,问它如何用matlab在区间x~[-pi, pi],y~[-pi, pi]上绘制z=sin(x)*cos(y)的三维图: 问题如下: how to make a figure and plot z=sin(x)*cos(y) for x=-pi to + pi and y = -pi to +pi using matlab?导入HTTP包: import matlab.net.* import matlab.net.http.*定义消息内容: umesges = "how to make a figure and plot z=sin(x)*cos(y) for x=-pi to + pi and y = -pi to +pi using matlab"; mesges = [struct('role',"system", ... 'content',"You are a helpful assistant."); struct('role',"user",'content',umesges)];定义服务器链接和api-keys: apiurl = "https://api.openai.com/v1/chat/completions"; apikey = "此处为你自己的api-keys";定义请求头和请求方法: % 定义请求消息 querymsg = struct('model',"gpt-3.5-turbo",... 'messages',mesges, ... 'max_tokens',1000,... 'temperature',0.75); % 请求头 headers = HeaderField('Content-Type', 'application/json',... 'Authorization', "Bearer " + apikey); % 请求消息 request = RequestMessage('post',headers,querymsg);向服务器发送请求: response = send(request, URI(apiurl));处理反馈信息: if response.StatusCode == "OK" responseText = response.Body.Data.choices(1).message; responseText = string(responseText.content); responseText = strtrim(responseText); disp(responseText); else responseText = "Error "; responseText = responseText + response.StatusCode + newline; responseText = responseText + response.StatusLine.ReasonPhrase; end反馈结果如下: Sure, you can use the following code to create a figure and plot the function z=sin(x)*cos(y) in MATLAB: ``` % Define the range of x and y values x = linspace(-pi, pi, 100); y = linspace(-pi, pi, 100); % Create a grid of x and y values [X, Y] = meshgrid(x, y); % Compute the function z=sin(x)*cos(y) Z = sin(X).*cos(Y); % Create a new figure figure(); % Plot the function using surf function surf(X, Y, Z); % Set the color map to 'jet' colormap('jet'); % Add a color bar colorbar(); % Add labels for the axes and title xlabel('x'); ylabel('y'); zlabel('z'); title('z = sin(x)*cos(y)'); ``` This code will create a 3D plot of the function z=sin(x)*cos(y) for x=-pi to + pi and y = -pi to +pi, with x, y, and z axes labeled and a color bar indicating the values of z. You can customize the plot further by adjusting the grid size, color map, and other plot properties as needed.这样就完成了整个对ChatGPT API的调用过程,通过稍加改进完善就可以做成一个即时的ChatGPT聊天机器人。这里需要特别感谢Hans Scharler在matlab的官方问答论坛中首次给出了在matlab调用ChatGPT的解决方案。Duncan Carlsmith基于Hans Scharler的答案在File Exchange中分享了几个使用ChatGPT生成m文件的小工具。Toshiakit基于基于Hans Scharler的答案开发了一个专门用于调用ChatGPT API的chatGPT类。以上提到的内容都可在参考资料中找到,想进一步研究的伙伴可通过参考资料深入学习。 首发链接-《matlab调用ChatGPT保姆级教程》 参考资料: [1] matlabcentral/answers/1894530-connecting-to-chatgpt-using-api [2] http://github.com/toshiakit/chatgpt_matlab [3] matlabcentral/fileexchange/124080-chatgpt-generated-matlab-program [4] http://github.com/OpsConfig/OpenAI_Lab/blob/main/chatgpt/ChatGPT%20Demo.ipynb 封面图片:由 Ruby Chen 在http://openai.com/blog/chatgpt上发布 |
CopyRight 2018-2019 实验室设备网 版权所有 |