Skip to content

Commit

Permalink
update for use local LLM add example (#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
a.gorlanov committed Nov 22, 2024
1 parent b771876 commit 63779f7
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions ee/experimental/generative_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
MAX_SUMMARY_LENGTH = 900
MAX_NAME_LENGTH = 75


def generate_incident_summary(
incident: Incident,
use_n_alerts_for_summary: int = -1,
generate_summary: str = None,
max_summary_length: int = None,
incident: Incident,
use_n_alerts_for_summary: int = -1,
generate_summary: str = None,
max_summary_length: int = None,
) -> str:
if "OPENAI_API_KEY" not in os.environ:
logger.error(
Expand All @@ -31,7 +32,7 @@ def generate_incident_summary(

if "OPENAI_API_URL" not in os.environ:
logger.error(
"OpenAI API URL is not set. You use OpenAI models. But if you want use yourself LLM just add url to your LLM(vllm/ollama)",
"OpenAI API URL is not set. You use OpenAI models. But if you want use yourself LLM just add url to your LLM(vllm/ollama). Example http://IP:PORT/v1",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME,
"incident_id": incident.id, "tenant_id": incident.tenant_id}
)
Expand All @@ -42,7 +43,8 @@ def generate_incident_summary(

if generate_summary == "False":
logger.info(f"Incident summary generation is disabled. Aborting.",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})
return ""

if incident.user_summary:
Expand All @@ -53,7 +55,10 @@ def generate_incident_summary(
"MAX_SUMMARY_LENGTH", MAX_SUMMARY_LENGTH)

try:
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url=os.environ["OPENAI_API_URL"]
)

incident = get_incident_by_id(incident.tenant_id, incident.id)

Expand Down Expand Up @@ -105,11 +110,13 @@ def generate_incident_summary(
)

logger.info(f"Generated incident summary with length {len(summary)} symbols",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})

if len(summary) > max_summary_length:
logger.info(f"Generated incident summary is too long. Applying smart truncation",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})

summary = (
client.chat.completions.create(
Expand All @@ -133,21 +140,25 @@ def generate_incident_summary(
)

logger.info(f"Generated new incident summary with length {len(summary)} symbols",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})

if len(summary) > max_summary_length:
logger.info(f"Generated incident summary is too long. Applying hard truncation",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})
summary = summary[: max_summary_length]

return summary
except Exception as e:
logger.error(f"Error in generating incident summary: {e}",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})
return ""


def generate_incident_name(incident: Incident, generate_name: str = None, max_name_length: int = None, use_n_alerts_for_name: int = -1) -> str:
def generate_incident_name(incident: Incident, generate_name: str = None, max_name_length: int = None,
use_n_alerts_for_name: int = -1) -> str:
if "OPENAI_API_KEY" not in os.environ:
logger.error(
"OpenAI API key is not set. Incident name generation is not available.",
Expand All @@ -158,7 +169,7 @@ def generate_incident_name(incident: Incident, generate_name: str = None, max_na

if "OPENAI_API_URL" not in os.environ:
logger.error(
"OpenAI API URL is not set. You use OpenAI models. But if you want use yourself LLM just add url to your LLM(vllm/ollama)",
"OpenAI API URL is not set. You use OpenAI models. But if you want use yourself LLM just add url to your LLM(vllm/ollama). Example http://IP:PORT/v1",
extra={"algorithm": SUMMARY_GENERATOR_VERBOSE_NAME,
"incident_id": incident.id, "tenant_id": incident.tenant_id}
)
Expand All @@ -169,7 +180,8 @@ def generate_incident_name(incident: Incident, generate_name: str = None, max_na

if generate_name == "False":
logger.info(f"Incident name generation is disabled. Aborting.",
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})
return ""

if incident.user_generated_name:
Expand Down Expand Up @@ -224,7 +236,8 @@ def generate_incident_name(incident: Incident, generate_name: str = None, max_na

if len(name) > max_name_length:
logger.info(f"Generated incident name is too long. Applying smart truncation",
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})

name = client.chat.completions.create(model=model, messages=[
{
Expand All @@ -244,15 +257,18 @@ def generate_incident_name(incident: Incident, generate_name: str = None, max_na
]).choices[0].message.content

logger.info(f"Generated new incident name with length {len(name)} symbols",
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})

if len(name) > max_name_length:
logger.info(f"Generated incident name is too long. Applying hard truncation",
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})
name = name[: max_name_length]

return name
except Exception as e:
logger.error(f"Error in generating incident name: {e}",
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id, "tenant_id": incident.tenant_id})
extra={"algorithm": NAME_GENERATOR_VERBOSE_NAME, "incident_id": incident.id,
"tenant_id": incident.tenant_id})
return ""

0 comments on commit 63779f7

Please sign in to comment.