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
相关推荐
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054965 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月5 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237175 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian5 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡5 天前
简单工厂模式
开发语言·算法·c#