Matlab调用GPT-5 API示例

借鉴这位博客的代码

复制代码
https://blog.csdn.net/qq_25837853/article/details/129448600

调用GPT-5如下

复制代码
%连接互联网
import matlab.net.*
import matlab.net.http.*
%提问文本输入
umesges  =input('我:','s');
mesges   = [struct('role',"system",'content',"You are a helpful assistant.");
struct('role',"user",'content',umesges)];
apiurl   = "https://api.chuanchuan.cloud/v1/chat/completions"; %中转或者官网的baseurl
apikey   = "密钥"; %密钥
% 定义请求消息
querymsg = struct('model',"gpt-5-chat-latest",...
    '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
    responseText = string(responseText.content); %将 responseText 转换为字符串类型
    responseText = strtrim(responseText); %移除wrapped_s开头和结尾的空格和换行符
    str=['chatgpt: ',num2str(responseText)];
    disp(str) %在命令窗口显示回答
else
    responseText = "Error "; %将字符串 "Error " 赋值给变量 responseText
    responseText = responseText + response.StatusCode + newline; %将响应状态码和一个换行符添加到 responseText 的结尾
    responseText = responseText + response.StatusLine.ReasonPhrase; %将响应状态行的原因短语添加到 responseText 的结尾
    disp(str) %在命令窗口显示回答
end
相关推荐
CSDN_RTKLIB1 天前
inline内联函数基础知识
开发语言·c++
No0d1es1 天前
2025年12月 GESP CCF编程能力等级认证Python四级真题
开发语言·python·青少年编程·等级考试·gesp·ccf
love530love1 天前
EPGF 新手教程 13在 PyCharm(中文版 GUI)中创建 Hatch 项目环境,并把 Hatch 做成“项目自包含”(工具本地化为必做环节)
开发语言·ide·人工智能·windows·python·pycharm·hatch
Ralph_Y1 天前
C++异常对象
开发语言·c++
baiduopenmap1 天前
【智图译站】GENREGION——高准确度、高可扩展的城市区域自动划分方法
开发语言·百度地图
蚰蜒螟1 天前
Redis网络层深度解析:数据如何写回客户端
java·开发语言·bootstrap
No0d1es1 天前
2025年12月 GESP CCF编程能力等级认证Python五级真题
开发语言·python·青少年编程·等级考试·gesp·ccf
风送雨1 天前
Go 语言进阶学习:第 2 周 —— 接口、反射与错误处理进阶
开发语言·学习·golang
福楠1 天前
模拟实现stack、queue、priority_queue
c语言·开发语言·数据结构·c++
峰上踏雪1 天前
Go(Golang)Windows 环境配置关键点总结
开发语言·windows·golang·go语言