Testing with qwen and updating tools

This commit is contained in:
2025-06-01 11:50:12 -05:00
parent 4c4f9b7f2c
commit 7844f26d59
6 changed files with 418 additions and 311 deletions
+5 -2
View File
@@ -11,7 +11,8 @@ class BaseTelegramInferenceBot(ABC):
self.processing_status = {}
self.system_prompt = self.load_system_prompt()
self.tools, self.functions = self.load_functions()
print(f'Github Token: {os.environ.get("GITHUB_TOKEN")}')
print(f'System Prompt: {os.environ.get("SYSTEM_PROMPT_PATH")}')
print(f'Github Repository: {os.environ.get("GITHUB_REPOSITORY")}')
@staticmethod
def load_system_prompt():
@@ -59,9 +60,11 @@ class BaseTelegramInferenceBot(ABC):
function_name = function_call_name
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}")
for tool in self.tools:
if function_name in [f["name"] for f in tool.get_functions()]:
for function in tool.get_functions():
if function["function"]["name"] == function_name:
return tool.execute(function_name, **function_args)
@abstractmethod
async def start(self):
pass
+18 -14
View File
@@ -8,16 +8,16 @@ from openai import OpenAI
class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
def __init__(self):
super().__init__()
self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
self.model = "gpt-4o-mini"
self.max_tokens = 16384
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,
functions=self.functions,
function_call="auto",
tools=self.functions,
tool_choice = "auto",
max_tokens=self.max_tokens
)
return response
@@ -34,8 +34,8 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
tool_calls = []
for message_part in response.choices:
if message_part.finish_reason == "function_call":
tool_calls.append(message_part.message.function_call)
if message_part.finish_reason == "tool_calls":
tool_calls.extend(message_part.message.tool_calls)
messages.append(response.choices[0].message)
@@ -44,9 +44,13 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
tool_use_results = []
while len(tool_calls) > 0:
tool_call = tool_calls.pop(0)
tool_call = tool_calls.pop(0).function
tool_response = self.call_tool(tool_call.name, tool_call.arguments)
tool_use_results.append({"role": "function", "name": tool_call.name, "content": json.dumps(tool_response)})
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"})
messages.extend(tool_use_results)
@@ -84,12 +88,12 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
return "No active processing to abort."
async def switch_model(self):
if self.model == "gpt-4o-mini":
self.model = "gpt-4o"
self.max_tokens = 4096
if self.model == "qwen3-4b":
self.model = "qwen3-30b-a3b"
# self.max_tokens = 4096
else:
self.model = "gpt-4o-mini"
self.max_tokens = 16384
self.model = "qwen3-4b"
# self.max_tokens = 16384
logging.info(f"Switched to model: {self.model}")
return f"Switched to model: {self.model}"
+3 -1
View File
@@ -63,7 +63,9 @@ class TelegramHelper:
await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id)
del self.bot.processing_status[user_id]
await update.message.reply_text(response)
response = response.replace("<think>", "<blockquote expandable><b>Thinking...</b>").replace("</think>", "</blockquote>")
# Return response as html message
await update.message.reply_html(response)
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
+91 -6
View File
@@ -47,6 +47,8 @@ class GitHubTool(BaseTool):
def get_functions(self):
return [
{
"type": "function",
"function": {
"name": "read_file",
"description": "Read a file from the repository",
"parameters": {
@@ -59,8 +61,11 @@ class GitHubTool(BaseTool):
},
"required": ["path"]
}
}
},
{
"type": "function",
"function": {
"name": "list_files",
"description": "List files in a directory of the repository",
"parameters": {
@@ -73,8 +78,11 @@ class GitHubTool(BaseTool):
},
"required": ["path"]
}
}
},
{
"type": "function",
"function": {
"name": "search_code",
"description": "Search for code in the repository",
"parameters": {
@@ -87,8 +95,11 @@ class GitHubTool(BaseTool):
},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
"name": "create_branch",
"description": "Create a new branch in the repository",
"parameters": {
@@ -106,8 +117,11 @@ class GitHubTool(BaseTool):
},
"required": ["branch_name"]
}
}
},
{
"type": "function",
"function": {
"name": "commit_file",
"description": "Commit a file to a branch (not main)",
"parameters": {
@@ -128,8 +142,11 @@ class GitHubTool(BaseTool):
},
"required": ["file_path", "commit_message", "content"]
}
}
},
{
"type": "function",
"function": {
"name": "create_pull_request",
"description": "Create a pull request",
"parameters": {
@@ -151,8 +168,11 @@ class GitHubTool(BaseTool):
},
"required": ["title", "body"]
}
}
},
{
"type": "function",
"function": {
"name": "get_commit_history",
"description": "Get commit history for a file",
"parameters": {
@@ -170,8 +190,11 @@ class GitHubTool(BaseTool):
},
"required": ["file_path"]
}
}
},
{
"type": "function",
"function": {
"name": "get_branch_sha",
"description": "Get the SHA of the latest commit on a branch",
"parameters": {
@@ -184,13 +207,19 @@ class GitHubTool(BaseTool):
},
"required": ["branch"]
}
}
},
{
"type": "function",
"function": {
"name": "get_current_branch",
"description": "Get the name of the current branch",
"parameters": {}
"parameters": { "type": "object", "properties": {} }
}
},
{
"type": "function",
"function": {
"name": "set_current_branch",
"description": "Set the current branch",
"parameters": {
@@ -203,8 +232,11 @@ class GitHubTool(BaseTool):
},
"required": ["branch_name"]
}
}
},
{
"type": "function",
"function": {
"name": "get_file_at_commit",
"description": "Get the contents of a file at a specific commit",
"parameters": {
@@ -221,8 +253,11 @@ class GitHubTool(BaseTool):
},
"required": ["file_path", "commit_sha"]
}
}
},
{
"type": "function",
"function": {
"name": "list_branches",
"description": "List all branches in the repository",
"parameters": {
@@ -240,8 +275,11 @@ class GitHubTool(BaseTool):
}
}
}
}
},
{
"type": "function",
"function": {
"name": "approve_pull_request",
"description": "Approve a pull request",
"parameters": {
@@ -254,8 +292,11 @@ class GitHubTool(BaseTool):
},
"required": ["pull_number"]
}
}
},
{
"type": "function",
"function": {
"name": "close_pull_request",
"description": "Close a pull request",
"parameters": {
@@ -268,8 +309,11 @@ class GitHubTool(BaseTool):
},
"required": ["pull_number"]
}
}
},
{
"type": "function",
"function": {
"name": "merge_pull_request",
"description": "Merge a pull request",
"parameters": {
@@ -298,8 +342,11 @@ class GitHubTool(BaseTool):
},
"required": ["pull_number"]
}
}
},
{
"type": "function",
"function": {
"name": "delete_branch",
"description": "Delete a branch",
"parameters": {
@@ -312,8 +359,11 @@ class GitHubTool(BaseTool):
},
"required": ["branch_name"]
}
}
},
{
"type": "function",
"function": {
"name": "get_issue_details",
"description": "Get details of a specific issue",
"parameters": {
@@ -326,8 +376,11 @@ class GitHubTool(BaseTool):
},
"required": ["issue_number"]
}
}
},
{
"type": "function",
"function": {
"name": "create_issue",
"description": "Create a new issue in the repository",
"parameters": {
@@ -351,8 +404,11 @@ class GitHubTool(BaseTool):
},
"required": ["title", "body"]
}
}
},
{
"type": "function",
"function": {
"name": "list_issues",
"description": "List issues in the repository",
"parameters": {
@@ -376,8 +432,11 @@ class GitHubTool(BaseTool):
}
}
}
}
},
{
"type": "function",
"function": {
"name": "add_issue_comment",
"description": "Add a comment to an issue",
"parameters": {
@@ -394,8 +453,11 @@ class GitHubTool(BaseTool):
},
"required": ["issue_number", "comment"]
}
}
},
{
"type": "function",
"function": {
"name": "get_issue_comments",
"description": "Get comments for an issue",
"parameters": {
@@ -408,8 +470,11 @@ class GitHubTool(BaseTool):
},
"required": ["issue_number"]
}
}
},
{
"type": "function",
"function": {
"name": "create_project_board",
"description": "Create a new project board",
"parameters": {
@@ -426,8 +491,11 @@ class GitHubTool(BaseTool):
},
"required": ["name"]
}
}
},
{
"type": "function",
"function": {
"name": "create_project_column",
"description": "Create a new column in a project board",
"parameters": {
@@ -444,8 +512,11 @@ class GitHubTool(BaseTool):
},
"required": ["project_id", "column_name"]
}
}
},
{
"type": "function",
"function": {
"name": "create_project_card",
"description": "Create a new card in a project column",
"parameters": {
@@ -462,8 +533,11 @@ class GitHubTool(BaseTool):
},
"required": ["column_id", "note"]
}
}
},
{
"type": "function",
"function": {
"name": "move_project_card",
"description": "Move a card to a new position",
"parameters": {
@@ -484,8 +558,11 @@ class GitHubTool(BaseTool):
},
"required": ["card_id", "position", "column_id"]
}
}
},
{
"type": "function",
"function": {
"name": "link_issue_to_project_card",
"description": "Link an issue or pull request to a project card",
"parameters": {
@@ -506,18 +583,24 @@ class GitHubTool(BaseTool):
},
"required": ["card_id", "content_id", "content_type"]
}
}
},
{
"type": "function",
"function": {
"name": "list_project_boards",
"description": "List project boards associated with the repository",
"parameters": {}
"parameters": { "type": "object", "properties": {} }
}
},
{
"type": "function",
"function": {
"name": "view_project_board_items",
"description": "View items (columns and cards) in a specific project board",
"parameters": {
"type": "object",
"properties" : {
"properties": {
"project_id": {
"type": "integer",
"description": "ID of the project board"
@@ -526,6 +609,7 @@ class GitHubTool(BaseTool):
"required": ["project_id"]
}
}
}
]
@@ -712,10 +796,11 @@ class GitHubTool(BaseTool):
url = f"{self.base_url}/repos/{self.repo}/contents/{path}"
response = requests.get(url, headers=self.headers, params={"ref": self.current_branch})
if response.status_code == 200:
files = [item["name"] for item in response.json() if item["type"] == "file"]
directories = [item["name"] for item in response.json() if item["type"] == "dir"]
files = [{"type": "file", "name": item["name"]} for item in response.json() if item["type"] == "file"]
directories = [{"type": "directory", "name": item["name"]} for item in response.json() if item["type"] == "dir"]
self.logger.info(f"Successfully listed files and directories in {path}")
return {"files": files, "directories": directories}
files.extend(directories)
return files
else:
error_message = f"Error listing files: {response.status_code}"
self.logger.error(error_message)
+4
View File
@@ -33,8 +33,11 @@ class LogTool(BaseTool):
pass
def get_functions(self):
return [
{
"type": "function",
"function": {
"name": "get_log_contents",
"description": "Get the contents of the log file.",
"parameters": {
@@ -48,6 +51,7 @@ class LogTool(BaseTool):
"required": []
}
}
}
]
@metrics.measure
+9
View File
@@ -13,6 +13,8 @@ class MetricsTool(BaseTool):
def get_functions(self):
return [
{
"type": "function",
"function": {
"name": "get_function_metrics",
"description": "Get metrics for all measured functions.",
"parameters": {
@@ -20,8 +22,11 @@ class MetricsTool(BaseTool):
"properties": {},
"required": []
}
}
},
{
"type": "function",
"function": {
"name": "get_specific_function_metrics",
"description": "Get metrics for a specific function.",
"parameters": {
@@ -34,8 +39,11 @@ class MetricsTool(BaseTool):
},
"required": ["function_name"]
}
}
},
{
"type": "function",
"function": {
"name": "get_top_n_functions",
"description": "Get the top N functions by total execution time.",
"parameters": {
@@ -49,6 +57,7 @@ class MetricsTool(BaseTool):
"required": ["n"]
}
}
}
]
@metrics.measure