Skip to content

Commit 1249106

Browse files
committed
crewAI curation
1 parent c7d80b1 commit 1249106

File tree

1 file changed

+121
-29
lines changed
  • src/langtrace_python_sdk/instrumentation/crewai

1 file changed

+121
-29
lines changed

src/langtrace_python_sdk/instrumentation/crewai/patch.py

Lines changed: 121 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"share_crew": "bool",
3434
"step_callback": "object",
3535
"task_callback": "object",
36-
"prompt_file": "object",
37-
"output_log_file": "object",
36+
"prompt_file": "str",
37+
"output_log_file": "bool",
3838
}
3939

4040
task_properties = {
@@ -90,9 +90,125 @@
9090
}
9191

9292

93+
def parse_crewai_tasks(tasks):
94+
task_dict = {}
95+
for task in tasks:
96+
for key, value in task.__dict__.items():
97+
if value is None:
98+
continue
99+
100+
elif key == "id":
101+
task_dict[key] = str(value)
102+
103+
elif key == "agent":
104+
task_dict[key] = value.__dict__["role"]
105+
106+
elif key in [
107+
"used_tools",
108+
"tools_errors",
109+
"delegations",
110+
"description",
111+
"expected_output",
112+
"human_input",
113+
"async_execution",
114+
"prompt_context",
115+
"expected_output",
116+
"output_file",
117+
]:
118+
task_dict[key] = value
119+
else:
120+
task_dict[key] = str(value)
121+
return [task_dict]
122+
123+
124+
def parse_crewai_agents(agents):
125+
agent_dict = {}
126+
for agent in agents:
127+
for key, value in agent.__dict__.items():
128+
if value is None:
129+
continue
130+
131+
elif key == "id":
132+
agent_dict[key] = str(value)
133+
134+
elif key in [
135+
"role" "formatting_errors",
136+
"goal",
137+
"backstory",
138+
"cache",
139+
"verbose",
140+
"max_rpm",
141+
"allow_delegation",
142+
"max_iter",
143+
"max_execution_time",
144+
]:
145+
agent_dict[key] = value
146+
else:
147+
agent_dict[key] = str(value)
148+
return [agent_dict]
149+
150+
151+
def set_crewai_attributes(instance):
152+
crew_config = {}
153+
class_name = instance.__class__.__name__
154+
for key, value in instance.__dict__.items():
155+
if value is None:
156+
continue
157+
158+
if class_name == "Crew":
159+
set_crew_attributes(key, value, crew_config)
160+
elif class_name == "Agent":
161+
set_agent_attributes(key, value, crew_config)
162+
elif class_name == "Task":
163+
set_task_attributes(key, value, crew_config)
164+
165+
return crew_config
166+
167+
168+
def set_crew_attributes(key, value, config: dict):
169+
if key not in crew_properties:
170+
return
171+
172+
if key == "tasks":
173+
config[key] = parse_crewai_tasks(value)
174+
175+
if key == "agents":
176+
config[key] = parse_crewai_agents(value)
177+
178+
# if crew_properties[key] == "json":
179+
# config[key] = json.dumps(value)
180+
# elif crew_properties[key] == "object":
181+
# config[key] = str(value)
182+
# else:
183+
# config[key] = value
184+
185+
186+
def set_agent_attributes(key, value, config: dict):
187+
if key not in agent_properties:
188+
return
189+
190+
if agent_properties[key] == "json":
191+
config[key] = json.dumps(value)
192+
elif agent_properties[key] == "object":
193+
config[key] = str(value)
194+
else:
195+
config[key] = value
196+
197+
198+
def set_task_attributes(key, value, config: dict):
199+
if key not in task_properties:
200+
return
201+
202+
if task_properties[key] == "json":
203+
config[key] = json.dumps(value)
204+
elif task_properties[key] == "object":
205+
config[key] = str(value)
206+
else:
207+
config[key] = value
208+
209+
93210
def patch_crew(operation_name, version, tracer):
94211
def traced_method(wrapped, instance, args, kwargs):
95-
96212
service_provider = SERVICE_PROVIDERS["CREWAI"]
97213
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
98214
span_attributes = {
@@ -104,32 +220,8 @@ def traced_method(wrapped, instance, args, kwargs):
104220
**(extra_attributes if extra_attributes is not None else {}),
105221
}
106222

107-
crew_config = {}
108-
for key, value in instance.__dict__.items():
109-
if instance.__class__.__name__ == "Crew":
110-
if key in crew_properties and value is not None:
111-
if crew_properties[key] == "json":
112-
crew_config[key] = json.dumps(value)
113-
elif crew_properties[key] == "object":
114-
crew_config[key] = str(value)
115-
else:
116-
crew_config[key] = value
117-
elif instance.__class__.__name__ == "Agent":
118-
if key in agent_properties and value is not None:
119-
if agent_properties[key] == "json":
120-
crew_config[key] = json.dumps(value)
121-
elif agent_properties[key] == "object":
122-
crew_config[key] = str(value)
123-
else:
124-
crew_config[key] = value
125-
elif instance.__class__.__name__ == "Task":
126-
if key in task_properties and value is not None:
127-
if task_properties[key] == "json":
128-
crew_config[key] = json.dumps(value)
129-
elif task_properties[key] == "object":
130-
crew_config[key] = str(value)
131-
else:
132-
crew_config[key] = value
223+
crew_config = set_crewai_attributes(instance)
224+
133225
if crew_config:
134226
if instance.__class__.__name__ == "Crew":
135227
if "inputs" in kwargs and kwargs["inputs"]:

0 commit comments

Comments
 (0)