feat(ml2): feedback_loop, hypertuner, heavy_predictor
This commit is contained in:
parent
4c5034df88
commit
11845adae0
39
agents/core-logic/feedback_loop.py
Normal file
39
agents/core-logic/feedback_loop.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# agents/core-logic/feedback_loop.py
|
||||
|
||||
import requests
|
||||
from google.cloud import bigquery
|
||||
|
||||
# Configuration
|
||||
TELEMETRY_URL = "https://osvauco-agent-357036551735.europe-west1.run.app/telemetry/history"
|
||||
BIGQUERY_PROJECT = "propane-will-491900-m5"
|
||||
BIGQUERY_DATASET = "osvauco_logs"
|
||||
BIGQUERY_TABLE = "cloud_run_logs"
|
||||
|
||||
def collect_and_store_telemetry():
|
||||
"""
|
||||
Collects telemetry data from the specified endpoint and stores it in BigQuery.
|
||||
"""
|
||||
try:
|
||||
# 1. Collect telemetry data
|
||||
response = requests.get(TELEMETRY_URL)
|
||||
response.raise_for_status() # Raise an exception for bad status codes
|
||||
telemetry_data = response.json()
|
||||
|
||||
# 2. Store data in BigQuery
|
||||
client = bigquery.Client(project=BIGQUERY_PROJECT)
|
||||
table_id = f"{BIGQUERY_PROJECT}.{BIGQUERY_DATASET}.{BIGQUERY_TABLE}"
|
||||
|
||||
# Assuming telemetry_data is a list of dicts matching the table schema
|
||||
errors = client.insert_rows_json(table_id, telemetry_data)
|
||||
if errors == []:
|
||||
print(f"Successfully inserted {len(telemetry_data)} rows into {table_id}")
|
||||
else:
|
||||
print(f"Encountered errors while inserting rows: {errors}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error collecting telemetry data: {e}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
collect_and_store_telemetry()
|
||||
45
agents/core-logic/heavy_predictor.py
Normal file
45
agents/core-logic/heavy_predictor.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# agents/core-logic/heavy_predictor.py
|
||||
|
||||
from google.cloud import bigquery
|
||||
|
||||
# Configuration
|
||||
BIGQUERY_PROJECT = "propane-will-491900-m5"
|
||||
BIGQUERY_DATASET = "osvauco_logs"
|
||||
BIGQUERY_TABLE = "cloud_run_logs"
|
||||
TELEMETRY_CALLS_THRESHOLD = 500
|
||||
|
||||
def check_and_trigger_heavy_mode():
|
||||
"""
|
||||
Checks the number of telemetry calls and triggers heavy mode if the threshold is exceeded.
|
||||
"""
|
||||
try:
|
||||
client = bigquery.Client(project=BIGQUERY_PROJECT)
|
||||
table_id = f"{BIGQUERY_PROJECT}.{BIGQUERY_DATASET}.{BIGQUERY_TABLE}"
|
||||
|
||||
# 1. Query the number of telemetry calls from BigQuery
|
||||
query = f"""
|
||||
SELECT COUNT(*) as total_calls
|
||||
FROM `{table_id}`
|
||||
WHERE timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 24 HOUR)
|
||||
"""
|
||||
query_job = client.query(query)
|
||||
results = query_job.result()
|
||||
|
||||
for row in results:
|
||||
total_calls = row.total_calls
|
||||
print(f"Total telemetry calls in the last 24 hours: {total_calls}")
|
||||
|
||||
# 2. Trigger heavy mode if the threshold is exceeded
|
||||
if total_calls > TELEMETRY_CALLS_THRESHOLD:
|
||||
print("Telemetry calls threshold exceeded. Triggering heavy mode.")
|
||||
# Placeholder for logic to trigger heavy mode
|
||||
# This could involve scaling up resources, switching to a more powerful model, etc.
|
||||
pass
|
||||
else:
|
||||
print("Telemetry calls are within acceptable limits.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_and_trigger_heavy_mode()
|
||||
46
agents/core-logic/hypertuner.py
Normal file
46
agents/core-logic/hypertuner.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# agents/core-logic/hypertuner.py
|
||||
|
||||
from google.cloud import bigquery
|
||||
|
||||
# Configuration
|
||||
BIGQUERY_PROJECT = "propane-will-491900-m5"
|
||||
BIGQUERY_DATASET = "osvauco_logs"
|
||||
BIGQUERY_TABLE = "cloud_run_logs"
|
||||
RESPONSE_TIME_THRESHOLD = 1000 # in milliseconds
|
||||
|
||||
def adjust_prompt_parameters():
|
||||
"""
|
||||
Reads telemetry data from BigQuery, analyzes response times,
|
||||
and adjusts prompt parameters accordingly.
|
||||
"""
|
||||
try:
|
||||
client = bigquery.Client(project=BIGQUERY_PROJECT)
|
||||
table_id = f"{BIGQUERY_PROJECT}.{BIGQUERY_DATASET}.{BIGQUERY_TABLE}"
|
||||
|
||||
# 1. Query response time data from BigQuery
|
||||
query = f"""
|
||||
SELECT AVG(latency_ms) as avg_latency
|
||||
FROM `{table_id}`
|
||||
WHERE timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR)
|
||||
"""
|
||||
query_job = client.query(query)
|
||||
results = query_job.result()
|
||||
|
||||
for row in results:
|
||||
avg_latency = row.avg_latency
|
||||
print(f"Average response time in the last hour: {avg_latency} ms")
|
||||
|
||||
# 2. Adjust prompt parameters based on response time
|
||||
if avg_latency > RESPONSE_TIME_THRESHOLD:
|
||||
print("Response time is high. Adjusting prompt parameters to reduce complexity.")
|
||||
# Placeholder for logic to adjust prompt parameters
|
||||
# For example, reduce max_tokens, use a simpler model, etc.
|
||||
pass
|
||||
else:
|
||||
print("Response time is within acceptable limits.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
adjust_prompt_parameters()
|
||||
Loading…
Reference in New Issue
Block a user