将 Siri 接入 ChatGPT,直接语音唤醒,并且支持连续对话。

image.png

01|拷贝项目

1、点击右上角「Get a copy」,这会打开 AirCode,并基于此模板创建一个你自己的项目。如果没登录的话,可能会先跳转到登录页面,推荐使用 GitHub 登录,会快一些。

image.png

2、在弹出的创建对话框中,输入你的项目名称,并点击「Create」完成创建。

image.png
image.png

02|将 OpenAI Key 填入到环境变量中

1、登录到你的 OpenAI 账号中(如果还没有,需要注册一个),进入「API Keys」页面,点击1「Create new secret key」创建一个密钥。

image.png

2、在弹出的对话框中,点击复制图标,将这个 API Key 复制并保存下来。注意:正确的 API Key 都是以 sk- 开头的字符串。

image.png

3、进入刚才创建好的 AirCode 应用中,在「Environments」标签页,将复制的 API Key 的值填入「OPENAI_KEY」这一项的 value 中。

image.png

03|部署 AirCode 应用

1、填完环境变量后,点击页面最上方的「Deploy」按钮,在弹出对话框中点击「Deploy」,开始部署。

image.png

2、部署成功后,在左侧文件列表中选中「chat.js」,可以看到中间编辑器部分,文件下方有一个 URL,点击复制这个 URL。

image.png

3、我们可以测试一下系统是否运行正常。将这个 URL 在新标签页打开,并在后面加上 ?question=你好,如果返回的结果包含正常的 reply 信息,则代表一切正常。注意:因为 ChatGPT 响应需要一定的时间,视网络状况大概 15 到 45 秒不等,所以请耐心等待,不要频繁刷新页面。

image.png

04|添加 iPhone 快捷指令

1、在 iPhone 的浏览器中,打开以下链接。
https://www.icloud.com/shortcuts/96820bde426948918c25ed0d7a4c548f
2、在打开的页面中点击「获取捷径」按钮,然后在弹出的窗口中点击「添加快捷指令」。

image.png
image.png

3、点击刚刚添加成功的快捷指令右上角的三个点,打开快捷指令的编辑页面。将上面「第三步」中获取到的 AirCode 云函数的 URL 填入「文本」区域,点击右上角「完成」。注意:云函数 URL 是类似 https://xxxx.hk.aircode.run/chat 这样的格式。

image.png image.png

05|使用

至此,你完成了所有配置过程,直接在手机中通过「嘿 Siri,打开机器人」就可以唤醒 ChatGPT,然后问问题了。

image.png

另外,你也可以在快捷指令的编辑页面中,点击下方的「分享」按钮,在弹出的菜单中选择「添加到主屏幕」,这样就可以在桌面通过点击打开对话框。

image.png image.png

Enjoy your life with ChatGPT!

06|附:chat.js

  1. // @see https://docs.aircode.io/guide/functions/
  2. const aircode = require('aircode');
  3. const { Configuration, OpenAIApi } = require('openai');
  4. const { v4: uuidv4 } = require('uuid');
  5. const { db } = aircode;
  6. const ChatTable = db.table('chat');
  7. // Setup OpenAI configurations
  8. const OPENAI_KEY = process.env.OPENAI_KEY || "";
  9. const OPENAI_MODEL = process.env.MODEL || "gpt-3.5-turbo";
  10. const MAX_MESSAGES_PER_CHAT = 40;
  11. const systemContent = 'You are a helpful assistant.';
  12. module.exports = async function(params, context) {
  13. console.log('Received params:', params);
  14. const { question, cid } = params;
  15. // Create a chat ID if not provided
  16. const chatId = cid ? cid : uuidv4();
  17. // Save user's question to the ChatTable
  18. await ChatTable.save({ chatId, role: 'user', content: question });
  19. // Retrieve chat history
  20. const chats = await ChatTable
  21. .where({ chatId })
  22. .sort({ createdAt: -1 })
  23. .limit(MAX_MESSAGES_PER_CHAT)
  24. .find();
  25. // Construct message array for GPT-3.5 Turbo
  26. const messages = [
  27. { role: 'system', content: 'You are a helpful assistant.' },
  28. ...chats.reverse().map(one => ({ role: one.role, content: one.content })),
  29. ];
  30. const openai = new OpenAIApi(new Configuration({ apiKey: OPENAI_KEY }));
  31. try {
  32. // Request completion from GPT-3.5 Turbo
  33. const completion = await openai.createChatCompletion({
  34. model: OPENAI_MODEL,
  35. messages,
  36. temperature: 1,
  37. n: 1,
  38. stream: false,
  39. });
  40. const responseMessage = completion.data.choices[0].message;
  41. // Save generated response to ChatTable
  42. await ChatTable.save({ chatId, ...responseMessage });
  43. // Return response message and chat ID
  44. return { reply: responseMessage.content, cid: chatId };
  45. } catch (error) {
  46. // Set the response status to 500 (Internal Server Error)
  47. context.status(500);
  48. // Log the error
  49. console.log('error', error.response || error);
  50. // Initialize an error message variable
  51. let errorMessage;
  52. // If there is a response object in the error,
  53. // it means the request was made and the server responded with an error status
  54. if (error.response) {
  55. const { status, statusText, data } = error.response;
  56. if (status === 401) {
  57. // If the status code is 401, set a specific error message related to the OpenAI API key
  58. errorMessage = 'Unauthorized: Invalid OpenAI API key, please check your API key in the AirCode Environments tab.';
  59. } else if (data.error && data.error.message) {
  60. // If there is an error message in the data, use it as the error message
  61. errorMessage = data.error.message;
  62. } else {
  63. // Otherwise, use the status code and status text as the error message
  64. errorMessage = `Request failed with status code ${status}: ${statusText}`;
  65. }
  66. } else if (error.request) {
  67. // If there is a request object in the error,
  68. // it means the request was made but no response was received
  69. errorMessage = 'No response received from the server';
  70. } else if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
  71. // If there is a network error, such as DNS resolution or connection refused
  72. errorMessage = `Network error: ${error.message}`;
  73. } else {
  74. // If none of the above conditions are met,
  75. // it means there was an error setting up the request
  76. errorMessage = `Request setup error: ${error.message}`;
  77. }
  78. // Return an object containing the error message
  79. return { error: errorMessage };
  80. }
  81. };

🎉 AIGC聚集地

AIGC聚集地产品介绍