main.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "deepseek"
  3. VERSION = "1.0.0"
  4. sys = require "sys"
  5. require "sysplus"
  6. openai = require "openai"
  7. local uartid = 2
  8. local opts = {
  9. apikey = "123456",
  10. apiurl = "https://api.deepseek.com",
  11. model = "deepseek-chat",
  12. }
  13. uart.setup(uartid, 115200)
  14. -- 收取数据会触发回调, 这里的"receive" 是固定值
  15. uart.on(uartid, "receive", function(id, len)
  16. local s = ""
  17. repeat
  18. s = uart.read(id, 1024)
  19. if #s > 0 then -- #s 是取字符串的长度
  20. log.info("uart", "receive", id, #s, s)
  21. uart.write(uartid,
  22. "消息发送成功,请等待回复,若串口60S没有回复,请检查luatools打印的日志\r\n")
  23. sys.publish("uart_rx", s)
  24. end
  25. until s == ""
  26. end)
  27. sys.taskInit(function()
  28. sys.waitUntil("IP_READY")
  29. sys.wait(2000)
  30. local chat = openai.completions(opts)
  31. if chat then
  32. uart.write(uartid, "大语言模型初始化完成,可以开始对话了\r\n")
  33. else
  34. uart.write(uartid, "大语言模型初始化失败,请检查代码\r\n")
  35. end
  36. -- -- uart交互演示
  37. while 1 do
  38. local re, data = sys.waitUntil("uart_rx")
  39. if data then
  40. local resp = chat:talk(data)
  41. if resp and type(resp) == "table" then
  42. log.info("deepseek回复", resp.content)
  43. uart.write(uartid, resp.content)
  44. else
  45. local re_data = "大语言模型返回失败,错误原因:\r\n"
  46. log.info(re_data,resp)
  47. uart.write(uartid,re_data)
  48. uart.write(uartid,resp)
  49. end
  50. end
  51. end
  52. end)
  53. sys.run()