Skip to content

Commit 35e233b

Browse files
authored
Merge pull request #483 from Scale3-Labs/obinna/S3EN-2880-integrate-phidata
Obinna/s3 en 2880 integrate phidata
2 parents 4bfebd6 + 8357e3a commit 35e233b

File tree

12 files changed

+390
-2
lines changed

12 files changed

+390
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ Langtrace automatically captures traces from the following vendors:
8282
| Autogen |||
8383
| HiveAgent |||
8484
| Inspect AI |||
85+
| Graphlit |||
86+
| Phidata |||
87+
| Arch |||
8588

8689
### Vector Databases
8790
| Database | TypeScript SDK | Python SDK |

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ dev = [
6161
"google-cloud-aiplatform",
6262
"mistralai",
6363
"embedchain",
64+
"phidata",
6465
]
6566

6667
test = ["pytest", "pytest-vcr", "pytest-asyncio"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import asyncio
2+
from examples.phidata_example.agent import agent_run
3+
from langtrace_python_sdk import langtrace
4+
5+
langtrace.init()
6+
7+
class PhiDataRunner:
8+
def run(self):
9+
agent_run()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from langtrace_python_sdk import langtrace
2+
from phi.agent import Agent
3+
from phi.model.openai import OpenAIChat
4+
from phi.tools.duckduckgo import DuckDuckGo
5+
6+
langtrace.init()
7+
8+
def agent_run():
9+
web_agent = Agent(
10+
model=OpenAIChat(id="gpt-4o"),
11+
tools=[DuckDuckGo()],
12+
instructions=["Always include sources"],
13+
show_tool_calls=True,
14+
markdown=True,
15+
)
16+
web_agent.print_response("how do I get from lagos to nairobi, through kigali rwanda (staying a week) and how much would it cost on average?", stream=True)

src/langtrace_python_sdk/constants/instrumentation/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"CEREBRAS": "Cerebras",
4242
"MILVUS": "Milvus",
4343
"GRAPHLIT": "Graphlit",
44+
"PHIDATA": "Phidata",
4445
}
4546

4647
LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY = "langtrace_additional_attributes"

src/langtrace_python_sdk/instrumentation/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .milvus import MilvusInstrumentation
3131
from .google_genai import GoogleGenaiInstrumentation
3232
from .graphlit import GraphlitInstrumentation
33+
from .phidata import PhiDataInstrumentation
3334

3435
__all__ = [
3536
"AnthropicInstrumentation",
@@ -61,4 +62,5 @@
6162
"GoogleGenaiInstrumentation",
6263
"CrewaiToolsInstrumentation",
6364
"GraphlitInstrumentation",
65+
"PhiDataInstrumentation",
6466
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .instrumentation import PhiDataInstrumentation
2+
3+
__all__ = [
4+
"PhiDataInstrumentation",
5+
]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
Copyright (c) 2024 Scale3 Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
18+
from opentelemetry.trace import get_tracer
19+
from wrapt import wrap_function_wrapper as _W
20+
from typing import Collection
21+
from importlib_metadata import version as v
22+
from .patch import patch_agent, patch_memory
23+
24+
class PhiDataInstrumentation(BaseInstrumentor):
25+
def instrumentation_dependencies(self) -> Collection[str]:
26+
return ["phidata >= 2.7.10"] # Adjust version as needed
27+
28+
def _instrument(self, **kwargs):
29+
tracer_provider = kwargs.get("tracer_provider")
30+
tracer = get_tracer(__name__, "", tracer_provider)
31+
version = v("phidata")
32+
33+
try:
34+
_W(
35+
"phi.agent.agent",
36+
"Agent.run",
37+
patch_agent("Agent.run", version, tracer),
38+
)
39+
_W(
40+
"phi.agent.agent",
41+
"Agent.arun",
42+
patch_agent("Agent.arun", version, tracer),
43+
)
44+
_W(
45+
"phi.agent.agent",
46+
"Agent._run",
47+
patch_agent("Agent._run", version, tracer),
48+
)
49+
_W(
50+
"phi.agent.agent",
51+
"Agent._arun",
52+
patch_agent("Agent._arun", version, tracer),
53+
)
54+
55+
_W(
56+
"phi.memory.agent",
57+
"AgentMemory.update_memory",
58+
patch_memory("AgentMemory.update_memory", version, tracer),
59+
)
60+
_W(
61+
"phi.memory.agent",
62+
"AgentMemory.aupdate_memory",
63+
patch_memory("AgentMemory.aupdate_memory", version, tracer),
64+
)
65+
_W(
66+
"phi.memory.agent",
67+
"AgentMemory.update_summary",
68+
patch_memory("AgentMemory.update_summary", version, tracer),
69+
)
70+
_W(
71+
"phi.memory.agent",
72+
"AgentMemory.aupdate_summary",
73+
patch_memory("AgentMemory.aupdate_summary", version, tracer),
74+
)
75+
76+
except Exception:
77+
pass
78+
79+
def _uninstrument(self, **kwargs):
80+
pass

0 commit comments

Comments
 (0)