Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for calling Robusta AI & LLM tool setup/config refactor #23

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 43 additions & 34 deletions holmes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
# add_custom_certificate("cert goes here as a string (not path to the cert rather the cert itself)")

import logging
import re
import warnings
from pathlib import Path
from typing import List, Optional, Pattern
import json
from typing import List, Optional

import typer
from rich.console import Console
from rich.logging import RichHandler
from rich.markdown import Markdown
from rich.rule import Rule

from holmes.utils.file_utils import write_json_file
from holmes.config import Config, LLMType
from holmes.config import BaseLLMConfig, LLMProviderType
from holmes.core.provider import LLMProviderFactory
from holmes.plugins.destinations import DestinationType
from holmes.plugins.prompts import load_prompt
from holmes.plugins.sources.opsgenie import OPSGENIE_TEAM_INTEGRATION_KEY_HELP
Expand All @@ -28,22 +29,12 @@
)
app.add_typer(investigate_app, name="investigate")

def init_logging(verbose = False):
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO, format="%(message)s", handlers=[RichHandler(show_level=False, show_time=False)])
# disable INFO logs from OpenAI
logging.getLogger("httpx").setLevel(logging.WARNING)
# when running in --verbose mode we don't want to see DEBUG logs from these libraries
logging.getLogger("openai._base_client").setLevel(logging.INFO)
logging.getLogger("httpcore").setLevel(logging.INFO)
logging.getLogger("markdown_it").setLevel(logging.INFO)
# Suppress UserWarnings from the slack_sdk module
warnings.filterwarnings("ignore", category=UserWarning, module="slack_sdk.*")
return Console()

# Common cli options
opt_llm: Optional[LLMType] = typer.Option(
LLMType.OPENAI,
help="Which LLM to use ('openai' or 'azure')",
llm_provider_names = ", ".join(str(tp) for tp in LLMProviderType)
opt_llm: Optional[LLMProviderType] = typer.Option(
LLMProviderType.OPENAI,
help=f"LLM provider (supported values: {llm_provider_names})"
)
opt_api_key: Optional[str] = typer.Option(
None,
Expand Down Expand Up @@ -111,6 +102,19 @@ def init_logging(verbose = False):
system_prompt_help = "Advanced. System prompt for LLM. Values starting with builtin:// are loaded from holmes/plugins/prompts, values starting with file:// are loaded from the given path, other values are interpreted as a prompt string"


def init_logging(verbose = False):
logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO, format="%(message)s", handlers=[RichHandler(show_level=False, show_time=False)])
# disable INFO logs from OpenAI
logging.getLogger("httpx").setLevel(logging.WARNING)
# when running in --verbose mode we don't want to see DEBUG logs from these libraries
logging.getLogger("openai._base_client").setLevel(logging.INFO)
logging.getLogger("httpcore").setLevel(logging.INFO)
logging.getLogger("markdown_it").setLevel(logging.INFO)
# Suppress UserWarnings from the slack_sdk module
warnings.filterwarnings("ignore", category=UserWarning, module="slack_sdk.*")
return Console()


# TODO: add interactive interpreter mode
# TODO: add streaming output
@app.command()
Expand Down Expand Up @@ -141,7 +145,7 @@ def ask(
Ask any question and answer using available tools
"""
console = init_logging(verbose)
config = Config.load_from_file(
config = BaseLLMConfig.load_from_file(
config_file,
api_key=api_key,
llm=llm,
Expand All @@ -150,8 +154,9 @@ def ask(
max_steps=max_steps,
custom_toolsets=custom_toolsets,
)
provider_factory = LLMProviderFactory(config)
system_prompt = load_prompt(system_prompt)
ai = config.create_toolcalling_llm(console, allowed_toolsets)
ai = provider_factory.create_toolcalling_llm(console, allowed_toolsets)
console.print("[bold yellow]User:[/bold yellow] " + prompt)
response = ai.call(system_prompt, prompt)
text_result = Markdown(response.result)
Expand All @@ -160,7 +165,8 @@ def ask(
if show_tool_output and response.tool_calls:
for tool_call in response.tool_calls:
console.print(f"[bold magenta]Used Tool:[/bold magenta]", end="")
# we need to print this separately with markup=False because it contains arbitrary text and we don't want console.print to interpret it
# we need to print this separately with markup=False because it contains arbitrary text
# and we don't want console.print to interpret it
console.print(f"{tool_call.description}. Output=\n{tool_call.result}", markup=False)
console.print(f"[bold green]AI:[/bold green]", end=" ")
console.print(text_result, soft_wrap=True)
Expand All @@ -180,7 +186,7 @@ def alertmanager(
None, help="Password to use for basic auth"
),
# common options
llm: Optional[LLMType] = opt_llm,
llm: Optional[LLMProviderType] = opt_llm,
api_key: Optional[str] = opt_api_key,
azure_endpoint: Optional[str] = opt_azure_endpoint,
model: Optional[str] = opt_model,
Expand All @@ -203,7 +209,7 @@ def alertmanager(
Investigate a Prometheus/Alertmanager alert
"""
console = init_logging(verbose)
config = Config.load_from_file(
config = BaseLLMConfig.load_from_file(
config_file,
api_key=api_key,
llm=llm,
Expand All @@ -219,14 +225,15 @@ def alertmanager(
custom_toolsets=custom_toolsets,
custom_runbooks=custom_runbooks
)
provider_factory = LLMProviderFactory(config)

system_prompt = load_prompt(system_prompt)
ai = config.create_issue_investigator(console, allowed_toolsets)
ai = provider_factory.create_issue_investigator(console, allowed_toolsets)

source = config.create_alertmanager_source()
source = provider_factory.create_alertmanager_source()

if destination == DestinationType.SLACK:
slack = config.create_slack_destination()
slack = provider_factory.create_slack_destination()

try:
issues = source.fetch_issues()
Expand Down Expand Up @@ -282,7 +289,7 @@ def jira(
False, help="Update Jira with AI results"
),
# common options
llm: Optional[LLMType] = opt_llm,
llm: Optional[LLMProviderType] = opt_llm,
api_key: Optional[str] = opt_api_key,
azure_endpoint: Optional[str] = opt_azure_endpoint,
model: Optional[str] = opt_model,
Expand All @@ -301,7 +308,7 @@ def jira(
Investigate a Jira ticket
"""
console = init_logging(verbose)
config = Config.load_from_file(
config = BaseLLMConfig.load_from_file(
config_file,
api_key=api_key,
llm=llm,
Expand All @@ -315,10 +322,11 @@ def jira(
custom_toolsets=custom_toolsets,
custom_runbooks=custom_runbooks
)
provider_factory = LLMProviderFactory(config)

system_prompt = load_prompt(system_prompt)
ai = config.create_issue_investigator(console, allowed_toolsets)
source = config.create_jira_source()
ai = provider_factory.create_issue_investigator(console, allowed_toolsets)
source = provider_factory.create_jira_source()
try:
# TODO: allow passing issue ID
issues = source.fetch_issues()
Expand Down Expand Up @@ -371,7 +379,7 @@ def github(
help="Investigate tickets matching a GitHub query (e.g. 'is:issue is:open')",
),
# common options
llm: Optional[LLMType] = opt_llm,
llm: Optional[LLMProviderType] = opt_llm,
api_key: Optional[str] = opt_api_key,
azure_endpoint: Optional[str] = opt_azure_endpoint,
model: Optional[str] = opt_model,
Expand All @@ -390,7 +398,7 @@ def github(
Investigate a GitHub issue
"""
console = init_logging(verbose)
config = Config.load_from_file(
config = BaseLLMConfig.load_from_file(
config_file,
api_key=api_key,
llm=llm,
Expand All @@ -405,10 +413,11 @@ def github(
custom_toolsets=custom_toolsets,
custom_runbooks=custom_runbooks
)
provider_factory = LLMProviderFactory(config)

system_prompt = load_prompt(system_prompt)
ai = config.create_issue_investigator(console, allowed_toolsets)
source = config.create_github_source()
ai = provider_factory.create_issue_investigator(console, allowed_toolsets)
source = provider_factory.create_github_source()
try:
issues = source.fetch_issues()
except Exception as e:
Expand Down
1 change: 1 addition & 0 deletions holmes/common/env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ROBUSTA_CONFIG_PATH = os.environ.get('ROBUSTA_CONFIG_PATH', "/etc/robusta/config/active_playbooks.yaml")

ROBUSTA_ACCOUNT_ID = os.environ.get("ROBUSTA_ACCOUNT_ID", "")
ROBUSTA_USER_ID = os.environ.get("ROBUSTA_USER_ID", "")
STORE_URL = os.environ.get("STORE_URL", "")
STORE_API_KEY = os.environ.get("STORE_API_KEY", "")
STORE_EMAIL = os.environ.get("STORE_EMAIL", "")
Expand Down
Loading