EN
/news/show.php/video/24376394.html

ESP32C3创建开发板入门 修改AI大模型接口的depseek3接口

2025-06-24 12:13:33 来源: 新华社
字号:默认 超大 | 打印 |

#用于原代码的AI模型是minimax的API接口,现在试着把它改成最流行的deepsek3接口。#。

首先,理解收入󿀌在main文件夹下有minimax.c和minimax.h, 它们是API接口的第一个文件和实现文件,然后在main.在c中调用。所以我们一步一步地改变。

申请deepsek的API,deepseek等人工智能,谢谢!!

从minimax开始.开始:

char *AI_chat(const char *text); //AI_chat。

就这么简单的开了个头。顺便把文件名改成ai__chat.h。

接下来,改变minimax.c:。

搜索minimax.h,改头文件。

搜索MINIMAX󿼌第一个包含文件,#include “minimax.h",改成#include “ai_chat.h”。顺手同步改main.c里的#include "ai_chat.h"。

#include "ai_chat.h"

搜索minimax_chat,改成AI_chat。

日志标签�MINIMAX_CHAT,全大写改为AI_CHAT。

static const char *TAG = "AI_CHAT"; 

第二个是minimax_chat(const char *text) 改成AI_chat(const char *text) 。

char *AI_chat(const char *text)  // 它被自己的minimax_chat函数取代,名为AI-chat。 

搜索minimax_key, 修改密钥相关接口。 。

extern const char * minimax_key;

extern const char * AIchat_key; 
esp_http_client_set_header(client, "Authorization", AIchat_key);

更改POST__DATA。

#define POST_DATA "{ \\"model\":\"deepseek-chat\",\\"messages\":\{ \"role\":\"system\",\"content\":\"You are a helpful assistant.\"},\{ \"role\":\"user\",\"content\":\"%s\"}\,\"stream\":false\}"#define MAX_CHAT_BUFFER (2048)。

更改minimax_contentAIchat_content。

char AIchat_content[2048] = { 0};
strncpy(AIchat_content, content->valuestring, MAX_CHAT_BUFFER - 1);AIchat_content[MAX_CHAT_BUFFER - 1] = '\0'; // 确保字符串终止response__text = AIchat_content;

 更改.url = "",API接口网站࿱修改为depseek3a;它不同于minimax,不需要Groupid用户名。

.url = "https://api.deepseek.com/chat/completions", // 替换为 DeepSeek 的 API URL。

解释CJSON和信息提取。

// 解析响应 JSON    cJSON *root = cJSON_Parse(data_buf);    if (root == NULL)    {         ESP_LOGE(TAG, "解析 JSON 响应失败");        goto cleanup;    }    // 提取生成的回复    cJSON *choices = cJSON_GetObjectItem(root, "choices");    if (choices != NULL && cJSON_IsArray(choices))    {         cJSON *first_choice = cJSON_GetArrayItem(choices, 0);        if (first_choice != NULL)        {             cJSON *message = cJSON_GetObjectItem(first_choice, "message");            if (message != NULL)            {                 cJSON *content = cJSON_GetObjectItem(message, "content");                if (content != NULL && cJSON_IsString(content))                {                     strncpy(AIchat_content, content->valuestring, MAX_CHAT_BUFFER - 1);                    AIchat_content[MAX_CHAT_BUFFER - 1] = '\0'; // 确保字符串终止                    response_text = AIchat_content;                    ESP_LOGI(TAG, "生成的回复: %s", response_text);                }            }        }    }。

改变exit_translatecleanup。

改变exit_translatecleanup。
goto cleanup;

cleanup: // 使用 SAFE_FREE 宏释放资源 SAFE_FREE(post_buffer); SAFE_FREE(data_buf); esp_http_client_cleanup(client); return response_text;

 在引入头文件下加一个宏。

// 定义 SAFE_FREE 宏#define SAFE_FREE(ptr) \ do \ { \ if (ptr != NULL) \ { \ free(ptr); \ ptr = NULL; \ } \ } while (0)。

接下来改main.c:。

搜索minimax.h,改头文件。以上例子为例。

#include "ai_chat.h"

 搜索minimax_chat,改成AI_chat。

char *answer = AI_chat(original_text);

搜索minimax_key, 修改密钥相关接口。 。

const char * AIchat_key = "Bearer deepseekAPI密钥";

更改minimax_contentAIchat_content。
extern char AIchat_content[2048]; // 定义全球变量,用于接收字符串。
GitCode, 有完整的源代码。GitCode - 开源代码托管平台是全球开发者的开源社区。

【我要纠错】责任编辑:新华社