-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
70 lines (56 loc) · 1.92 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import google.generativeai as palm
import telebot
# Your Telegram bot token
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
# Configure the generative AI API key
palm.configure(api_key="YOUR_GENERATIVE_AI_API_KEY")
# Create a Telegram bot instance
bot = telebot.TeleBot(TOKEN)
# Handle /start, /help, and /hello commands
@bot.message_handler(commands=['start', 'help', 'hello'])
def send_welcome(message):
name = message.from_user.first_name
bot.reply_to(message, f"Hello {name}! Ask me about anything")
# Handle all other messages
@bot.message_handler(func=lambda message: True)
def handle_search_product(message):
name = message.from_user.first_name
msg = message.text
# Check for empty message
if msg == "":
bot.reply_to(message, "Sorry, you mustn't write an empty message")
try:
bot.reply_to(message, f"Please wait a moment, {name}, before sending another message")
# Default parameters for the generative AI model
defaults = {
'model': 'models/chat-bison-001',
'temperature': 0.25,
'candidate_count': 1,
'top_k': 40,
'top_p': 0.95,
}
context = ""
examples = [
[
" ",
]
]
examples[0].append(str(msg))
messages = []
messages.append("NEXT REQUEST")
# Generate response using the generative AI model
response = palm.chat(
**defaults,
context=context,
examples=examples,
messages=messages
)
print(response.messages)
print(response.last)
bot.reply_to(message, response.last)
except Exception as e:
bot.reply_to(message, str(e))
bot.reply_to(message, "Sorry, an error occurred while processing your request.")
# Start the bot's polling loop
if __name__ == "__main__":
bot.polling()