|
15 | 15 | # this is used as the default value for optional parameters |
16 | 16 | OMIT = typing.cast(typing.Any, ...) |
17 | 17 |
|
| 18 | +record_function = lambda *args, **kwargs: lambda fn: fn |
| 19 | + |
18 | 20 |
|
19 | 21 | class MultiOn(BaseMultiOn): |
20 | 22 | """ |
@@ -52,25 +54,37 @@ def __init__( |
52 | 54 | agentops_api_key: typing.Optional[str] = os.getenv("AGENTOPS_API_KEY"), |
53 | 55 | **kwargs |
54 | 56 | ): |
| 57 | + |
55 | 58 | super().__init__(*args, **kwargs) |
56 | | - self.sessions = WrappedSessionsClient(client_wrapper=self._client_wrapper) |
57 | | - if agentops_api_key is not None: |
| 59 | + self._agentops_api_key = agentops_api_key |
| 60 | + |
| 61 | + self.sessions = WrappedSessionsClient( |
| 62 | + client_wrapper=self._client_wrapper, use_agentops=self._agentops_api_key is not None) |
| 63 | + |
| 64 | + if self._agentops_api_key is not None: |
58 | 65 | agentops.init( |
59 | 66 | api_key=agentops_api_key, |
60 | 67 | parent_key=os.getenv("AGENTOPS_PARENT_KEY"), |
61 | 68 | auto_start_session=False, |
62 | 69 | ) |
| 70 | + # Redefine the record_function globally |
| 71 | + global record_function |
| 72 | + record_function = agentops.record_function |
| 73 | + # Apply the decorator at runtime |
| 74 | + self.browse = record_function(event_name="browse")(self.browse) |
| 75 | + self.retrieve = record_function( |
| 76 | + event_name="retrieve")(self.retrieve) |
63 | 77 |
|
64 | | - @agentops.record_function(event_name="browse") # type: ignore |
65 | 78 | @wraps_function(BaseMultiOn.browse) |
66 | 79 | def browse(self, *args, **kwargs) -> BrowseOutput: |
67 | | - agentops.start_session(tags=["multion-sdk"]) |
| 80 | + if self._agentops_api_key is not None: |
| 81 | + agentops.start_session(tags=["multion-sdk"]) |
68 | 82 | return super().browse(*args, **kwargs) |
69 | 83 |
|
70 | | - @agentops.record_function(event_name="retrieve") # type: ignore |
71 | 84 | @wraps_function(BaseMultiOn.retrieve) |
72 | 85 | def retrieve(self, *args, **kwargs) -> RetrieveOutput: |
73 | | - agentops.start_session(tags=["multion-sdk"]) |
| 86 | + if self._agentops_api_key is not None: |
| 87 | + agentops.start_session(tags=["multion-sdk"]) |
74 | 88 | return super().retrieve(*args, **kwargs) |
75 | 89 |
|
76 | 90 |
|
@@ -111,22 +125,31 @@ def __init__( |
111 | 125 | **kwargs |
112 | 126 | ): |
113 | 127 | super().__init__(*args, **kwargs) |
114 | | - self.sessions = WrappedAsyncSessionsClient(client_wrapper=self._client_wrapper) |
| 128 | + self._agentops_api_key = agentops_api_key |
| 129 | + self.sessions = WrappedAsyncSessionsClient(client_wrapper=self._client_wrapper, |
| 130 | + use_agentops=self._agentops_api_key is not None) |
115 | 131 | if agentops_api_key is not None: |
116 | 132 | agentops.init( |
117 | 133 | api_key=agentops_api_key, |
118 | 134 | parent_key=os.getenv("AGENTOPS_PARENT_KEY"), |
119 | 135 | auto_start_session=False, |
120 | 136 | ) |
| 137 | + # If agentops is set, we need to wrap the record function |
| 138 | + global record_function |
| 139 | + record_function = agentops.record_function |
| 140 | + |
| 141 | + record_function = agentops.record_function |
121 | 142 |
|
122 | | - @agentops.record_function(event_name="browse") # type: ignore |
| 143 | + @record_function(event_name="browse") # type: ignore |
123 | 144 | @wraps_function(AsyncBaseMultiOn.browse) |
124 | 145 | async def browse(self, *args, **kwargs) -> BrowseOutput: |
125 | | - agentops.start_session(tags=["multion-sdk"]) |
| 146 | + if self._agentops_api_key is not None: |
| 147 | + agentops.start_session(tags=["multion-sdk"]) |
126 | 148 | return await super().browse(*args, **kwargs) |
127 | 149 |
|
128 | | - @agentops.record_function(event_name="retrieve") # type: ignore |
| 150 | + @record_function(event_name="retrieve") # type: ignore |
129 | 151 | @wraps_function(BaseMultiOn.retrieve) |
130 | 152 | async def retrieve(self, *args, **kwargs) -> RetrieveOutput: |
131 | | - agentops.start_session(tags=["multion-sdk"]) |
| 153 | + if self._agentops_api_key is not None: |
| 154 | + agentops.start_session(tags=["multion-sdk"]) |
132 | 155 | return await super().retrieve(*args, **kwargs) |
0 commit comments