许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  基于matlab的deepseek自主编程代码分享

基于matlab的deepseek自主编程代码分享

阅读数 3
点赞 0
article_banner


视频配套代码deepseek+matlab编程及自动修改代码!无需额外下载任何编辑器!零帧入门ai编程!小白福音!​

另外,想学习多轮对话,或者不想买API的同学,可以下载ollama,用本地模型实现相同功能!零成本,亲测轻薄本可用。视频见(有手就行)零成本matlab+本地AI调代码​

本代码可以实现MATLab直接访问AI平台。然后根据AI平台的反馈,自助调节代码或者是生成回复。第1段代码相当于写了一个普通的平台。可以实现对话框功能。第2段代码相当于写了一个代码调试编辑器。就是它会根据你的需求生成代码,如果代码有问题,它就会自动的循环改造直到代码无问题为止,免去了你反复把问题复制到对话框,再让AI改的苦恼。

本程序应用到了api-key。基于deepseek写的,建议提前充值几块钱玩玩

1、matlab代码请求deepseek回复代码

fid = fopen('D:\1.txt', 'r');

if fid == -1

disp('无法打开密钥文件');

return;

end

api_key = fscanf(fid, '%s');

fclose(fid);

disp('API Key:');

url = 'https://api.deepseek.com/chat/completions';

headers = {'Content-Type', 'application/json'; 'Authorization', ['Bearer ', api_key]};

json_data = sprintf('{"model":"deepseek-chat","messages":[{"role":"user","content":"今天心情不好怎么办"}]}');

try

options = weboptions('HeaderFields', headers, 'MediaType', 'application/json', 'Timeout', 60);

response = webwrite(url, json_data, options);

disp('');

disp('=== 天气查询结果 ===');

disp(response.choices(1).message.content);

catch ME

disp('');

disp('查询天气时出错:');

disp(ME.message);

disp('');

disp('可能的原因:');

disp('1. 网络连接问题');

disp('2. API Key无效或过期');

disp('3. DeepSeek服务器暂时不可用');

end

matlab自主编程代码

disp('=== AI代码生成器 (DeepSeek API) ===');

disp('');

api_key = load_api_key();

if isempty(api_key)

disp('错误:无法加载API Key');

return;

end

user_request = input('请输入您的编程要求:', 's');

disp('');

disp('您的编程要求:');

disp(user_request);

disp('');

disp('正在向DeepSeek API请求代码...');

code = call_deepseek_api(api_key, user_request);

if isempty(code)

disp('错误:未能获取代码');

return;

end

disp('');

disp('生成的代码:');

disp('----------------------------------------');

disp(code);

disp('----------------------------------------');

disp('');

run_code_and_fix(code);

function api_key = load_api_key()

fid = fopen('D:\1.txt', 'r');

if fid == -1

disp('警告:无法打开密钥文件');

api_key = '';

return;

end

api_key = fscanf(fid, '%s');

fclose(fid);

end

function code = call_deepseek_api(api_key, request)

url = 'https://api.deepseek.com/chat/completions';

headers = {

'Content-Type', 'application/json';

'Authorization', ['Bearer ', api_key]

};

json_data = sprintf('{"model":"deepseek-chat","messages":[{"role":"system","content":"你是一个MATLAB编程专家。请根据用户的编程要求,生成完整、可运行的MATLAB脚本代码。代码必须是脚本形式(script),不要定义函数(function),所有代码都应该可以直接在命令窗口运行。只输出代码,不要输出解释性文字。"},{"role":"user","content":"请生成MATLAB代码:%s"}]}', request);

try

options = weboptions('HeaderFields', headers, 'MediaType', 'application/json', 'Timeout', 60);

response = webwrite(url, json_data, options);

code = response.choices(1).message.content;

code = strrep(code, '```matlab', '');

code = strrep(code, '```', '');

code = strtrim(code);

catch ME

disp(['API调用失败: ', ME.message]);

code = '';

end

end

function run_code_and_fix(code)

max_attempts = 3;

attempt = 1;

success = false;

current_code = code;

if contains(current_code, 'function ')

disp('检测到函数定义代码,正在转换为脚本形式...');

current_code = convert_function_to_script(current_code);

disp('');

disp('转换后的脚本代码:');

disp('----------------------------------------');

disp(current_code);

disp('----------------------------------------');

disp('');

end

while attempt <= max_attempts && ~success

disp(['第 ', num2str(attempt), ' 次尝试运行代码...']);

try

evalin('base', current_code);

disp('');

disp('✓ 代码运行成功!');

success = true;

save_code(current_code);

catch ME

disp('');

disp('✗ 代码运行出错:');

disp([' 错误类型: ', ME.identifier]);

disp([' 错误信息: ', ME.message]);

if attempt < max_attempts

disp('');

disp('正在尝试修复代码...');

current_code = fix_code(current_code, ME);

disp('');

disp('修复后的代码:');

disp('----------------------------------------');

disp(current_code);

disp('----------------------------------------');

end

attempt = attempt + 1;

end

end

if ~success

disp('');

disp('已达到最大尝试次数,代码仍无法运行。');

disp('请手动检查代码或提供更详细的编程要求。');

end

end

function script_code = convert_function_to_script(func_code)

script_code = strrep(func_code, 'function ', '');

script_code = regexprep(script_code, '^\s*[\w_]+\s*=\s*[\w_]+\s*\(\s*\)', '');

script_code = strrep(script_code, 'end', '');

script_code = strtrim(script_code);

lines = strsplit(script_code, char(10));

new_lines = {};

for i = 1:length(lines)

line = strtrim(lines{i});

if ~isempty(line) && ~(length(line) >= 1 && line(1) == '%')

new_lines{end+1} = line;

end

end

script_code = strjoin(new_lines, char(10));

end

function code = fix_code(code, error_info)

if contains(error_info.message, '未定义函数或变量')

vars = regexp(error_info.message, '''(.*?)''', 'tokens');

if ~isempty(vars)

undefined_var = vars{1}{1};

code = ['clear all;', char(10), 'clc;', char(10), code];

end

elseif contains(error_info.message, '下标索引必须为正整数或逻辑值')

code = strrep(code, 'x = 0', 'x = 1');

elseif contains(error_info.message, '无法打开文件')

code = strrep(code, 'fopen(', 'fopen(''test.txt'', ''w'');fclose(fid);fopen(');

elseif contains(error_info.message, '串联的数组的维度不一致')

code = regexprep(code, 'disp\(\[.*?\]\)', 'disp(''输出内容'')');

elseif contains(error_info.message, '参数数目不足')

code = regexprep(code, 'plot\(', 'plot([0:0.1:10], sin([0:0.1:10]));');

elseif contains(error_info.message, '函数定义在此上下文中不受支持')

code = convert_function_to_script(code);

end

end

function save_code(code)

[filename, pathname] = uiputfile('*.m', '保存生成的代码');

if filename ~= 0

full_path = fullfile(pathname, filename);

fid = fopen(full_path, 'w');

fprintf(fid, '%s', code);

fclose(fid);

disp(['代码已保存到: ', full_path]);

end

end




免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空