forked from robusta-dev/holmesgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
217 lines (171 loc) · 6.96 KB
/
server.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
from holmes.utils.cert_utils import add_custom_certificate
ADDITIONAL_CERTIFICATE: str = os.environ.get("CERTIFICATE", "")
if add_custom_certificate(ADDITIONAL_CERTIFICATE):
print("added custom certificate")
# DO NOT ADD ANY IMPORTS OR CODE ABOVE THIS LINE
# IMPORTING ABOVE MIGHT INITIALIZE AN HTTPS CLIENT THAT DOESN'T TRUST THE CUSTOM CERTIFICATE
from holmes.core import investigation
from contextlib import asynccontextmanager
from holmes.utils.holmes_status import update_holmes_status_in_db
import jinja2
import logging
import uvicorn
import colorlog
from litellm.exceptions import AuthenticationError
from fastapi import FastAPI, HTTPException
from rich.console import Console
from holmes.utils.robusta import load_robusta_api_key
from holmes.common.env_vars import (
HOLMES_HOST,
HOLMES_PORT,
HOLMES_POST_PROCESSING_PROMPT,
)
from holmes.core.supabase_dal import SupabaseDal
from holmes.config import Config
from holmes.core.conversations import (
build_chat_messages,
build_issue_chat_messages,
handle_issue_conversation,
)
from holmes.core.issue import Issue
from holmes.core.models import (
InvestigationResult,
ConversationRequest,
InvestigateRequest,
WorkloadHealthRequest,
ConversationInvestigationResponse,
ChatRequest,
ChatResponse,
IssueChatRequest,
)
from holmes.plugins.prompts import load_and_render_prompt
from holmes.utils.holmes_sync_toolsets import holmes_sync_toolsets_status
from holmes.utils.global_instructions import add_global_instructions_to_user_prompt
def init_logging():
logging_level = os.environ.get("LOG_LEVEL", "INFO")
logging_format = "%(log_color)s%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s"
logging_datefmt = "%Y-%m-%d %H:%M:%S"
print("setting up colored logging")
colorlog.basicConfig(
format=logging_format, level=logging_level, datefmt=logging_datefmt
)
logging.getLogger().setLevel(logging_level)
httpx_logger = logging.getLogger("httpx")
if httpx_logger:
httpx_logger.setLevel(logging.WARNING)
logging.info(f"logger initialized using {logging_level} log level")
init_logging()
dal = SupabaseDal()
config = Config.load_from_env()
@asynccontextmanager
async def lifespan(app: FastAPI):
try:
update_holmes_status_in_db(dal, config)
except Exception as error:
logging.error("Failed to update holmes status", exc_info=True)
try:
holmes_sync_toolsets_status(dal, config)
except Exception as error:
logging.error("Failed to synchronise holmes toolsets", exc_info=True)
yield
app = FastAPI(lifespan=lifespan)
console = Console()
@app.post("/api/investigate")
def investigate_issues(investigate_request: InvestigateRequest):
try:
result = investigation.investigate_issues(
investigate_request=investigate_request,
dal=dal,
config=config,
console=console
)
return result
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
@app.post("/api/workload_health_check")
def workload_health_check(request: WorkloadHealthRequest):
load_robusta_api_key(dal=dal, config=config)
try:
resource = request.resource
workload_alerts: list[str] = []
if request.alert_history:
workload_alerts = dal.get_workload_issues(
resource, request.alert_history_since_hours
)
instructions = request.instructions or []
if request.stored_instrucitons:
stored_instructions = dal.get_resource_instructions(
resource.get("kind", "").lower(), resource.get("name")
)
if stored_instructions:
instructions.extend(stored_instructions.instructions)
nl = "\n"
if instructions:
request.ask = f"{request.ask}\n My instructions for the investigation '''{nl.join(instructions)}'''"
global_instructions = dal.get_global_instructions_for_account()
request.ask = add_global_instructions_to_user_prompt(request.ask, global_instructions)
system_prompt = load_and_render_prompt(request.prompt_template, context={'alerts': workload_alerts})
ai = config.create_toolcalling_llm(console, dal=dal)
structured_output = {"type": "json_object"}
ai_call = ai.prompt_call(
system_prompt, request.ask, HOLMES_POST_PROCESSING_PROMPT, structured_output
)
return InvestigationResult(
analysis=ai_call.result,
tool_calls=ai_call.tool_calls,
instructions=instructions,
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
# older api that does not support conversation history
@app.post("/api/conversation")
def issue_conversation(conversation_request: ConversationRequest):
try:
load_robusta_api_key(dal=dal, config=config)
ai = config.create_toolcalling_llm(console, dal=dal)
system_prompt = handle_issue_conversation(conversation_request, ai)
investigation = ai.prompt_call(system_prompt, conversation_request.user_prompt)
return ConversationInvestigationResponse(
analysis=investigation.result,
tool_calls=investigation.tool_calls,
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
@app.post("/api/issue_chat")
def issue_conversation(issue_chat_request: IssueChatRequest):
try:
load_robusta_api_key(dal=dal, config=config)
ai = config.create_toolcalling_llm(console, dal=dal)
global_instructions = dal.get_global_instructions_for_account()
messages = build_issue_chat_messages(issue_chat_request, ai, global_instructions)
llm_call = ai.messages_call(messages=messages)
return ChatResponse(
analysis=llm_call.result,
tool_calls=llm_call.tool_calls,
conversation_history=llm_call.messages,
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
@app.post("/api/chat")
def chat(chat_request: ChatRequest):
try:
load_robusta_api_key(dal=dal, config=config)
ai = config.create_toolcalling_llm(console, dal=dal)
global_instructions = dal.get_global_instructions_for_account()
messages = build_chat_messages(
chat_request.ask, chat_request.conversation_history, ai=ai, global_instructions=global_instructions
)
llm_call = ai.messages_call(messages=messages)
return ChatResponse(
analysis=llm_call.result,
tool_calls=llm_call.tool_calls,
conversation_history=llm_call.messages,
)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=e.message)
@app.get("/api/model")
def get_model():
return {"model_name": config.model}
if __name__ == "__main__":
uvicorn.run(app, host=HOLMES_HOST, port=HOLMES_PORT)