Skip to content

Commit

Permalink
Merge pull request #17 from willismax/master
Browse files Browse the repository at this point in the history
修正至可用狀態
  • Loading branch information
howarder3 authored Dec 9, 2023
2 parents 858b7e2 + d97d605 commit 9f6a8c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# GPT-Linebot using python flask on vercel

* last updated: 2022/12/15
* last updated: 2023/10/9
- 更新說明 by [willismax](willismax/GPT-Linebot-python-flask-on-vercel)
- vercel.json修正,改為第2版
- 修正單純安裝Flask==2.2.2 會與 Werkzeug 衝突的問題,在 requirements.txt 添加 Werkzeug==2.3.7 ([參考stack overflow](https://stackoverflow.com/questions/77213053/importerror-cannot-import-name-url-quote-from-werkzeug-urls))

本文同步更新至我的個人網站:[【Side Project】(全圖文教學) 用 Python flask 實作類似 ChatGPT 的 Linebot,並部屬至 vercel 上](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/)

> `本篇教學無經驗的新手也可學習,無須寫任何程式。`
>
Expand Down Expand Up @@ -233,12 +235,16 @@ Import Git Repository,選擇你剛剛 fork 的專案 import

# 靈感來源
* 本文同步更新至我的個人網站:[【Side Project】(全圖文教學) 用 Python flask 實作類似 ChatGPT 的 Linebot,並部屬至 vercel 上](https://www.wongwonggoods.com/portfolio/personal_project/gpt-linebot-python-flask-for-vercel/)

* 感謝 [memochou1993/gpt-ai-assistant](https://github.com/memochou1993/gpt-ai-assistant?fbclid=IwAR25uqLdKoDKEQd591fSjyM2sDJJR3Xb-VgcXDIFV_7i3RMWWv2oiyG26RQ) 提供的 node.js 版本串接 vercel 示範,讓我有了想把 python linebot 也串進 vercel 的靈感,(目前感覺下來,免費又好用(?))
* 感謝 [Lanznx/HealthLineBot](https://github.com/Lanznx/HealthLineBot) 給了一個很好的 python Django 範例,然而我不會 Django XD,vercel 官方文件好像也沒有提到這部份,總之後來就改成了 flask 版本,也符合 linebot 推薦的範例。



# 參考資料

* Line 官方提供的 python flask 製作 linebot 的 sample code [line/line-bot-sdk-python](https://github.com/line/line-bot-sdk-python)
* Vercel 官方提供的 python runtime Flask 範例 [Deploy an example with Flask](https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python#python-version)


22 changes: 8 additions & 14 deletions api/chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
from api.prompt import Prompt

import os
import openai
from openai import OpenAI
client = OpenAI()

openai.api_key = os.getenv("OPENAI_API_KEY")
client.api_key = os.getenv("OPENAI_API_KEY")


class ChatGPT:
def __init__(self):
self.prompt = Prompt()
self.model = os.getenv("OPENAI_MODEL", default = "text-davinci-003")
self.model = os.getenv("OPENAI_MODEL", default = "gpt-4-1106-preview")
self.temperature = float(os.getenv("OPENAI_TEMPERATURE", default = 0))
self.frequency_penalty = float(os.getenv("OPENAI_FREQUENCY_PENALTY", default = 0))
self.presence_penalty = float(os.getenv("OPENAI_PRESENCE_PENALTY", default = 0.6))
self.max_tokens = int(os.getenv("OPENAI_MAX_TOKENS", default = 240))
self.max_tokens = int(os.getenv("OPENAI_MAX_TOKENS", default = 500))

def get_response(self):
response = openai.Completion.create(
response = client.chat.completions.create(
model=self.model,
prompt=self.prompt.generate_prompt(),
temperature=self.temperature,
frequency_penalty=self.frequency_penalty,
presence_penalty=self.presence_penalty,
max_tokens=self.max_tokens
messages=self.prompt.generate_prompt(),
)
return response['choices'][0]['text'].strip()
return response.choices[0].message.content

def add_msg(self, text):
self.prompt.add_msg(text)
20 changes: 11 additions & 9 deletions api/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

chat_language = os.getenv("INIT_LANGUAGE", default = "zh")

MSG_LIST_LIMIT = int(os.getenv("MSG_LIST_LIMIT", default = 20))
MSG_LIST_LIMIT = int(os.getenv("MSG_LIST_LIMIT", default = 7))
LANGUAGE_TABLE = {
"zh": "哈囉!",
"en": "Hello!"
}

AI_GUIDELINES = '你是一個AI助教,會用蘇格拉底教學法代替老師初步回應,如果有需要會提醒學生跟老師確認'

class Prompt:
def __init__(self):
self.msg_list = []
self.msg_list.append(f"AI:{LANGUAGE_TABLE[chat_language]}")

self.msg_list.append(
{
"role": "system",
"content": f"{LANGUAGE_TABLE[chat_language]}, {AI_GUIDELINES})"
})
def add_msg(self, new_msg):
if len(self.msg_list) >= MSG_LIST_LIMIT:
self.remove_msg()
self.msg_list.append(new_msg)

def remove_msg(self):
self.msg_list.pop(0)
self.msg_list.pop(0)
self.msg_list.append({"role": "user", "content": new_msg})

def generate_prompt(self):
return '\n'.join(self.msg_list)
return self.msg_list
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Flask==2.2.2
flask==2.2.2
Werkzeug==2.3.7
line-bot-sdk
openai
openai
19 changes: 10 additions & 9 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"version": 2,
"builds": [
{
"src": "api/index.py",
"use": "@vercel/python"
}
{
"src": "api/index.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "api/index.py"
}
{
"src": "/(.*)",
"dest": "api/index.py"
}
]
}
}

1 comment on commit 9f6a8c6

@vercel
Copy link

@vercel vercel bot commented on 9f6a8c6 Dec 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.