-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault_agent.py
More file actions
executable file
·80 lines (73 loc) · 3.88 KB
/
default_agent.py
File metadata and controls
executable file
·80 lines (73 loc) · 3.88 KB
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
71
72
73
74
75
76
77
78
79
80
import json
import sys
import logging
import time
import os
from mcp_servers.mcp_utils import fetch_tools_list
from agents import agent_with_tools
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def prompt_ai_agent(slack_message_json,history=[],tools = 'all'):
system_prompt = f"""You are an AI assistant specialized in helping DevOps/SRE/On-call engineers in their day-to-day operations -- including debug production issues, proactive monitoring setup and more.
Your primary goal is to provide and execute practical, actionable debugging guidance based *directly* on the user's problem/alert description.
CRITICAL INSTRUCTIONS:
- Analyze the user's message to understand their specific problem. You will receive instructions / notes from user or data sources for services. Prioritise these instructions over other information.
- You have access to multiple relevant tools. There will be tools that will help you get context and then there will be tools that will help you fetch actual data or take actions.
- Use the combination of both to help the user. Make sure to think logically as a software engineer would think who needs to figure out the issue and fix it. Use tools with right justifications.
- Text should be in Markdown format."""
messages = []
# if there's a file global_instructions.md, use it as a system prompt
messages.append({"role": "system", "content": system_prompt})
if os.path.exists('global_instructions.md'):
with open('global_instructions.md', 'r') as file:
messages.append({"role": "user", "content": "This is a global instructions file: " + file.read()})
messages.append({"role": "user", "content": str(slack_message_json)})
messages.extend(history)
if tools == 'all':
available_tools = fetch_tools_list()
agent_chat = agent_with_tools(messages, available_tools)
elif tools == 'none':
available_tools = []
#todo
# add handling for approval flows
return agent_chat
def agent_wrapper_fn(slack_message_json):
"""
Main function to execute prompt-based workflow
"""
try:
if not slack_message_json:
logger.error("No Slack message provided")
return {"error": "No message provided"}
except Exception as e:
logger.error(f"An unexpected error occurred in main: {e}", exc_info=True)
return {"error": f"An unexpected error occurred: {str(e)}"}
print(slack_message_json)
start_time = time.monotonic()
agent_chat_response = prompt_ai_agent(slack_message_json)
time_taken = time.monotonic() - start_time
time_taken_str = f"\n\n_Time taken: {time_taken:.2f} seconds_"
agent_chat_response.append({'type': 'time_taken','time_taken': time_taken_str})
text = ''
file_content = ""
for response in agent_chat_response:
if response['type'] == 'chat_text':
text = text + (response['content'])
# add tool result to a .txt file
elif response['type'] == 'tool_result':
text = text + ("\n\nTool Call: " + response['tool_name'] + " Tool Arguments: " + str(response['tool_config']) + " result: in attached .txt file")
file_content = file_content + ("\n\nTool Call:" + response['tool_name'] + " Tool Arguments: " + str(response['tool_config']) + " result: " + str(response['tool_result']))
elif response['type'] == 'time_taken':
text = text + (response['time_taken'])
slack_message_response = {
"text": text,
"channel": slack_message_json.get('channel'),
"thread_ts": slack_message_json.get('thread_ts',slack_message_json.get('ts',''))
}
if file_content:
slack_message_response['file_content'] = file_content
return slack_message_response
if __name__ == "__main__":
print("Test")
# prompt_ai_agent("prompts/sample_prompt.md","Error in Service, please fix it.")