Git Product home page Git Product logo

gpt-ai-assistant's Introduction

GPT AI Assistant

GPT AI Assistant 是基於 OpenAI API 與 LINE Messaging API 實作的範例應用程式,透過安裝步驟,你可以使用 LINE 手機應用程式與你專屬的 AI 助理聊天。

範例

安裝步驟

  • 登入 OpenAI 平台,或註冊一個新的帳號。
    • 生成一個 OpenAI 的 API key
  • 登入 LINE 平台,或註冊一個新的帳號。
    • 新增一個提供者(Provider),例如「My Provider」。
    • 在「My Provider」新增一個類型為「Messaging API」的頻道(Channel),例如「My AI Assistant」。
    • 在「My AI Assistant」點選「Messaging API」頁籤,生成一個頻道的 channel access token
  • 登入 GitHub 平台,或註冊一個新的帳號。
    • 進到 gpt-ai-assistant 專案頁面,點選「Fork」按鈕,將原始碼複製到自己的儲存庫。
  • 登入 Vercel 平台,或註冊一個新的帳號。
    • 點選「Create a New Project」按鈕,建立一個新專案。
    • 點選「Import」按鈕,將 gpt-ai-assistant 專案匯入。
    • 點選「Environment Variables」頁籤,新增以下環境變數:
    • 點選「Deploy」按鈕,等待部署完成。
    • 點選「Domains」按鈕,複製應用程式網址,例如「https://gpt-ai-assistant.vercel.app/」。
  • 回到 LINE 平台。
    • 進到「My AI Assistant」頻道頁面,點選「Messaging API」頁籤,設置「Webhook URL」,例如「https://gpt-ai-assistant.vercel.app/webhook」,點選「Update」按鈕。
    • 點選「Verify」按鈕,驗證是否呼叫成功。
    • 將「Use webhook」功能打開。
    • 將「Auto-reply messages」功能關閉。
    • 將「Greeting messages」功能關閉。
    • 使用 LINE 手機應用程式掃描 QR code,加入好友。
  • 開始與你專屬的 AI 助理聊天!

更新程式

進到 gpt-ai-assistant 專案頁面,點選「Sync fork」選單,再點選「Update branch」或「Discard commit」按鈕,以同步最新的程式碼到自己的儲存庫。

環境變數

在 Vercel 平台上新增或修改環境變數,以變更程式設定。

名字 說明
APP_DEBUG 決定是否印出訊息,可設置為 truefalse
OPENAI_API_KEY OpenAI 的 API key
OPENAI_COMPLETION_INIT_LANG 決定初始語言,可設置為 zhen
OPENAI_COMPLETION_MODEL 參見 model 說明
OPENAI_COMPLETION_TEMPERATURE 參見 temperature 說明
OPENAI_COMPLETION_MAX_TOKENS 參見 max_tokens 說明
OPENAI_COMPLETION_FREQUENCY_PENALTY 參見 frequency_penalty 說明
OPENAI_COMPLETION_PRESENCE_PENALTY 參見 presence_penalty 說明
LINE_API_KEY LINE 的 channel access token
LINE_API_SECRET LINE 的 channel secret

點選「Redeploy」按鈕,以重新部署。

除錯

首先在 Vercel 平台上檢查專案的環境變數是否填寫正確。

如果有進行變更,點選「Redeploy」按鈕,以重新部署。

進一步的除錯方式是,點選「View Function Logs」按鈕,以查看應用程式的錯誤訊息。

開發

下載專案。

[email protected]:memochou1993/gpt-ai-assistant.git

進到專案目錄。

cd gpt-ai-assistant

安裝依賴套件。

npm ci

建立 .env 檔。

cp .env.example .env

設置相關環境變數。

APP_ENV=local
APP_DEBUG=true
APP_PORT=3000
OPENAI_API_KEY=<your_openai_api_key>
LINE_API_KEY=<empty_string>

測試

執行以下指令,執行測試,並且向 OpenAI 伺服器發送請求。

npm run test

查看結果。

> [email protected] test
> jest

  console.info
    === 00000 ===
    
    AI: 嗨!我可以怎麼幫助你?
    Human: 嗨?
    AI: 你好!有什麼可以幫助你的嗎?

      at Assistant.info [as debug] (assistant/assistant.js:55:28)

 PASS  assistant/index.test.js
  ✓ assistant works (1689 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.579 s, estimated 4 s
Ran all test suites.

模擬請求

執行以下指令,啟動一個 Local 伺服器。

npm run dev

再執行以下指令,模擬 LINE 伺服器向 Local 伺服器發送請求,再由 Local 伺服器向 OpenAI 伺服器發送請求。

curl --request POST \
  --url http://localhost:3000/webhook \
  --header 'Content-Type: application/json' \
  --data '{
    "events": [
      {
        "type": "message",
        "source": {
          "type": "user",
          "userId": "00000"
        },
        "message": {
            "type": "text",
            "text": "我是誰"
          }
        }
      ]
    }'

查看結果。

> [email protected] dev
> node api/index.js

=== 00000 ===

AI: 嗨!我可以怎麼幫助你?
Human: 我是誰?
AI: 你是一個人,一個有意識的生物!

相關專案

貢獻者

gpt-ai-assistant's People

Contributors

androchentw avatar memochou1993 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.