-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
108 lines (89 loc) · 3.2 KB
/
app.py
File metadata and controls
108 lines (89 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
#======python的函數庫==========
import tempfile, os
import openai
import time
import traceback
import json
import random
import requests
from datetime import datetime,timezone,timedelta
app = Flask(__name__)
static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
# Set up LINE_bot
line_bot_api = LineBotApi(os.getenv('CHANNEL_ACCESS_TOKEN'))
handler = WebhookHandler(os.getenv('CHANNEL_SECRET'))
# OPENAI API Key初始化設定
open_ai_api_key = os.getenv('OpenAI_API_KEY')
open_ai_endpoint = os.getenv('OpenAI_ENDPOINT')
deployment_name = os.getenv('OpenAI_DEPLOY_NAME')
openai.api_base = open_ai_endpoint
headers = {
"Content-Type": "application/json",
"api-key": open_ai_api_key,
}
# 連接Azure OpenAI的Chatgpt
def Chatgpt_response(prompt):
# Define the payload for the request
# You can modify the system message and the user prompt as needed
payload = {
"model": "gpt-4o-mini", # You can switch between "gpt-4" or "gpt-3.5-turbo"
"messages": [
{"role": "system", "content": "You are a helpful assistant."}, # Context setting
{"role": "user", "content": prompt} # Replace with your actual prompt
],
"temperature": 0.7, # Modify this value to adjust the creativity level of the model
"max_tokens": 1000, # Control the length of the response
"top_p": 1.0,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}
# Send the request to OpenAI's API
response = requests.post(open_ai_endpoint, headers=headers, json=payload)
# Check if the request was successful
if response.status_code == 200:
# Parse and print the response from GPT
result = response.json()
return result['choices'][0]['message']['content']
else:
# Print the error if the request was unsuccessful
print(f"Error {response.status_code}: {response.text}")
# 監聽所有來自 /callback 的 Post Request
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
# 處理文字訊息
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
msg = event.message.text
if msg[0]=='*':
try:
gpt_answer = Chatgpt_response(msg)
print(gpt_answer)
line_bot_api.reply_message(event.reply_token, TextSendMessage(gpt_answer))
except:
print(traceback.format_exc())
line_bot_api.reply_message(event.reply_token, TextSendMessage('Please retry later'))
@handler.add(PostbackEvent)
def handle_message(event):
print(event.postback.data)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)