-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
53 lines (43 loc) · 1.75 KB
/
main.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
import os
import logging
import requests
from telegram import Update
from telegram.ext import MessageHandler, ApplicationBuilder, ContextTypes, filters
from dotenv import load_dotenv
import openai
load_dotenv()
# telegram
BOT_TOKEN = os.environ['BOT_TOKEN']
# openai
openai.organization = "org-xAfG7yH1ydHAjgbRGCeSFfrO"
openai.api_key = os.environ['OPENAI_API_KEY']
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
# completion model (text-davinci)
# def getResponse(prompt):
# response = requests.post(
# 'https://api.openai.com/v1/completions',
# headers={'Authorization': 'Bearer ' + openai.api_key},
# json={'model': "text-davinci-002", 'prompt': prompt, 'max_tokens': 512, 'temperature': 0.5 } )
# reply = response.json()['choices'][0]['text']
# print("AI: "+ reply)
# return reply
# chat completion (chat-3.5-turbo)
def getResponse(prompt):
response = requests.post(
'https://api.openai.com/v1/chat/completions',
headers={'Authorization': 'Bearer ' + openai.api_key},
json={'model': "gpt-3.5-turbo", 'messages': [{'role': 'user', 'content': prompt}], 'max_tokens': 512, 'temperature': 0.5 } )
reply = response.json()['choices'][0]['message']['content']
print("AI: "+ reply)
return reply
async def chatBot(update: Update, context: ContextTypes.DEFAULT_TYPE):
print("User: " + update.message.text)
reply = getResponse(update.message.text)
await context.bot.send_message(chat_id=update.effective_chat.id, text=reply)
if __name__ == '__main__':
application = ApplicationBuilder().token(BOT_TOKEN).build()
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), chatBot))
application.run_polling()