Update trace_agent_collaboratif.py
Browse files- trace_agent_collaboratif.py +103 -103
trace_agent_collaboratif.py
CHANGED
|
@@ -1,104 +1,104 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import importlib
|
| 4 |
-
from collections import defaultdict
|
| 5 |
-
|
| 6 |
-
from concurrent.futures import Future, ThreadPoolExecutor
|
| 7 |
-
|
| 8 |
-
from datetime import datetime, timedelta
|
| 9 |
-
import pandas as pd
|
| 10 |
-
from langsmith import Client
|
| 11 |
-
from tqdm.auto import tqdm
|
| 12 |
-
|
| 13 |
-
import chainlit as cl
|
| 14 |
-
|
| 15 |
-
async def get_trace(apiKey,task_list):
|
| 16 |
-
try:
|
| 17 |
-
client = Client(api_key=apiKey)
|
| 18 |
-
project_name = "agent-collaboratif-avid"
|
| 19 |
-
num_days = 30
|
| 20 |
-
|
| 21 |
-
# List all tool runs
|
| 22 |
-
tool_runs = client.list_runs(
|
| 23 |
-
project_name=project_name,
|
| 24 |
-
start_time=datetime.now() - timedelta(days=num_days),
|
| 25 |
-
is_root=True,
|
| 26 |
-
# We don't need to fetch inputs, outputs, and other values that # may increase the query time
|
| 27 |
-
select=["inputs","trace_id"],
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
data = []
|
| 31 |
-
futures: list[Future] = []
|
| 32 |
-
trace_cursor = 0
|
| 33 |
-
trace_batch_size = 20
|
| 34 |
-
|
| 35 |
-
tool_runs_by_parent = defaultdict(lambda: defaultdict(set))
|
| 36 |
-
# Do not exceed rate limit
|
| 37 |
-
with ThreadPoolExecutor(max_workers=2) as executor:
|
| 38 |
-
# Group tool runs by parent run ID
|
| 39 |
-
task2 = cl.Task(title="Grouper les outils invoqués dans une trace et les organiser par parent run ID")
|
| 40 |
-
await task_list.add_task(task2)
|
| 41 |
-
for run in tqdm(tool_runs):
|
| 42 |
-
# Collect all tools invoked within a given trace
|
| 43 |
-
tool_runs_by_parent[run.trace_id]["tools_involved"].add(run.name)
|
| 44 |
-
# maybe send a batch of parent run IDs to the server
|
| 45 |
-
# this lets us query for the root runs in batches
|
| 46 |
-
# while still processing the tool runs
|
| 47 |
-
if len(tool_runs_by_parent) % trace_batch_size == 0:
|
| 48 |
-
if this_batch := list(tool_runs_by_parent.keys())[
|
| 49 |
-
trace_cursor : trace_cursor + trace_batch_size
|
| 50 |
-
]:
|
| 51 |
-
trace_cursor += trace_batch_size
|
| 52 |
-
futures.append(
|
| 53 |
-
executor.submit(
|
| 54 |
-
client.list_runs,
|
| 55 |
-
project_name=project_name,
|
| 56 |
-
run_ids=this_batch,
|
| 57 |
-
select=["inputs","trace_id"],
|
| 58 |
-
)
|
| 59 |
-
)
|
| 60 |
-
await task_list.send()
|
| 61 |
-
if this_batch := list(tool_runs_by_parent.keys())[trace_cursor:]:
|
| 62 |
-
futures.append(
|
| 63 |
-
executor.submit(
|
| 64 |
-
client.list_runs,
|
| 65 |
-
project_name=project_name,
|
| 66 |
-
run_ids=this_batch,
|
| 67 |
-
select=["inputs","trace_id"],
|
| 68 |
-
)
|
| 69 |
-
)
|
| 70 |
-
task2.status = cl.TaskStatus.DONE
|
| 71 |
-
await task_list.send()
|
| 72 |
-
task3 = cl.Task(title="Rechercher les données d'actions des utilisateurs de l'agent collabroatif AVID et les organiser par parent run ID dans un DataFrame")
|
| 73 |
-
await task_list.add_task(task3)
|
| 74 |
-
for future in tqdm(futures):
|
| 75 |
-
root_runs = future.result()
|
| 76 |
-
for root_run in root_runs:
|
| 77 |
-
root_data = tool_runs_by_parent[root_run.id]
|
| 78 |
-
data.append(
|
| 79 |
-
{
|
| 80 |
-
"inputs": root_run.inputs,
|
| 81 |
-
"start_time": root_run.start_time,
|
| 82 |
-
"end_time": root_run.end_time,
|
| 83 |
-
}
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
# (Optional): Convert to a pandas DataFrame
|
| 87 |
-
task3.status = cl.TaskStatus.DONE
|
| 88 |
-
await task_list.send()
|
| 89 |
-
|
| 90 |
-
df_inputs = pd.DataFrame(data)
|
| 91 |
-
df_inputs['query'] = df_inputs.apply(lambda x: x.get('inputs', {}).get('query'), axis=1)
|
| 92 |
-
df_inputs['latency'] = df_inputs['end_time'] - df_inputs['start_time']
|
| 93 |
-
df_inputs['latency'] = df_inputs['latency'].apply(lambda x: x.total_seconds())
|
| 94 |
-
df_inputs=df_inputs[["query","latency","start_time"]].copy()
|
| 95 |
-
task4 = cl.Task(title="Conversion des données d'actions des utilisateurs de l'agent collabroatif AVID et les afficher au format texte")
|
| 96 |
-
await task_list.add_task(task4)
|
| 97 |
-
|
| 98 |
-
list_inputs = df_inputs.head(20).values.tolist()
|
| 99 |
-
str_inputs="".join(['* Requête : ' + str(item[0]) + '\nDate : ' + str(item[2]) + '\nDurée de la requête : ' + str(item[1]) + '\n\n' for item in list_inputs])
|
| 100 |
-
task4.status = cl.TaskStatus.DONE
|
| 101 |
-
await task_list.send()
|
| 102 |
-
return str_inputs
|
| 103 |
-
except Exception as e:
|
| 104 |
return f"Aucune connexion à LangSmith"
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import importlib
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
|
| 6 |
+
from concurrent.futures import Future, ThreadPoolExecutor
|
| 7 |
+
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from langsmith import Client
|
| 11 |
+
from tqdm.auto import tqdm
|
| 12 |
+
|
| 13 |
+
import chainlit as cl
|
| 14 |
+
|
| 15 |
+
async def get_trace(apiKey,task_list):
|
| 16 |
+
try:
|
| 17 |
+
client = Client(api_key=apiKey)
|
| 18 |
+
project_name = "agent-collaboratif-avid"
|
| 19 |
+
num_days = 30
|
| 20 |
+
|
| 21 |
+
# List all tool runs
|
| 22 |
+
tool_runs = client.list_runs(
|
| 23 |
+
project_name=project_name,
|
| 24 |
+
start_time=datetime.now() - timedelta(days=num_days),
|
| 25 |
+
is_root=True,
|
| 26 |
+
# We don't need to fetch inputs, outputs, and other values that # may increase the query time
|
| 27 |
+
select=["inputs","trace_id"],
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
data = []
|
| 31 |
+
futures: list[Future] = []
|
| 32 |
+
trace_cursor = 0
|
| 33 |
+
trace_batch_size = 20
|
| 34 |
+
|
| 35 |
+
tool_runs_by_parent = defaultdict(lambda: defaultdict(set))
|
| 36 |
+
# Do not exceed rate limit
|
| 37 |
+
with ThreadPoolExecutor(max_workers=2) as executor:
|
| 38 |
+
# Group tool runs by parent run ID
|
| 39 |
+
task2 = cl.Task(title="Grouper les outils invoqués dans une trace et les organiser par parent run ID")
|
| 40 |
+
await task_list.add_task(task2)
|
| 41 |
+
for run in tqdm(tool_runs):
|
| 42 |
+
# Collect all tools invoked within a given trace
|
| 43 |
+
tool_runs_by_parent[run.trace_id]["tools_involved"].add(run.name)
|
| 44 |
+
# maybe send a batch of parent run IDs to the server
|
| 45 |
+
# this lets us query for the root runs in batches
|
| 46 |
+
# while still processing the tool runs
|
| 47 |
+
if len(tool_runs_by_parent) % trace_batch_size == 0:
|
| 48 |
+
if this_batch := list(tool_runs_by_parent.keys())[
|
| 49 |
+
trace_cursor : trace_cursor + trace_batch_size
|
| 50 |
+
]:
|
| 51 |
+
trace_cursor += trace_batch_size
|
| 52 |
+
futures.append(
|
| 53 |
+
executor.submit(
|
| 54 |
+
client.list_runs,
|
| 55 |
+
project_name=project_name,
|
| 56 |
+
run_ids=this_batch,
|
| 57 |
+
select=["inputs","trace_id"],
|
| 58 |
+
)
|
| 59 |
+
)
|
| 60 |
+
await task_list.send()
|
| 61 |
+
if this_batch := list(tool_runs_by_parent.keys())[trace_cursor:]:
|
| 62 |
+
futures.append(
|
| 63 |
+
executor.submit(
|
| 64 |
+
client.list_runs,
|
| 65 |
+
project_name=project_name,
|
| 66 |
+
run_ids=this_batch,
|
| 67 |
+
select=["inputs","trace_id"],
|
| 68 |
+
)
|
| 69 |
+
)
|
| 70 |
+
task2.status = cl.TaskStatus.DONE
|
| 71 |
+
await task_list.send()
|
| 72 |
+
task3 = cl.Task(title="Rechercher les données d'actions des utilisateurs de l'agent collabroatif AVID et les organiser par parent run ID dans un DataFrame")
|
| 73 |
+
await task_list.add_task(task3)
|
| 74 |
+
for future in tqdm(futures):
|
| 75 |
+
root_runs = future.result()
|
| 76 |
+
for root_run in root_runs:
|
| 77 |
+
root_data = tool_runs_by_parent[root_run.id]
|
| 78 |
+
data.append(
|
| 79 |
+
{
|
| 80 |
+
"inputs": root_run.inputs,
|
| 81 |
+
"start_time": root_run.start_time,
|
| 82 |
+
"end_time": root_run.end_time,
|
| 83 |
+
}
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# (Optional): Convert to a pandas DataFrame
|
| 87 |
+
task3.status = cl.TaskStatus.DONE
|
| 88 |
+
await task_list.send()
|
| 89 |
+
|
| 90 |
+
df_inputs = pd.DataFrame(data)
|
| 91 |
+
df_inputs['query'] = df_inputs.apply(lambda x: x.get('inputs', {}).get('query'), axis=1)
|
| 92 |
+
df_inputs['latency'] = df_inputs['end_time'] - df_inputs['start_time']
|
| 93 |
+
df_inputs['latency'] = df_inputs['latency'].apply(lambda x: x.total_seconds())
|
| 94 |
+
df_inputs=df_inputs[["query","latency","start_time"]].copy()
|
| 95 |
+
task4 = cl.Task(title="Conversion des données d'actions des utilisateurs de l'agent collabroatif AVID et les afficher au format texte")
|
| 96 |
+
await task_list.add_task(task4)
|
| 97 |
+
|
| 98 |
+
list_inputs = df_inputs.head(20).values.tolist()
|
| 99 |
+
str_inputs="".join(['* Requête : ' + str(item[0]) + '\nDate : ' + str(item[2]) + '\nDurée de la requête : ' + str(item[1]) + '\n\n' for item in list_inputs])
|
| 100 |
+
task4.status = cl.TaskStatus.DONE
|
| 101 |
+
await task_list.send()
|
| 102 |
+
return str_inputs
|
| 103 |
+
except Exception as e:
|
| 104 |
return f"Aucune connexion à LangSmith"
|