Merge branch 'main' of https://github.com/bucolucas/cyclop
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
class PersonaTool:
|
||||||
|
def get_functions(self):
|
||||||
|
# Implementation to return functions in JSON format
|
||||||
|
pass
|
||||||
@@ -1,3 +1,15 @@
|
|||||||
|
"""
|
||||||
|
telegram_inference_bot.py
|
||||||
|
|
||||||
|
This module implements a Telegram bot that processes user messages and
|
||||||
|
images, interacts with the OpenAI API to provide intelligent responses,
|
||||||
|
and manages conversation history and temporary file storage for images.
|
||||||
|
|
||||||
|
It supports the following commands:
|
||||||
|
- /start: Initializes the bot and welcomes the user.
|
||||||
|
- /clear: Clears the user's conversation history and any uploaded images.
|
||||||
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import importlib
|
import importlib
|
||||||
@@ -62,10 +74,25 @@ for tool in tools:
|
|||||||
functions.extend(tool.get_functions())
|
functions.extend(tool.get_functions())
|
||||||
|
|
||||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
"""
|
||||||
|
Handles the /start command. Sends a welcome message to the user.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update (Update): Incoming update containing the message.
|
||||||
|
context (ContextTypes.DEFAULT_TYPE): Context for the callback.
|
||||||
|
"""
|
||||||
logging.info("Bot started")
|
logging.info("Bot started")
|
||||||
await update.message.reply_text("Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.")
|
await update.message.reply_text("Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.")
|
||||||
|
|
||||||
async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
"""
|
||||||
|
Handles the /clear command. Clears the user's conversation history
|
||||||
|
and any uploaded images.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update (Update): Incoming update containing the message.
|
||||||
|
context (ContextTypes.DEFAULT_TYPE): Context for the callback.
|
||||||
|
"""
|
||||||
user_id = update.effective_user.id
|
user_id = update.effective_user.id
|
||||||
if user_id in conversation_history:
|
if user_id in conversation_history:
|
||||||
del conversation_history[user_id]
|
del conversation_history[user_id]
|
||||||
@@ -76,6 +103,14 @@ async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
await update.message.reply_text("Conversation history and image cleared. Let's start fresh!")
|
await update.message.reply_text("Conversation history and image cleared. Let's start fresh!")
|
||||||
|
|
||||||
async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
"""
|
||||||
|
Handles image uploads from users. Downloads the largest photo to
|
||||||
|
a temporary file for further processing.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update (Update): Incoming update containing the image.
|
||||||
|
context (ContextTypes.DEFAULT_TYPE): Context for the callback.
|
||||||
|
"""
|
||||||
user_id = update.effective_user.id
|
user_id = update.effective_user.id
|
||||||
|
|
||||||
# Get the largest available photo
|
# Get the largest available photo
|
||||||
@@ -93,6 +128,14 @@ async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||||||
await update.message.reply_text("I've received your image. What would you like to know about it?")
|
await update.message.reply_text("I've received your image. What would you like to know about it?")
|
||||||
|
|
||||||
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
"""
|
||||||
|
Processes incoming text messages from users, calls the OpenAI API
|
||||||
|
for inference, and sends the assistant's reply back to the user.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
update (Update): Incoming update containing the text message.
|
||||||
|
context (ContextTypes.DEFAULT_TYPE): Context for the callback.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
user_id = update.effective_user.id
|
user_id = update.effective_user.id
|
||||||
user_message = update.message.text
|
user_message = update.message.text
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import openai
|
||||||
|
import json
|
||||||
|
from tools.base_tool import BaseTool
|
||||||
|
|
||||||
|
class PersonaTool(BaseTool):
|
||||||
|
def __init__(self, api_key: str):
|
||||||
|
super().__init__()
|
||||||
|
openai.api_key = api_key
|
||||||
|
|
||||||
|
def generate_response(self, persona_description: str, query: str) -> str:
|
||||||
|
"""
|
||||||
|
Makes a call to the OpenAI API using the persona as a system prompt.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
persona_description (str): Description of the persona.
|
||||||
|
query (str): Query to be processed.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The response generated by the OpenAI API.
|
||||||
|
"""
|
||||||
|
response = openai.ChatCompletion.create(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[
|
||||||
|
{"role": "system", "content": persona_description},
|
||||||
|
{"role": "user", "content": query}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return response.choices[0].message['content']
|
||||||
|
|
||||||
|
def get_functions(self):
|
||||||
|
return json.dumps({
|
||||||
|
"functions": [
|
||||||
|
{
|
||||||
|
"name": "generate_response",
|
||||||
|
"description": "Generates a response based on a persona description and a user query.",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"persona_description": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Description of the persona."
|
||||||
|
},
|
||||||
|
"query": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "User's query to be processed."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["persona_description", "query"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
def execute(self, function_name, **kwargs):
|
||||||
|
if function_name == "generate_response":
|
||||||
|
return self.generate_response(kwargs.get("persona_description"), kwargs.get("query"))
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Function {function_name} not found")
|
||||||
Reference in New Issue
Block a user