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
相关推荐
踏着七彩祥云的小丑1 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid1 小时前
Python12(网络编程)
开发语言·网络·php
W23035765732 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
Y4090012 小时前
【多线程】线程安全(1)
java·开发语言·jvm
不爱吃炸鸡柳2 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
minji...3 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
Dxy12393102163 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert
Cosolar5 小时前
Nanobot 深度解析:超轻量级通用 AI Agent 运行时的架构设计与实战指南
gpt·llm·ai编程
wjs20245 小时前
JavaScript 语句
开发语言