Mutugi
commited on
Commit
·
433b97d
1
Parent(s):
00698db
added app file
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import required libraries
|
| 2 |
+
import openai
|
| 3 |
+
import telegram
|
| 4 |
+
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
| 5 |
+
|
| 6 |
+
# Initialize GPT-3 model
|
| 7 |
+
openai.api_key = "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"
|
| 8 |
+
model_engine = "davinci"
|
| 9 |
+
|
| 10 |
+
# Set prompt and response length for chatbot
|
| 11 |
+
prompt = "Hello, I am a chatbot. How can I help you today?"
|
| 12 |
+
length = 32
|
| 13 |
+
|
| 14 |
+
# Create function to handle incoming messages
|
| 15 |
+
def handle_message(update, context):
|
| 16 |
+
# Store user's message in stateful history
|
| 17 |
+
history = context.user_data.get("history", [])
|
| 18 |
+
history.append(update.message.text)
|
| 19 |
+
context.user_data["history"] = history
|
| 20 |
+
|
| 21 |
+
# Use GPT-3 model to generate response based on user's message and previous conversation history
|
| 22 |
+
completions = openai.Completion.create(
|
| 23 |
+
engine=model_engine,
|
| 24 |
+
prompt=prompt + "\n" + "\n".join(history),
|
| 25 |
+
max_tokens=length,
|
| 26 |
+
n=1,
|
| 27 |
+
stop=None,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
)
|
| 30 |
+
response = completions.choices[0].text
|
| 31 |
+
|
| 32 |
+
# Send response back to user
|
| 33 |
+
update.message.reply_text(response)
|
| 34 |
+
|
| 35 |
+
# Set up Telegram bot
|
| 36 |
+
updater = Updater('5166541267:AAF75Dv9glLaaPAOu87hSuBr9gqKkk6URdQ', use_context=True)
|
| 37 |
+
dispatcher = updater.dispatcher
|
| 38 |
+
|
| 39 |
+
# Add message handler
|
| 40 |
+
message_handler = MessageHandler(Filters.text, handle_message)
|
| 41 |
+
# message_handler = MessageHandler(Filters.text & ~Filters.command, handle_message)
|
| 42 |
+
dispatcher.add_handler(message_handler)
|
| 43 |
+
|
| 44 |
+
# Start listening for incoming messages
|
| 45 |
+
updater.start_polling()
|
| 46 |
+
updater.idle()
|