Files
cyclop/chatgpt_telegram_inference_bot.py
T

106 lines
3.7 KiB
Python
Raw Normal View History

2024-08-19 10:24:17 -05:00
import json
import os
import logging
from base_telegram_inference_bot import BaseTelegramInferenceBot
from telegram_helper import TelegramHelper
2024-08-19 10:36:03 -05:00
from openai import OpenAI
2024-08-19 10:24:17 -05:00
class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
def __init__(self):
super().__init__()
2025-06-01 11:50:12 -05:00
self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"), base_url="http://localhost:1234/v1")
self.model = "qwen3-1.7b"
self.max_tokens = 32768
def get_chat_response(self, messages):
response = self.client.chat.completions.create(
model=self.model,
messages=[{"role": "system", "content": self.system_prompt}] + messages,
2025-06-01 11:50:12 -05:00
tools=self.functions,
tool_choice = "auto",
max_tokens=self.max_tokens
)
return response
async def handle_message(self, user_id, user_message):
if user_id not in self.conversation_history:
self.conversation_history[user_id] = []
self.conversation_history[user_id].append({"role": "user", "content": user_message})
messages = self.conversation_history[user_id]
response = self.get_chat_response(messages)
2024-08-19 13:38:39 -05:00
tool_calls = []
2024-08-19 13:41:12 -05:00
2024-08-19 13:38:39 -05:00
for message_part in response.choices:
2025-06-01 11:50:12 -05:00
if message_part.finish_reason == "tool_calls":
tool_calls.extend(message_part.message.tool_calls)
2024-08-19 14:30:17 -05:00
messages.append(response.choices[0].message)
2024-08-19 10:24:17 -05:00
tool_use_count = 0
2024-08-19 16:23:58 -05:00
while len(tool_calls) > 0 and tool_use_count < 500:
2024-08-19 10:24:17 -05:00
tool_use_results = []
2024-08-19 13:38:39 -05:00
while len(tool_calls) > 0:
2025-06-01 11:50:12 -05:00
tool_call = tool_calls.pop(0).function
2024-08-19 13:38:39 -05:00
tool_response = self.call_tool(tool_call.name, tool_call.arguments)
2025-06-01 11:50:12 -05:00
try:
tool_use_results.append({"role": "tool", "name": tool_call.name, "content": tool_response})
except (TypeError, ValueError) as e:
logging.error(f"Failed to serialize tool response: {e}")
tool_use_results.append({"role": "function", "name": tool_call.name, "content": "Serialization error"})
2024-08-19 10:24:17 -05:00
messages.extend(tool_use_results)
2024-08-19 10:24:17 -05:00
2024-08-19 14:30:17 -05:00
response = self.get_chat_response(messages)
2024-08-19 13:38:39 -05:00
for message_part in response.choices:
if message_part.finish_reason == "function_call":
tool_calls.append(message_part.message.function_call)
2024-08-19 14:30:17 -05:00
messages.append(response.choices[0].message)
2024-08-19 10:24:17 -05:00
tool_use_count += 1
2024-08-19 10:24:17 -05:00
if len(self.conversation_history[user_id]) > 20:
self.conversation_history[user_id] = self.conversation_history[user_id][-20:]
2024-08-19 14:30:17 -05:00
return messages[-1].content
async def start(self):
logging.info("Bot started")
async def clear(self, user_id):
super().clear_conversation(user_id)
logging.info(f"Cleared conversation history for user {user_id}")
async def status(self):
return f"Currently using: {self.model}"
async def abort_processing(self, user_id):
if user_id in self.processing_status:
self.processing_status[user_id]["processing"] = False
await self.clear(user_id)
return "Processing aborted."
else:
return "No active processing to abort."
async def switch_model(self):
2025-06-01 11:50:12 -05:00
if self.model == "qwen3-4b":
self.model = "qwen3-30b-a3b"
# self.max_tokens = 4096
else:
2025-06-01 11:50:12 -05:00
self.model = "qwen3-4b"
# self.max_tokens = 16384
logging.info(f"Switched to model: {self.model}")
return f"Switched to model: {self.model}"
def main():
bot = ChatGPTTelegramInferenceBot()
telegram_helper = TelegramHelper(bot)
telegram_helper.run()
2024-08-19 10:24:17 -05:00
if __name__ == '__main__':
main()