forked from dreamlimit666/Line-MewBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_chat.py
125 lines (109 loc) · 4.48 KB
/
index_chat.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from flask import Flask, request, abort, jsonify
import os
import openai
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
# Set OpenAI API details
openai.api_type = "azure"
openai.api_version = "2023-05-15"
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_base = os.getenv("OPENAI_API_BASE")
bot_token = os.getenv("SLACK_TOKEN")
verification_token = os.getenv("V_TOKEN")
slack_client = WebClient(token=bot_token)
app = Flask(__name__)
# Initialize messages list with the system message
messages = [
{"role": "system", "content": '''{{
"Expert": "LangGPT",
"Profile": {
"Author": "YZFly",
"Version": "1.0",
"Language": "English",
"Description": "You are an expert in .NET Core and Entity Framework Core. Your goal is to provide assistance and guidance on using these technologies effectively."
},
"Skills": [
"Proficiency in .NET Core",
"Expertise in Entity Framework Core",
"Troubleshooting and problem-solving skills"
],
"Goals": [
"Help users with .NET Core and Entity Framework Core queries",
"Provide best practices and code examples",
"Explain complex concepts in a simple manner"
],
"Constraints": [
"Don't break character under any circumstance.",
"Don't talk nonsense and make up facts.",
"You are a .NET Core and Entity Framework Core expert.",
"You will strictly follow these constraints.",
"You will try your best to accomplish these goals."
]
}}'''},
]
# This function takes a chat message as input, appends it to the messages list, sends the recent messages to the OpenAI API, and returns the assistant's response.
def aoai_chat_model(chat):
# Append the user's message to the messages list
messages.append({"role": "user", "content": chat})
# Only send the last 5 messages to the API
recent_messages = messages[-5:]
# Send the recent messages to the OpenAI API and get the response
response_chat = openai.ChatCompletion.create(
engine="GPT35_Dev",
messages=recent_messages,
temperature=0.7,
max_tokens=500,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None
)
# Append the assistant's response to the messages list
messages.append({"role": "assistant", "content": response_chat['choices'][0]['message']['content'].strip()})
return response_chat['choices'][0]['message']['content'].strip()
# Initialize Line API with access token and channel secret
line_bot_api = LineBotApi(os.getenv('LINE_ACCESS_TOKEN'))
handler1 = WebhookHandler(os.getenv('LINE_CHANNEL_SECRET'))
# This route serves as a health check or landing page for the web app.
@app.route("/")
def mewobot():
return 'Cat Time!!!'
@app.route("/gpt/chat", methods=["POST"])
def gptchat():
if request.form['token'] == verification_token:
user_message = request.form.get('text')
text = aoai_chat_model(user_message)
#try:
#slack_client.chat_postMessage(channel=request.json.get('event')['channel'], text=text)
#except SlackApiError as e:
#print("消息發送失敗:", e.response["error"])
return jsonify({"response_type": "in_channel","text": text})
@app.route("/slack/events", methods=["POST"])
def slack_events():
challenge = request.json.get('challenge')
return jsonify({'challenge': challenge})
# This route handles callbacks from the Line API, verifies the signature, and passes the request body to the handler.
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler1.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
# This event handler is triggered when a message event is received from the Line API. It sends the user's message to the OpenAI chat model and replies with the assistant's response.
@handler1.add(MessageEvent, message=TextMessage)
def handle_message(event):
if event.message.text.startswith("gpt "):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=aoai_chat_model(event.message.text))
)
if __name__ == "__main__":
app.run()