-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathpython-chatgpt-bot.py
54 lines (40 loc) · 1.13 KB
/
python-chatgpt-bot.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
#Author: Arman Idrisi
import telebot
import openai
#Bot Api Token
API_TOKEN = ''
#Openai Api Key
openai.api_key=""
bot = telebot.TeleBot(API_TOKEN)
#Generate The Response
def get_response(msg):
completion = openai.Completion.create(
engine="text-davinci-003",
prompt=msg,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
return completion.choices[0].text
# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
# bot.send_message(message.chat.id,message.text)
bot.send_message(message.chat.id, """\
Hi there, I am A Ai ChatBot.
I am here to Give Answers Of Your Question.
I Am Created Using Chatgpt Api !
Use /ask To Ask Questions\
""")
#Handle The '/ask'
@bot.message_handler(commands=['ask'])
def first_process(message):
bot.send_message(message.chat.id,"Send Me your Question")
bot.register_next_step_handler(message,second_process)
def again_send(message):
bot.register_next_step_handler(message,second_process)
def second_process(message):
bot.send_message(message.chat.id,get_response(message.text))
again_send(message)
bot.infinity_polling()