-
Notifications
You must be signed in to change notification settings - Fork 5
feat: connect cwv to ai provider #118
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
Open
olekszczepanowski
wants to merge
20
commits into
main
Choose a base branch
from
cvw-112-connect-cwv-to-ai-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dfaf6dd
feat: add anomaly detection logic and a test for it
Levosilimo 66f6ff5
feat: improve anomaly detection with lognormal dist
Levosilimo a32ca20
fix: apply coderabbit suggestions
Levosilimo dcdc7c4
feat: add base agent
olekszczepanowski f06e834
fix: fix test flakyness
Levosilimo 8e79f7d
feat: improve system prompt and clickhouse tool
olekszczepanowski ab11532
Merge branch 'cwv-109-anomaly-detection-logic' into cvw-112-connect-c…
olekszczepanowski 72be373
feat: add variables in env ci
olekszczepanowski c65c1d3
fix: make ai configuration optional
olekszczepanowski 0fba5ec
feat: change preview value
olekszczepanowski c8929de
feat: move agent clickhouse client to utils directory
olekszczepanowski 37253b2
feat: add anomaly checker worker
Levosilimo 8b292f5
fix: fix docker setup
Levosilimo 2fd942a
fix: fix ci
Levosilimo aeb2e47
feat: add authorization
olekszczepanowski 3267c3c
fix: code rabbit comments
olekszczepanowski 660433e
feat: add adaptivecards types
Levosilimo 280c17f
Merge remote-tracking branch 'origin/cwv-115-anomaly-pipeline' into c…
olekszczepanowski 8aa2fbe
feat: load schema from catalog.yml
olekszczepanowski b32dccb
fix: ci
olekszczepanowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
apps/monitor-app/clickhouse/migrations/007_anomaly_detection.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| CREATE TABLE IF NOT EXISTS cwv_stats_hourly | ||
| ( | ||
| project_id UUID, | ||
| route String, | ||
| device_type LowCardinality(String), | ||
| metric_name LowCardinality(String), | ||
| hour DateTime, | ||
| avg_state AggregateFunction(avg, Float64), | ||
| var_state AggregateFunction(varSampStable, Float64), | ||
| count_state AggregateFunction(count, UInt64) | ||
| ) | ||
| ENGINE = AggregatingMergeTree | ||
| PARTITION BY toYYYYMM(hour) | ||
| ORDER BY (project_id, hour, route, device_type, metric_name) | ||
| TTL hour + INTERVAL 30 DAY; | ||
|
|
||
| CREATE MATERIALIZED VIEW IF NOT EXISTS mv_cwv_stats_hourly | ||
| TO cwv_stats_hourly | ||
| AS | ||
| SELECT | ||
| project_id, | ||
| route, | ||
| device_type, | ||
| metric_name, | ||
| toStartOfHour(recorded_at) AS hour, | ||
| avgState(log1p(metric_value)) AS avg_state, | ||
| varSampStableState(log1p(metric_value)) AS var_state, | ||
| countState() AS count_state | ||
| FROM cwv_events | ||
| GROUP BY project_id, hour, route, device_type, metric_name; | ||
|
|
||
| INSERT INTO cwv_stats_hourly | ||
| SELECT | ||
| project_id, | ||
| route, | ||
| device_type, | ||
| metric_name, | ||
| toStartOfHour(recorded_at) AS hour, | ||
| avgState(log1p(metric_value)) AS avg_state, | ||
| varSampStableState(log1p(metric_value)) AS var_state, | ||
| countState() AS count_state | ||
| FROM cwv_events | ||
| WHERE recorded_at >= toStartOfHour(now()) - INTERVAL 7 DAY | ||
| GROUP BY project_id, hour, route, device_type, metric_name; | ||
|
|
||
| CREATE VIEW IF NOT EXISTS v_cwv_anomalies AS | ||
| WITH | ||
| toStartOfHour(now()) AS current_hour_mark, | ||
| current_hour_mark - INTERVAL 1 HOUR AS gap_hour, | ||
| current_hour_mark - INTERVAL 7 DAY AS baseline_start | ||
| SELECT | ||
| lower(hex(MD5(concat( | ||
| toString(project_id), '\0', route, '\0', metric_name, '\0', device_type, '\0', toString(current_hour_mark) | ||
| )))) AS anomaly_id, | ||
| project_id, route, metric_name, device_type, | ||
| current_hour_mark AS detection_time, | ||
|
|
||
| avgMergeIf(avg_state, hour = current_hour_mark) AS log_avg_curr, | ||
| avgMergeIf(avg_state, hour >= baseline_start AND hour < gap_hour) AS log_avg_base, | ||
| sqrt(varSampStableMergeIf(var_state, hour >= baseline_start AND hour < gap_hour)) AS log_stddev_base, | ||
|
|
||
| exp(log_avg_curr) - 1 AS current_avg_raw, | ||
| exp(log_avg_base) - 1 AS baseline_avg_raw, | ||
|
|
||
| countMergeIf(count_state, hour = current_hour_mark) AS sample_size, | ||
| countMergeIf(count_state, hour >= baseline_start AND hour < gap_hour) AS baseline_n, | ||
|
|
||
| (log_avg_curr - log_avg_base) / IF(log_stddev_base = 0, 0.00001, log_stddev_base) AS z_score | ||
| FROM cwv_stats_hourly | ||
| WHERE hour >= baseline_start | ||
| GROUP BY project_id, route, device_type, metric_name | ||
| HAVING sample_size >= 20 AND baseline_n >= 100; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS processed_anomalies | ||
| ( | ||
| anomaly_id String, | ||
| project_id UUID, | ||
| metric_name LowCardinality(String), | ||
| route String, | ||
| device_type LowCardinality(String), | ||
| last_z_score Float64, | ||
| notified_at DateTime DEFAULT now(), | ||
| status Enum8('new' = 1, 'notified' = 2, 'acknowledged' = 3, 'resolved' = 4) DEFAULT 'new', | ||
| updated_at DateTime64(3) DEFAULT now64(3) | ||
| ) | ||
| ENGINE = ReplacingMergeTree(updated_at) | ||
| ORDER BY (project_id, anomaly_id); | ||
|
|
||
| CREATE SETTINGS PROFILE IF NOT EXISTS ai_analyst_profile SETTINGS | ||
| max_execution_time = 15, | ||
| max_memory_usage = 2000000000, | ||
| max_rows_to_read = 100000000; | ||
|
|
||
| CREATE ROLE IF NOT EXISTS r_ai_analyst; | ||
| GRANT SELECT ON cwv_events TO r_ai_analyst; | ||
| GRANT SELECT ON custom_events TO r_ai_analyst; | ||
| GRANT SELECT ON cwv_daily_aggregates TO r_ai_analyst; | ||
| GRANT SELECT ON cwv_stats_hourly TO r_ai_analyst; | ||
| GRANT SELECT ON v_cwv_anomalies TO r_ai_analyst; | ||
| GRANT SELECT ON projects TO r_ai_analyst; | ||
| GRANT SELECT, INSERT, ALTER UPDATE ON processed_anomalies TO r_ai_analyst; | ||
olekszczepanowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| CREATE USER IF NOT EXISTS ai_analyst_user | ||
| IDENTIFIED WITH no_password; | ||
olekszczepanowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| GRANT r_ai_analyst TO ai_analyst_user; | ||
| ALTER USER ai_analyst_user DEFAULT ROLE r_ai_analyst; | ||
| ALTER USER ai_analyst_user SETTINGS PROFILE ai_analyst_profile; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { createAgent } from "@/app/server/lib/agent/agent"; | ||
| import { createAgentUIStreamResponse, UIMessage } from "ai"; | ||
| import type { NextRequest } from "next/server"; | ||
|
|
||
| export async function POST(req: NextRequest, { params }: { params: Promise<{ projectId: string }> }) { | ||
| try { | ||
| const { projectId } = await params; | ||
olekszczepanowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { messages }: { messages: UIMessage[] } = await req.json(); | ||
olekszczepanowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const agent = createAgent(projectId); | ||
|
|
||
| return createAgentUIStreamResponse({ | ||
| agent, | ||
| uiMessages: messages, | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : String(error); | ||
| return Response.json( | ||
| { ok: false, error: errorMessage }, | ||
| { | ||
| status: 500, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { env } from "@/env"; | ||
| import { stepCountIs, ToolLoopAgent } from "ai"; | ||
| import { executeClickHouse } from "@/app/server/lib/agent/tools/execute-clickhouse"; | ||
| import { buildSystemPrompt } from "@/app/server/lib/agent/utils/system-prompt"; | ||
| import { createAnthropic } from "@ai-sdk/anthropic"; | ||
| import { createGoogleGenerativeAI } from "@ai-sdk/google"; | ||
| import { createOpenAI } from "@ai-sdk/openai"; | ||
|
|
||
| const getModel = () => { | ||
| const provider = env.AI_PROVIDER; | ||
| const apiKey = env.AI_API_KEY; | ||
| const model = env.AI_MODEL; | ||
|
|
||
| if (!provider || !apiKey || !model) { | ||
| throw new Error("AI_PROVIDER, AI_API_KEY, and AI_MODEL must be set to use the agent"); | ||
| } | ||
|
|
||
| switch (provider) { | ||
| case "anthropic": { | ||
| const anthropic = createAnthropic({ apiKey }); | ||
| return anthropic(model); | ||
| } | ||
| case "google": { | ||
| const google = createGoogleGenerativeAI({ apiKey }); | ||
| return google(model); | ||
| } | ||
| case "openai": { | ||
| const openai = createOpenAI({ apiKey }); | ||
| return openai(model); | ||
| } | ||
| default: { | ||
| throw new Error(`Provider not supported: ${provider}`); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export function createAgent(projectId: string) { | ||
| return new ToolLoopAgent({ | ||
| model: getModel(), | ||
| instructions: buildSystemPrompt(projectId), | ||
| tools: { | ||
| executeClickHouse, | ||
| }, | ||
| stopWhen: [stepCountIs(15)], | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.