前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >matlab调用ChatGPT保姆级教程

matlab调用ChatGPT保姆级教程

作者头像
巴山学长
发布2023-03-15 11:15:03
1.4K0
发布2023-03-15 11:15:03
举报
文章被收录于专栏:巴山学长巴山学长
之前用matlab的appdesigner写了一个调用ChatGPT API的小工具,并作了上面的小视频分享在各个视频平台上,之后用不少伙伴找咱要这个代码。之前的分享的时候能调用的最高级模型也就是text-davinci-003,现在都已经更新到了gpt-3.5-turbogpt-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)的三维图:

问题如下:

代码语言:javascript
复制
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包:

代码语言:javascript
复制
import matlab.net.*
import matlab.net.http.*

定义消息内容:

代码语言:javascript
复制
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:

代码语言:javascript
复制
apiurl   = "https://api.openai.com/v1/chat/completions";
apikey   = "此处为你自己的api-keys";

定义请求头和请求方法:

代码语言:javascript
复制
% 定义请求消息
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);

向服务器发送请求:

代码语言:javascript
复制
response = send(request, URI(apiurl));

处理反馈信息:

代码语言:javascript
复制
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

反馈结果如下:

代码语言:javascript
复制
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上发布

如需转载,请在公众号中回复“转载”获取授权,如未经授权擅自搬运抄袭的,本公众号将保留一切追责权利!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 巴山学长 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档