1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "oss_app.h"
6 #include "py/builtin.h"
7 #include "py/mperrno.h"
8 #include "py/obj.h"
9 #include "py/runtime.h"
10 #include "ucloud_ai_chatbot.h"
11 #include "ulog/ulog.h"
12
13 #define LOG_TAG "MOD_CHATBOT"
14
15 extern const mp_obj_type_t chatbot_type;
16
17 #define CHATBOT_CHECK_PARAMS() \
18 chatbot_obj_t *self = (chatbot_obj_t *)MP_OBJ_TO_PTR(self_in); \
19 do { \
20 if (self == NULL) { \
21 mp_raise_OSError(MP_EINVAL); \
22 return mp_const_none; \
23 } \
24 } while (0)
25
26 // this is the actual C-structure for our new object
27 typedef struct {
28 // base represents some basic information, like type
29 mp_obj_base_t base;
30
31 // a member created by us
32 char *modName;
33 } chatbot_obj_t;
34
35 static chatbot_obj_t *chatbot_obj = NULL;
36
chatbot_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)37 STATIC mp_obj_t chatbot_new(const mp_obj_type_t *type, size_t n_args,
38 size_t n_kw, const mp_obj_t *args)
39 {
40 chatbot_obj = m_new_obj(chatbot_obj_t);
41 if (!chatbot_obj) {
42 mp_raise_OSError(MP_ENOMEM);
43 return mp_const_none;
44 }
45
46 memset(chatbot_obj, 0, sizeof(chatbot_obj));
47
48 chatbot_obj->base.type = &chatbot_type;
49 chatbot_obj->modName = "chatbot";
50
51 return MP_OBJ_FROM_PTR(chatbot_obj);
52 }
53
chatbot_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)54 void chatbot_print(const mp_print_t *print, mp_obj_t self_in,
55 mp_print_kind_t kind)
56 {
57 chatbot_obj_t *self = MP_OBJ_TO_PTR(self_in);
58 mp_printf(print, "modName(%s)", self->modName);
59 }
60
obj_input(size_t n_args,const mp_obj_t * args)61 STATIC mp_obj_t obj_input(size_t n_args, const mp_obj_t *args)
62 {
63 LOGD(LOG_TAG, "entern %s; n_args = %d;\n", __func__, n_args);
64 int ret = -1;
65 char *response = NULL;
66 char buffer[2048];
67
68 if (n_args < 6) {
69 LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__,
70 n_args);
71 return mp_const_none;
72 }
73
74 mp_obj_t self_in = args[0];
75 CHATBOT_CHECK_PARAMS();
76
77 char *key = (char *)mp_obj_str_get_str(args[1]);
78 char *secret = (char *)mp_obj_str_get_str(args[2]);
79 char *instanceId = (char *)mp_obj_str_get_str(args[3]);
80 char *sessionId =
81 mp_obj_is_str(args[4]) ? (char *)mp_obj_str_get_str(args[4]) : NULL;
82 char *text = (char *)mp_obj_str_get_str(args[5]);
83 LOGD(LOG_TAG, "key = %s;\n", key);
84 LOGD(LOG_TAG, "secret = %s;\n", secret);
85 LOGD(LOG_TAG, "instanceId = %s;\n", instanceId);
86 LOGD(LOG_TAG, "sessionId = %s\n", sessionId);
87 LOGD(LOG_TAG, "text = %s;\n", text);
88
89 ucloud_ai_set_key_secret(key, secret);
90 response = ucloud_ai_chatbot(instanceId, sessionId, text);
91 if (!response)
92 return mp_const_none;
93
94 if (strlen(response) > 2048) {
95 LOGE(LOG_TAG, "buffer is not enough\n");
96 free(response);
97 return mp_const_none;
98 }
99 strcpy(buffer, response);
100 free(response);
101 return mp_obj_new_strn(buffer);
102 }
103
104 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(chatbot_obj_input, 6, obj_input);
105
106 STATIC const mp_rom_map_elem_t chatbot_locals_dict_table[] = {
107 { MP_ROM_QSTR(MP_QSTR_input), MP_ROM_PTR(&chatbot_obj_input) },
108 };
109
110 STATIC MP_DEFINE_CONST_DICT(chatbot_locals_dict, chatbot_locals_dict_table);
111
112 const mp_obj_type_t chatbot_type = {
113 .base = { &mp_type_type },
114 .name = MP_QSTR_ChatBot,
115 .print = chatbot_print,
116 .make_new = chatbot_new,
117 .locals_dict = (mp_obj_dict_t *)&chatbot_locals_dict,
118 };
119