Sfoglia il codice sorgente

Merge branches 'master' and 'master' of gitee.com:openLuat/LuatOS

Wendal Chen 1 anno fa
parent
commit
8dac13b1e6
3 ha cambiato i file con 78 aggiunte e 31 eliminazioni
  1. 33 21
      demo/openai/deepseek/main.lua
  2. 2 0
      luat/include/luat_gpio.h
  3. 43 10
      script/libs/openai.lua

+ 33 - 21
demo/openai/deepseek/main.lua

@@ -6,46 +6,58 @@ sys = require "sys"
 require "sysplus"
 openai = require "openai"
 
+local uartid = 2
+
 local opts = {
-    apikey = "sk-123456",
+    -- apikey = "123456",
+    apikey = "sk-b08cf8b9bdc64050a0a20e3d44d7c15a",
+
     apiurl = "https://api.deepseek.com",
     model = "deepseek-chat"
 }
-local chat = openai.completions(opts)
-
-uart.setup(1, 115200)
-uart.on(1, "receive", function(id, len)
-    local data = uart.read(id, len)
-    log.info("uart", id, len, data)
-    if data and #data > 0 then
-        sys.publish("uart_rx", data)
-    end
+uart.setup(uartid, 115200)
+
+-- 收取数据会触发回调, 这里的"receive" 是固定值
+uart.on(uartid, "receive", function(id, len)
+    local s = ""
+    repeat
+        s = uart.read(id, 1024)
+        if #s > 0 then -- #s 是取字符串的长度
+            log.info("uart", "receive", id, #s, s)
+            uart.write(uartid,
+                "消息发送成功,请等待回复,若串口60S没有回复,请检查luatools打印的日志\r\n")
+            sys.publish("uart_rx", s)
+        end
+    until s == ""
 end)
 
 sys.taskInit(function()
     sys.waitUntil("IP_READY")
-    sys.wait(100)
-    -- -- 固定问答演示
-    -- local resp = chat:talk("你好,请问LuatOS是什么软件?应该如何学习呢?")
-    -- if resp then
-    --     log.info("deepseek回复", resp.content)
-    -- else
-    --     log.info("deepseek执行失败")
-    -- end
+    sys.wait(2000)
 
+    local chat = openai.completions(opts)
+    if chat then
+        uart.write(uartid, "大语言模型初始化完成,可以开始对话了\r\n")
+    else
+        uart.write(uartid, "大语言模型初始化失败,请检查代码\r\n")
+    end
     -- -- uart交互演示
     while 1 do
         local re, data = sys.waitUntil("uart_rx")
         if data then
             local resp = chat:talk(data)
-            if resp then
+            if resp and type(resp) == "table" then
                 log.info("deepseek回复", resp.content)
-                uart.write(1, resp.content)
+                uart.write(uartid, resp.content)
+            else
+                local re_data = "大语言模型返回失败,错误原因:\r\n"
+                log.info(re_data,resp)
+                uart.write(uartid,re_data)
+                uart.write(uartid,resp)
             end
         end
     end
 end)
 
-
 sys.run()
 

+ 2 - 0
luat/include/luat_gpio.h

@@ -100,6 +100,8 @@ void luat_gpio_close(int pin);
  */
 int luat_gpio_set_irq_cb(int pin, luat_gpio_irq_cb cb, void* args);
 
+int luat_gpio_irq_enable(int pin, uint8_t enabled);
+
 /**
  * @brief GPIO模拟单线输出模式
  * @param Pin Pin序号

+ 43 - 10
script/libs/openai.lua

@@ -10,9 +10,7 @@
 -- 对接deepseek演示 请阅demo/openai
 
 -- 本API正在积极设计中
-]]
-
-local openai = {
+]] local openai = {
     conf = {}
 }
 
@@ -28,7 +26,10 @@ local function talk(self, msg)
     if type(msg) == "table" then
         table.insert(self.msgs, msg)
     else
-        table.insert(self.msgs, {role="user", content=msg})
+        table.insert(self.msgs, {
+            role = "user",
+            content = msg
+        })
     end
 
     local rbody = {
@@ -38,7 +39,10 @@ local function talk(self, msg)
     }
     local url = self.opts.apiurl .. "/chat/completions"
     -- log.info("openai", "request", url, json.encode(rheaders), json.encode(rbody))
-    local code,headers,body = http.request("POST", url, rheaders, (json.encode(rbody)), {timeout=60*1000}).wait()
+    local code, headers, body = http.request("POST", url, rheaders, (json.encode(rbody)), {
+        timeout = 60 * 1000
+    }).wait()
+    local tag = ""
     -- log.info("openai", code, json.encode(headers) or "", body or "")
     if code == 200 then
         -- log.info("openai", "执行完成!!!")
@@ -46,10 +50,39 @@ local function talk(self, msg)
         if jdata and jdata.choices and #jdata.choices > 0 then
             -- 自动选用第一个回应
             local ch = jdata.choices[1].message
-            table.insert(self.msgs, {role=ch.role, content=ch.content})
+            table.insert(self.msgs, {
+                role = ch.role,
+                content = ch.content
+            })
             return ch
         end
+    elseif code == 400 then
+        tag = "请求体格式错误,请根据错误信息提示修改请求体"
+        log.warn(tag)
+    elseif code == 401 then
+        tag = "API key错误,认证失败,请检查您的API key是否正确,如没有API key,请先创建API key"
+        log.warn(tag)
+    elseif code == 402 then
+        tag = "账号余额不足,请充值"
+        log.warn(tag)
+    elseif code == 422 then
+        tag = "请求体参数错误,请根据错误信息提示修改请求体"
+        log.warn(tag)
+    elseif code == 429 then
+        tag = "请求速率(TPM 或 RPM)达到上限,请稍后再试"
+        log.warn(tag)
+    elseif code == 500 then
+        tag = "服务器内部故障,请等待后重试,若问题一直存在,请联系deepseek官方解决"
+        log.warn(tag)
+
+    elseif code == 503 then
+        tag = "服务器负载过高,请稍后重试您的请求"
+        log.warn(tag)
+    else
+        tag = "未知原因" .. code
     end
+    log.info("openai", code, json.encode(headers) or "", body or "")
+    return tag
 end
 
 --[[
@@ -86,12 +119,12 @@ function openai.completions(opts, prompt)
     local chat = {
         opts = opts,
         talk = talk,
-        msgs = {
-            prompt and {role="system", content=prompt}
-        }
+        msgs = {prompt and {
+            role = "system",
+            content = prompt
+        }}
     }
     return chat
 end
 
-
 return openai