2 Star 1 Fork 1

刘煜/smartspeaker

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
nlp_llm.c 3.87 KB
一键复制 编辑 原始数据 按行查看 历史
刘煜 提交于 2024-05-18 17:22 +08:00 . 将HTTP协议处理放到单独的文件中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include "http.h"
char* api_key = "bce-v3/...";
char* app_id = "38b39091-0040-4190-aec6-b271aa9233ef";
char* conversation_id;
char* new_conversation(char* app_id, char* api_key)
{
char *url = "https://qianfan.baidubce.com/v2/app/conversation";
//构造请求消息
cJSON* root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "app_id", app_id);
char *postdata = cJSON_Print(root);
cJSON_Delete(root);
puts(postdata);
//增加请求头部字段
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
char* auth = NULL;
asprintf(&auth, "Authorization: Bearer %s", api_key);
headers = curl_slist_append(headers, auth);
free(auth);
size_t size = strlen(postdata);
char* response = post(url, headers, postdata, &size);
curl_slist_free_all(headers);
free(postdata);
if (!response)
{
return NULL;
}
puts(response);
cJSON *resp_root = cJSON_ParseWithLength(response, size);
free(response);
if (!resp_root)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
return NULL;
}
cJSON* conversation_id = cJSON_GetObjectItem(resp_root, "conversation_id");
if (!conversation_id)
{
cJSON* msg = cJSON_GetObjectItem(resp_root, "message");
if (msg)
{
fprintf(stderr, "Error: %s\n", msg->valuestring);
}
cJSON_Delete(resp_root);
return NULL;
}
char* retval = strdup(conversation_id->valuestring);
cJSON_Delete(resp_root);
return retval;
}
char* conversation(char* app_id, char* api_key, char* conversation_id, char* query)
{
char *url = "https://qianfan.baidubce.com/v2/app/conversation/runs";
//构造请求消息
cJSON* root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "app_id", app_id);
cJSON_AddStringToObject(root, "conversation_id", conversation_id);
cJSON_AddFalseToObject(root, "stream");
cJSON_AddStringToObject(root, "query", query);
char *postdata = cJSON_Print(root);
cJSON_Delete(root);
puts(postdata);
//增加请求头部字段
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
char* auth = NULL;
asprintf(&auth, "Authorization: Bearer %s", api_key);
headers = curl_slist_append(headers, auth);
free(auth);
size_t size = strlen(postdata);
char* response = post(url, headers, postdata, &size);
curl_slist_free_all(headers);
free(postdata);
if (!response)
{
return NULL;
}
puts(response);
cJSON *resp_root = cJSON_ParseWithLength(response, size);
if (!resp_root)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
return NULL;
}
cJSON* answer = cJSON_GetObjectItem(resp_root, "answer");
if (!answer)
{
cJSON* msg = cJSON_GetObjectItem(resp_root, "message");
if (msg)
{
fprintf(stderr, "Error: %s\n", msg->valuestring);
}
cJSON_Delete(root);
return NULL;
}
char* retval = strdup(answer->valuestring);
cJSON_Delete(resp_root);
return retval;
}
int main()
{
char line[80];
conversation_id = new_conversation(app_id, api_key);
if (!conversation_id)
{
return 1;
}
while (fgets(line, sizeof line, stdin))
{
char* answer = conversation(app_id, api_key, conversation_id, line);
if (answer)
{
puts(answer);
free(answer);
}
}
free(conversation_id);
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tinytaro/smartspeaker.git
git@gitee.com:tinytaro/smartspeaker.git
tinytaro
smartspeaker
smartspeaker
main

搜索帮助