matlab调用ChatGPT保姆级教程
新模型新气象,天下不会掉馅饼,调用新模型并不免费,而是按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类。以上提到的内容都可在参考资料中找到,想进一步研究的伙伴可通过参考资料深入学习。
参考资料:
[1] matlabcentral/answers/1894530-connecting-to-chatgpt-using-api
[2] github.com/toshiakit/chatgpt_matlab
[3] matlabcentral/fileexchange/124080-chatgpt-generated-matlab-program
[4] github.com/OpsConfig/OpenAI_Lab/blob/main/chatgpt/ChatGPT%20Demo.ipynb
封面图片:由 Ruby Chen 在openai.com/blog/chatgpt上发布
如需转载,请在公众号中回复“转载”获取授权,如未经授权擅自搬运抄袭的,本公众号将保留一切追责权利!
相关文章
- matlab 求矩阵秩,用MATLAB编程求矩阵的秩
- matlab中错误使用fmincon,MATLAB中fmincon 函数问题
- matlab interp1db,matlab – Matlab interp1图出现数据偏移 – 堆栈内存溢出
- matlab画图标签,Matlab绘图
- 【Matlab】如何规范地编写一个MATLAB函数文件
- zigzag扫描matlab,ZIGZAG扫描的MATLAB实现
- Matlab 直方图_matlab分析
- MATLAB 循环语句_for循环matlab
- ChatGPT竟写出毁灭人类计划书,还给出相应Python代码,网友:AI正在指数级发展
- 全网唯一,不忽悠的ChatGPT
- 在日报、读后感、小说、公文模版、编程等场景体验了一把chatGPT
- 搜索arXiv最相关论文一键直达,多亏了ChatGPT,几天就完成
- 普华永道:ChatGPT和生成式AI的11大安全趋势
- 港科大允许学生使用ChatGPT!AI能加强教学,老师可自行决策
- 做大模型时代的「Linux」, ChatGPT 仅是开端
- ChatGPT 加持,决策大模型距离 AGI 更进一步
- 训练ChatGPT成为一名网络安全专家
- ChatGPT|Stable Diffusion实践
- 快速安装Linux上的MATLAB(linux安装matlab)