-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lmstudiotelegram.py
55 lines (42 loc) · 1.62 KB
/
Lmstudiotelegram.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
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from openai import OpenAI
# Configure logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
# Initialize OpenAI client for local model
client = OpenAI(base_url="http://localhost:1234/v1", api_key="not-needed")
# Bot token and allowed chat IDs
BOT_TOKEN = 'your-bot-token'
ALLOWED_CHAT_IDS = [] # Replace with the allowed chat IDs
def chat(update, context):
chat_id = update.message.chat_id
# Check if the chat ID is allowed
if chat_id not in ALLOWED_CHAT_IDS:
logging.info(f"Unauthorized access attempt from chat ID: {chat_id}")
return
user_message = update.message.text
# Process and respond to the message
try:
logging.info("Sending message to local model: %s", user_message)
completion = client.chat.completions.create(
model="local-model",
messages=[
{"role": "user", "content": user_message}
],
temperature=0.7,
)
reply = completion.choices[0].message.content
update.message.reply_text(reply)
except Exception as e:
logging.error(f"Error processing message: {e}")
update.message.reply_text("An error occurred while processing your request.")
def main():
updater = Updater(BOT_TOKEN, use_context=True)
dp = updater.dispatcher
# Message handler for text messages
dp.add_handler(MessageHandler(Filters.text, chat))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()