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
相关推荐
辛苦才能8 小时前
C++多态原理:虚函数表的内存布局与动态绑定的汇编真相
开发语言·c++
铲灰8 小时前
做个总结而已
开发语言
IvanCodes8 小时前
Python 文件操作(十二):文件与目录的读写
开发语言·python
神仙别闹16 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
伞伞悦读17 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
2601_9669496519 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
m0_7345717620 小时前
深入理解5g <十八>传输块tbsize的计算
开发语言·5g
RAN114267600821 小时前
1.3第一个程序的介绍
开发语言·qt
blue_ice .1 天前
Verilog DDS 数字信号发生器:DAC 输出模拟波形与 ADC 回采观测
开发语言·单片机·嵌入式硬件·fpga开发
远翔调光芯片^138287988721 天前
ECP5702能芯科技PD取电芯片在市场上的优势有哪些?
开发语言·人工智能·单片机·嵌入式硬件·智能家居