mantisdk 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mantisdk might be problematic. Click here for more details.
- mantisdk/__init__.py +22 -0
- mantisdk/adapter/__init__.py +15 -0
- mantisdk/adapter/base.py +94 -0
- mantisdk/adapter/messages.py +270 -0
- mantisdk/adapter/triplet.py +1028 -0
- mantisdk/algorithm/__init__.py +39 -0
- mantisdk/algorithm/apo/__init__.py +5 -0
- mantisdk/algorithm/apo/apo.py +889 -0
- mantisdk/algorithm/apo/prompts/apply_edit_variant01.poml +22 -0
- mantisdk/algorithm/apo/prompts/apply_edit_variant02.poml +18 -0
- mantisdk/algorithm/apo/prompts/text_gradient_variant01.poml +18 -0
- mantisdk/algorithm/apo/prompts/text_gradient_variant02.poml +16 -0
- mantisdk/algorithm/apo/prompts/text_gradient_variant03.poml +107 -0
- mantisdk/algorithm/base.py +162 -0
- mantisdk/algorithm/decorator.py +264 -0
- mantisdk/algorithm/fast.py +250 -0
- mantisdk/algorithm/gepa/__init__.py +59 -0
- mantisdk/algorithm/gepa/adapter.py +459 -0
- mantisdk/algorithm/gepa/gepa.py +364 -0
- mantisdk/algorithm/gepa/lib/__init__.py +18 -0
- mantisdk/algorithm/gepa/lib/adapters/README.md +12 -0
- mantisdk/algorithm/gepa/lib/adapters/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/README.md +341 -0
- mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/__init__.py +1 -0
- mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/anymaths_adapter.py +174 -0
- mantisdk/algorithm/gepa/lib/adapters/anymaths_adapter/requirements.txt +1 -0
- mantisdk/algorithm/gepa/lib/adapters/default_adapter/README.md +0 -0
- mantisdk/algorithm/gepa/lib/adapters/default_adapter/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/adapters/default_adapter/default_adapter.py +209 -0
- mantisdk/algorithm/gepa/lib/adapters/dspy_adapter/README.md +7 -0
- mantisdk/algorithm/gepa/lib/adapters/dspy_adapter/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/adapters/dspy_adapter/dspy_adapter.py +307 -0
- mantisdk/algorithm/gepa/lib/adapters/dspy_full_program_adapter/README.md +99 -0
- mantisdk/algorithm/gepa/lib/adapters/dspy_full_program_adapter/dspy_program_proposal_signature.py +137 -0
- mantisdk/algorithm/gepa/lib/adapters/dspy_full_program_adapter/full_program_adapter.py +266 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/GEPA_RAG.md +621 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/__init__.py +56 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/evaluation_metrics.py +226 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/generic_rag_adapter.py +496 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/rag_pipeline.py +238 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_store_interface.py +212 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/__init__.py +2 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/chroma_store.py +196 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/lancedb_store.py +422 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/milvus_store.py +409 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/qdrant_store.py +368 -0
- mantisdk/algorithm/gepa/lib/adapters/generic_rag_adapter/vector_stores/weaviate_store.py +418 -0
- mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/README.md +552 -0
- mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/__init__.py +37 -0
- mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/mcp_adapter.py +705 -0
- mantisdk/algorithm/gepa/lib/adapters/mcp_adapter/mcp_client.py +364 -0
- mantisdk/algorithm/gepa/lib/adapters/terminal_bench_adapter/README.md +9 -0
- mantisdk/algorithm/gepa/lib/adapters/terminal_bench_adapter/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/adapters/terminal_bench_adapter/terminal_bench_adapter.py +217 -0
- mantisdk/algorithm/gepa/lib/api.py +375 -0
- mantisdk/algorithm/gepa/lib/core/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/core/adapter.py +180 -0
- mantisdk/algorithm/gepa/lib/core/data_loader.py +74 -0
- mantisdk/algorithm/gepa/lib/core/engine.py +356 -0
- mantisdk/algorithm/gepa/lib/core/result.py +233 -0
- mantisdk/algorithm/gepa/lib/core/state.py +636 -0
- mantisdk/algorithm/gepa/lib/examples/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/examples/aime.py +24 -0
- mantisdk/algorithm/gepa/lib/examples/anymaths-bench/eval_default.py +111 -0
- mantisdk/algorithm/gepa/lib/examples/anymaths-bench/prompt-templates/instruction_prompt.txt +9 -0
- mantisdk/algorithm/gepa/lib/examples/anymaths-bench/prompt-templates/optimal_prompt.txt +24 -0
- mantisdk/algorithm/gepa/lib/examples/anymaths-bench/train_anymaths.py +177 -0
- mantisdk/algorithm/gepa/lib/examples/dspy_full_program_evolution/arc_agi.ipynb +25705 -0
- mantisdk/algorithm/gepa/lib/examples/dspy_full_program_evolution/example.ipynb +348 -0
- mantisdk/algorithm/gepa/lib/examples/mcp_adapter/__init__.py +4 -0
- mantisdk/algorithm/gepa/lib/examples/mcp_adapter/mcp_optimization_example.py +455 -0
- mantisdk/algorithm/gepa/lib/examples/rag_adapter/RAG_GUIDE.md +613 -0
- mantisdk/algorithm/gepa/lib/examples/rag_adapter/__init__.py +9 -0
- mantisdk/algorithm/gepa/lib/examples/rag_adapter/rag_optimization.py +824 -0
- mantisdk/algorithm/gepa/lib/examples/rag_adapter/requirements-rag.txt +29 -0
- mantisdk/algorithm/gepa/lib/examples/terminal-bench/prompt-templates/instruction_prompt.txt +16 -0
- mantisdk/algorithm/gepa/lib/examples/terminal-bench/prompt-templates/terminus.txt +9 -0
- mantisdk/algorithm/gepa/lib/examples/terminal-bench/train_terminus.py +161 -0
- mantisdk/algorithm/gepa/lib/gepa_utils.py +117 -0
- mantisdk/algorithm/gepa/lib/logging/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/logging/experiment_tracker.py +187 -0
- mantisdk/algorithm/gepa/lib/logging/logger.py +75 -0
- mantisdk/algorithm/gepa/lib/logging/utils.py +103 -0
- mantisdk/algorithm/gepa/lib/proposer/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/proposer/base.py +31 -0
- mantisdk/algorithm/gepa/lib/proposer/merge.py +357 -0
- mantisdk/algorithm/gepa/lib/proposer/reflective_mutation/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/proposer/reflective_mutation/base.py +49 -0
- mantisdk/algorithm/gepa/lib/proposer/reflective_mutation/reflective_mutation.py +176 -0
- mantisdk/algorithm/gepa/lib/py.typed +0 -0
- mantisdk/algorithm/gepa/lib/strategies/__init__.py +0 -0
- mantisdk/algorithm/gepa/lib/strategies/batch_sampler.py +77 -0
- mantisdk/algorithm/gepa/lib/strategies/candidate_selector.py +50 -0
- mantisdk/algorithm/gepa/lib/strategies/component_selector.py +36 -0
- mantisdk/algorithm/gepa/lib/strategies/eval_policy.py +64 -0
- mantisdk/algorithm/gepa/lib/strategies/instruction_proposal.py +127 -0
- mantisdk/algorithm/gepa/lib/utils/__init__.py +10 -0
- mantisdk/algorithm/gepa/lib/utils/stop_condition.py +196 -0
- mantisdk/algorithm/gepa/tracing.py +105 -0
- mantisdk/algorithm/utils.py +177 -0
- mantisdk/algorithm/verl/__init__.py +5 -0
- mantisdk/algorithm/verl/interface.py +202 -0
- mantisdk/cli/__init__.py +56 -0
- mantisdk/cli/prometheus.py +115 -0
- mantisdk/cli/store.py +131 -0
- mantisdk/cli/vllm.py +29 -0
- mantisdk/client.py +408 -0
- mantisdk/config.py +348 -0
- mantisdk/emitter/__init__.py +43 -0
- mantisdk/emitter/annotation.py +370 -0
- mantisdk/emitter/exception.py +54 -0
- mantisdk/emitter/message.py +61 -0
- mantisdk/emitter/object.py +117 -0
- mantisdk/emitter/reward.py +320 -0
- mantisdk/env_var.py +156 -0
- mantisdk/execution/__init__.py +15 -0
- mantisdk/execution/base.py +64 -0
- mantisdk/execution/client_server.py +443 -0
- mantisdk/execution/events.py +69 -0
- mantisdk/execution/inter_process.py +16 -0
- mantisdk/execution/shared_memory.py +282 -0
- mantisdk/instrumentation/__init__.py +119 -0
- mantisdk/instrumentation/agentops.py +314 -0
- mantisdk/instrumentation/agentops_langchain.py +45 -0
- mantisdk/instrumentation/litellm.py +83 -0
- mantisdk/instrumentation/vllm.py +81 -0
- mantisdk/instrumentation/weave.py +500 -0
- mantisdk/litagent/__init__.py +11 -0
- mantisdk/litagent/decorator.py +536 -0
- mantisdk/litagent/litagent.py +252 -0
- mantisdk/llm_proxy.py +1890 -0
- mantisdk/logging.py +370 -0
- mantisdk/reward.py +7 -0
- mantisdk/runner/__init__.py +11 -0
- mantisdk/runner/agent.py +845 -0
- mantisdk/runner/base.py +182 -0
- mantisdk/runner/legacy.py +309 -0
- mantisdk/semconv.py +170 -0
- mantisdk/server.py +401 -0
- mantisdk/store/__init__.py +23 -0
- mantisdk/store/base.py +897 -0
- mantisdk/store/client_server.py +2092 -0
- mantisdk/store/collection/__init__.py +30 -0
- mantisdk/store/collection/base.py +587 -0
- mantisdk/store/collection/memory.py +970 -0
- mantisdk/store/collection/mongo.py +1412 -0
- mantisdk/store/collection_based.py +1823 -0
- mantisdk/store/insight.py +648 -0
- mantisdk/store/listener.py +58 -0
- mantisdk/store/memory.py +396 -0
- mantisdk/store/mongo.py +165 -0
- mantisdk/store/sqlite.py +3 -0
- mantisdk/store/threading.py +357 -0
- mantisdk/store/utils.py +142 -0
- mantisdk/tracer/__init__.py +16 -0
- mantisdk/tracer/agentops.py +242 -0
- mantisdk/tracer/base.py +287 -0
- mantisdk/tracer/dummy.py +106 -0
- mantisdk/tracer/otel.py +555 -0
- mantisdk/tracer/weave.py +677 -0
- mantisdk/trainer/__init__.py +6 -0
- mantisdk/trainer/init_utils.py +263 -0
- mantisdk/trainer/legacy.py +367 -0
- mantisdk/trainer/registry.py +12 -0
- mantisdk/trainer/trainer.py +618 -0
- mantisdk/types/__init__.py +6 -0
- mantisdk/types/core.py +553 -0
- mantisdk/types/resources.py +204 -0
- mantisdk/types/tracer.py +515 -0
- mantisdk/types/tracing.py +218 -0
- mantisdk/utils/__init__.py +1 -0
- mantisdk/utils/id.py +18 -0
- mantisdk/utils/metrics.py +1025 -0
- mantisdk/utils/otel.py +578 -0
- mantisdk/utils/otlp.py +536 -0
- mantisdk/utils/server_launcher.py +1045 -0
- mantisdk/utils/system_snapshot.py +81 -0
- mantisdk/verl/__init__.py +8 -0
- mantisdk/verl/__main__.py +6 -0
- mantisdk/verl/async_server.py +46 -0
- mantisdk/verl/config.yaml +27 -0
- mantisdk/verl/daemon.py +1154 -0
- mantisdk/verl/dataset.py +44 -0
- mantisdk/verl/entrypoint.py +248 -0
- mantisdk/verl/trainer.py +549 -0
- mantisdk-0.1.0.dist-info/METADATA +119 -0
- mantisdk-0.1.0.dist-info/RECORD +190 -0
- mantisdk-0.1.0.dist-info/WHEEL +4 -0
- mantisdk-0.1.0.dist-info/entry_points.txt +2 -0
- mantisdk-0.1.0.dist-info/licenses/LICENSE +19 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import logging
|
|
7
|
+
from typing import Any, Callable, no_type_check
|
|
8
|
+
|
|
9
|
+
import requests
|
|
10
|
+
from agentops.client.api import V3Client, V4Client
|
|
11
|
+
from agentops.client.api.types import AuthTokenResponse
|
|
12
|
+
from agentops.sdk.exporters import AuthenticatedOTLPExporter
|
|
13
|
+
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
|
|
14
|
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
|
15
|
+
from opentelemetry.sdk.metrics.export import MetricExportResult
|
|
16
|
+
|
|
17
|
+
from mantisdk.utils.otlp import LightningStoreOTLPExporter
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"instrument_agentops",
|
|
23
|
+
"uninstrument_agentops",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# Module-level storage for originals
|
|
27
|
+
_original_handle_chat_attributes: Callable[..., Any] | None = None
|
|
28
|
+
_original_handle_response: Callable[..., Any] | None = None
|
|
29
|
+
_agentops_service_enabled = False
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def enable_agentops_service(enabled: bool = True) -> None:
|
|
33
|
+
"""
|
|
34
|
+
Enable or disable communication with the AgentOps service.
|
|
35
|
+
|
|
36
|
+
By default, AgentOps exporters and clients will run in local mode
|
|
37
|
+
and will NOT attempt to communicate with the remote AgentOps service.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
enabled: If True, enable all AgentOps exporters and clients.
|
|
41
|
+
All exporters and clients will operate in normal mode and send data
|
|
42
|
+
to the [AgentOps service](https://www.agentops.ai).
|
|
43
|
+
"""
|
|
44
|
+
global _agentops_service_enabled
|
|
45
|
+
_agentops_service_enabled = enabled
|
|
46
|
+
logger.info(f"AgentOps service enabled is set to {enabled}.")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _patch_exporters():
|
|
50
|
+
import agentops.client.api
|
|
51
|
+
import agentops.sdk.core
|
|
52
|
+
|
|
53
|
+
agentops.sdk.core.AuthenticatedOTLPExporter = BypassableAuthenticatedOTLPExporter # type: ignore
|
|
54
|
+
agentops.sdk.core.OTLPMetricExporter = BypassableOTLPMetricExporter
|
|
55
|
+
if hasattr(agentops.sdk.core, "OTLPSpanExporter"):
|
|
56
|
+
agentops.sdk.core.OTLPSpanExporter = BypassableOTLPSpanExporter # type: ignore
|
|
57
|
+
agentops.client.api.V3Client = BypassableV3Client
|
|
58
|
+
agentops.client.api.V4Client = BypassableV4Client
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _unpatch_exporters():
|
|
62
|
+
import agentops.client.api
|
|
63
|
+
import agentops.sdk.core
|
|
64
|
+
|
|
65
|
+
agentops.sdk.core.AuthenticatedOTLPExporter = AuthenticatedOTLPExporter # type: ignore
|
|
66
|
+
agentops.sdk.core.OTLPMetricExporter = OTLPMetricExporter
|
|
67
|
+
if hasattr(agentops.sdk.core, "OTLPSpanExporter"):
|
|
68
|
+
agentops.sdk.core.OTLPSpanExporter = OTLPSpanExporter # type: ignore
|
|
69
|
+
agentops.client.api.V3Client = V3Client
|
|
70
|
+
agentops.client.api.V4Client = V4Client
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _unwrap_legacy_response(response: Any) -> Any:
|
|
74
|
+
if hasattr(response, "parse") and callable(response.parse):
|
|
75
|
+
return response.parse()
|
|
76
|
+
return response
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _patch_new_agentops():
|
|
80
|
+
import agentops.instrumentation.providers.openai.stream_wrapper
|
|
81
|
+
import agentops.instrumentation.providers.openai.wrappers.chat
|
|
82
|
+
from agentops.instrumentation.providers.openai.wrappers.chat import handle_chat_attributes # type: ignore
|
|
83
|
+
|
|
84
|
+
global _original_handle_chat_attributes
|
|
85
|
+
|
|
86
|
+
if _original_handle_chat_attributes is not None:
|
|
87
|
+
logger.warning("AgentOps already patched. Skipping.")
|
|
88
|
+
return True
|
|
89
|
+
|
|
90
|
+
_original_handle_chat_attributes = handle_chat_attributes # type: ignore
|
|
91
|
+
|
|
92
|
+
@no_type_check
|
|
93
|
+
def _handle_chat_attributes_with_tokens(args=None, kwargs=None, return_value=None, **kws): # type: ignore
|
|
94
|
+
attributes = _original_handle_chat_attributes(args=args, kwargs=kwargs, return_value=return_value, **kws)
|
|
95
|
+
|
|
96
|
+
# In some cases, response is a openai._legacy_response.LegacyAPIResponse (e.g., LiteLLM, or LangChain),
|
|
97
|
+
# This is created by client.with_raw_response.create()
|
|
98
|
+
return_value = _unwrap_legacy_response(return_value)
|
|
99
|
+
|
|
100
|
+
if (
|
|
101
|
+
return_value is not None
|
|
102
|
+
and hasattr(return_value, "prompt_token_ids")
|
|
103
|
+
and return_value.prompt_token_ids is not None
|
|
104
|
+
):
|
|
105
|
+
attributes["prompt_token_ids"] = list(return_value.prompt_token_ids)
|
|
106
|
+
if (
|
|
107
|
+
return_value is not None
|
|
108
|
+
and hasattr(return_value, "response_token_ids")
|
|
109
|
+
and return_value.response_token_ids is not None
|
|
110
|
+
):
|
|
111
|
+
attributes["response_token_ids"] = list(return_value.response_token_ids[0])
|
|
112
|
+
|
|
113
|
+
# For LiteLLM Proxy (v0.2) with vLLM return_token_ids, response_token_ids now lives in choices
|
|
114
|
+
if (
|
|
115
|
+
return_value is not None
|
|
116
|
+
and hasattr(return_value, "choices")
|
|
117
|
+
and return_value.choices
|
|
118
|
+
and isinstance(return_value.choices, list)
|
|
119
|
+
and len(return_value.choices) > 0
|
|
120
|
+
):
|
|
121
|
+
first_choice = return_value.choices[0]
|
|
122
|
+
# Token IDs from "choices[0].token_ids"
|
|
123
|
+
if "response_token_ids" not in attributes:
|
|
124
|
+
if hasattr(first_choice, "token_ids") and first_choice.token_ids is not None:
|
|
125
|
+
attributes["response_token_ids"] = list(first_choice.token_ids)
|
|
126
|
+
# newer versions of OpenAI client SDK
|
|
127
|
+
elif (
|
|
128
|
+
hasattr(first_choice, "provider_specific_fields")
|
|
129
|
+
and first_choice.provider_specific_fields.get("token_ids") is not None
|
|
130
|
+
):
|
|
131
|
+
attributes["response_token_ids"] = list(first_choice.provider_specific_fields["token_ids"])
|
|
132
|
+
|
|
133
|
+
# log probability
|
|
134
|
+
# This is temporary. We need a unified convention for classifying and naming logprobs.
|
|
135
|
+
if hasattr(first_choice, "logprobs") and first_choice.logprobs is not None:
|
|
136
|
+
if hasattr(first_choice.logprobs, "content") and first_choice.logprobs.content is not None:
|
|
137
|
+
attributes["logprobs.content"] = json.dumps(
|
|
138
|
+
[logprob.model_dump() for logprob in first_choice.logprobs.content]
|
|
139
|
+
)
|
|
140
|
+
if hasattr(first_choice.logprobs, "refusal") and first_choice.logprobs.refusal is not None:
|
|
141
|
+
attributes["logprobs.refusal"] = json.dumps(
|
|
142
|
+
[logprob.model_dump() for logprob in first_choice.logprobs.refusal]
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
return attributes
|
|
146
|
+
|
|
147
|
+
agentops.instrumentation.providers.openai.wrappers.chat.handle_chat_attributes = _handle_chat_attributes_with_tokens
|
|
148
|
+
agentops.instrumentation.providers.openai.stream_wrapper.handle_chat_attributes = (
|
|
149
|
+
_handle_chat_attributes_with_tokens
|
|
150
|
+
)
|
|
151
|
+
logger.info("Patched newer version of agentops using handle_chat_attributes")
|
|
152
|
+
return True
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _unpatch_new_agentops():
|
|
156
|
+
import agentops.instrumentation.providers.openai.stream_wrapper
|
|
157
|
+
import agentops.instrumentation.providers.openai.wrappers.chat
|
|
158
|
+
|
|
159
|
+
global _original_handle_chat_attributes
|
|
160
|
+
if _original_handle_chat_attributes is not None:
|
|
161
|
+
agentops.instrumentation.providers.openai.wrappers.chat.handle_chat_attributes = (
|
|
162
|
+
_original_handle_chat_attributes
|
|
163
|
+
)
|
|
164
|
+
agentops.instrumentation.providers.openai.stream_wrapper.handle_chat_attributes = (
|
|
165
|
+
_original_handle_chat_attributes
|
|
166
|
+
)
|
|
167
|
+
_original_handle_chat_attributes = None
|
|
168
|
+
logger.info("Unpatched newer version of agentops using handle_chat_attributes")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _patch_old_agentops():
|
|
172
|
+
import opentelemetry.instrumentation.openai.shared.chat_wrappers # type: ignore
|
|
173
|
+
from opentelemetry.instrumentation.openai.shared.chat_wrappers import _handle_response, dont_throw # type: ignore
|
|
174
|
+
|
|
175
|
+
global _original_handle_response
|
|
176
|
+
_original_handle_response = _handle_response # type: ignore
|
|
177
|
+
|
|
178
|
+
@dont_throw # type: ignore
|
|
179
|
+
def _handle_response_with_tokens(response, span, *args, **kwargs): # type: ignore
|
|
180
|
+
_original_handle_response(response, span, *args, **kwargs) # type: ignore
|
|
181
|
+
if hasattr(response, "prompt_token_ids"): # type: ignore
|
|
182
|
+
span.set_attribute("prompt_token_ids", list(response.prompt_token_ids)) # type: ignore
|
|
183
|
+
if hasattr(response, "response_token_ids"): # type: ignore
|
|
184
|
+
span.set_attribute("response_token_ids", list(response.response_token_ids[0])) # type: ignore
|
|
185
|
+
|
|
186
|
+
# For LiteLLM, response is a openai._legacy_response.LegacyAPIResponse
|
|
187
|
+
if hasattr(response, "http_response") and hasattr(response.http_response, "json"): # type: ignore
|
|
188
|
+
json_data = response.http_response.json() # type: ignore
|
|
189
|
+
if isinstance(json_data, dict):
|
|
190
|
+
if "prompt_token_ids" in json_data:
|
|
191
|
+
span.set_attribute("prompt_token_ids", list(json_data["prompt_token_ids"])) # type: ignore
|
|
192
|
+
if "response_token_ids" in json_data:
|
|
193
|
+
span.set_attribute("response_token_ids", list(json_data["response_token_ids"][0])) # type: ignore
|
|
194
|
+
|
|
195
|
+
opentelemetry.instrumentation.openai.shared.chat_wrappers._handle_response = _handle_response_with_tokens # type: ignore
|
|
196
|
+
logger.info("Patched earlier version of agentops using _handle_response")
|
|
197
|
+
return True
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _unpatch_old_agentops():
|
|
201
|
+
import opentelemetry.instrumentation.openai.shared.chat_wrappers # type: ignore
|
|
202
|
+
|
|
203
|
+
global _original_handle_response
|
|
204
|
+
if _original_handle_response is not None:
|
|
205
|
+
opentelemetry.instrumentation.openai.shared.chat_wrappers._handle_response = _original_handle_response # type: ignore
|
|
206
|
+
_original_handle_response = None
|
|
207
|
+
logger.info("Unpatched earlier version of agentops using _handle_response")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def instrument_agentops():
|
|
211
|
+
"""
|
|
212
|
+
Instrument agentops to capture token IDs.
|
|
213
|
+
Automatically detects and uses the appropriate patching method based on the installed agentops version.
|
|
214
|
+
"""
|
|
215
|
+
_patch_exporters()
|
|
216
|
+
|
|
217
|
+
# Try newest version first (tested for 0.4.16)
|
|
218
|
+
try:
|
|
219
|
+
return _patch_new_agentops()
|
|
220
|
+
except ImportError as e:
|
|
221
|
+
logger.debug(f"Couldn't patch newer version of agentops: {str(e)}")
|
|
222
|
+
|
|
223
|
+
# Note: 0.4.15 needs another patching method, but it's too shortlived to be worth handling separately.
|
|
224
|
+
|
|
225
|
+
# Try older version (tested for 0.4.13)
|
|
226
|
+
try:
|
|
227
|
+
return _patch_old_agentops()
|
|
228
|
+
except ImportError as e:
|
|
229
|
+
logger.warning(f"Couldn't patch older version of agentops: {str(e)}")
|
|
230
|
+
logger.error("Failed to instrument agentops - neither patching method was successful")
|
|
231
|
+
return False
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def uninstrument_agentops():
|
|
235
|
+
"""Uninstrument agentops to stop capturing token IDs."""
|
|
236
|
+
_unpatch_exporters()
|
|
237
|
+
|
|
238
|
+
try:
|
|
239
|
+
_unpatch_new_agentops()
|
|
240
|
+
except Exception:
|
|
241
|
+
pass
|
|
242
|
+
try:
|
|
243
|
+
_unpatch_old_agentops()
|
|
244
|
+
except Exception:
|
|
245
|
+
pass
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class BypassableAuthenticatedOTLPExporter(LightningStoreOTLPExporter, AuthenticatedOTLPExporter):
|
|
249
|
+
"""
|
|
250
|
+
AuthenticatedOTLPExporter with switchable service control.
|
|
251
|
+
|
|
252
|
+
When `_agentops_service_enabled` is False, skip export and return success.
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
def should_bypass(self) -> bool:
|
|
256
|
+
return not _agentops_service_enabled
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
class BypassableOTLPMetricExporter(OTLPMetricExporter):
|
|
260
|
+
"""
|
|
261
|
+
OTLPMetricExporter with switchable service control.
|
|
262
|
+
When `_agentops_service_enabled` is False, skip export and return success.
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
def export(self, *args: Any, **kwargs: Any) -> MetricExportResult:
|
|
266
|
+
if _agentops_service_enabled:
|
|
267
|
+
return super().export(*args, **kwargs) # type: ignore[reportUnknownMemberType]
|
|
268
|
+
else:
|
|
269
|
+
logger.debug("SwitchableOTLPMetricExporter is switched off, skipping export.")
|
|
270
|
+
return MetricExportResult.SUCCESS
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
class BypassableOTLPSpanExporter(LightningStoreOTLPExporter):
|
|
274
|
+
"""
|
|
275
|
+
OTLPSpanExporter with switchable service control.
|
|
276
|
+
When `_agentops_service_enabled` is False, skip export and return success.
|
|
277
|
+
|
|
278
|
+
This is used instead of BypassableAuthenticatedOTLPExporter on legacy AgentOps versions.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
def should_bypass(self) -> bool:
|
|
282
|
+
return not _agentops_service_enabled
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
class BypassableV3Client(V3Client):
|
|
286
|
+
"""
|
|
287
|
+
V3Client with toggleable authentication calls.
|
|
288
|
+
Returns dummy auth response when `_agentops_service_enabled` is False.
|
|
289
|
+
"""
|
|
290
|
+
|
|
291
|
+
# Temporary synchronous override of fetch_auth_token for mock purposes.
|
|
292
|
+
def fetch_auth_token(self, *args: Any, **kwargs: Any) -> AuthTokenResponse: # type: ignore[override]
|
|
293
|
+
if _agentops_service_enabled:
|
|
294
|
+
return super().fetch_auth_token(*args, **kwargs) # type: ignore[override]
|
|
295
|
+
else:
|
|
296
|
+
logger.debug("SwitchableV3Client is switched off, skipping fetch_auth_token request.")
|
|
297
|
+
return AuthTokenResponse(token="dummy", project_id="dummy")
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
class BypassableV4Client(V4Client):
|
|
301
|
+
"""
|
|
302
|
+
V4Client with toggleable post requests.
|
|
303
|
+
Returns dummy response when `_agentops_service_enabled` is False.
|
|
304
|
+
"""
|
|
305
|
+
|
|
306
|
+
def post(self, *args: Any, **kwargs: Any) -> requests.Response:
|
|
307
|
+
if _agentops_service_enabled:
|
|
308
|
+
return super().post(*args, **kwargs)
|
|
309
|
+
else:
|
|
310
|
+
logger.debug("SwitchableV4Client is switched off, skipping post request.")
|
|
311
|
+
response = requests.Response()
|
|
312
|
+
response.status_code = 200
|
|
313
|
+
response._content = b"{}"
|
|
314
|
+
return response
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict
|
|
4
|
+
|
|
5
|
+
from agentops import instrumentation
|
|
6
|
+
from agentops.integration.callbacks.langchain import LangchainCallbackHandler
|
|
7
|
+
|
|
8
|
+
original_on_chain_start = LangchainCallbackHandler.on_chain_start
|
|
9
|
+
langgraph_entry = None
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"instrument_agentops_langchain",
|
|
13
|
+
"uninstrument_agentops_langchain",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def on_chain_start(self: Any, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) -> None:
|
|
18
|
+
if "name" in kwargs:
|
|
19
|
+
if serialized is None: # type: ignore
|
|
20
|
+
serialized = {}
|
|
21
|
+
serialized = serialized.copy()
|
|
22
|
+
serialized["name"] = kwargs["name"]
|
|
23
|
+
if "run_id" in kwargs:
|
|
24
|
+
if serialized is None: # type: ignore
|
|
25
|
+
serialized = {}
|
|
26
|
+
serialized = serialized.copy()
|
|
27
|
+
if "id" not in serialized:
|
|
28
|
+
serialized["id"] = kwargs["run_id"]
|
|
29
|
+
return original_on_chain_start(self, serialized, inputs, **kwargs)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def instrument_agentops_langchain():
|
|
33
|
+
"""Bypass AgentOp's native support for Langchain."""
|
|
34
|
+
global langgraph_entry
|
|
35
|
+
langgraph_entry = instrumentation.AGENTIC_LIBRARIES.pop("langgraph", None)
|
|
36
|
+
LangchainCallbackHandler.on_chain_start = on_chain_start
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def uninstrument_agentops_langchain():
|
|
40
|
+
"""Restore AgentOp's native support for Langchain."""
|
|
41
|
+
global langgraph_entry
|
|
42
|
+
if langgraph_entry is not None:
|
|
43
|
+
instrumentation.AGENTIC_LIBRARIES["langgraph"] = langgraph_entry
|
|
44
|
+
langgraph_entry = None
|
|
45
|
+
LangchainCallbackHandler.on_chain_start = original_on_chain_start
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
"""LiteLLM instrumentations.
|
|
4
|
+
|
|
5
|
+
Patches LiteLLM's OpenTelemetry integration to add Mantisdk-specific attributes
|
|
6
|
+
to spans, including session_id, tags, environment, and call_type from context.
|
|
7
|
+
|
|
8
|
+
The call_type is read from a ContextVar that is set by algorithm-specific
|
|
9
|
+
decorators (e.g., @gepa.judge, @gepa.agent).
|
|
10
|
+
|
|
11
|
+
[Related documentation](https://docs.litellm.ai/docs/observability/agentops_integration).
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
from typing import Any, Optional
|
|
16
|
+
|
|
17
|
+
from litellm.integrations.opentelemetry import OpenTelemetry
|
|
18
|
+
|
|
19
|
+
from mantisdk.types.tracing import get_current_call_type
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"instrument_litellm",
|
|
23
|
+
"uninstrument_litellm",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
original_set_attributes = OpenTelemetry.set_attributes # type: ignore
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def patched_set_attributes(self: Any, span: Any, kwargs: Any, response_obj: Optional[Any]):
|
|
30
|
+
"""Enhanced set_attributes that adds Mantisdk tracing metadata to spans.
|
|
31
|
+
|
|
32
|
+
Extracts session_id, tags, and environment from kwargs["metadata"] and sets
|
|
33
|
+
them as OTEL span attributes for visibility in Insight/Langfuse.
|
|
34
|
+
|
|
35
|
+
Also reads the current call_type from context (set by decorators like
|
|
36
|
+
@gepa.judge) and adds it as a span attribute.
|
|
37
|
+
"""
|
|
38
|
+
original_set_attributes(self, span, kwargs, response_obj)
|
|
39
|
+
|
|
40
|
+
# Add token IDs if available
|
|
41
|
+
if response_obj is not None and response_obj.get("prompt_token_ids"):
|
|
42
|
+
span.set_attribute("prompt_token_ids", list(response_obj.get("prompt_token_ids")))
|
|
43
|
+
if response_obj is not None and response_obj.get("response_token_ids"):
|
|
44
|
+
span.set_attribute("response_token_ids", list(response_obj.get("response_token_ids")[0]))
|
|
45
|
+
|
|
46
|
+
# Read call_type from context (set by @gepa.judge, @gepa.agent, etc.)
|
|
47
|
+
call_type = get_current_call_type()
|
|
48
|
+
if call_type:
|
|
49
|
+
span.set_attribute("mantis.call_type", call_type)
|
|
50
|
+
|
|
51
|
+
# Extract Mantisdk tracing metadata from kwargs
|
|
52
|
+
metadata = kwargs.get("metadata", {}) if kwargs else {}
|
|
53
|
+
if not metadata:
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
# Set session_id as span attribute (standard Langfuse/Insight attribute)
|
|
57
|
+
session_id = metadata.get("session_id")
|
|
58
|
+
if session_id:
|
|
59
|
+
span.set_attribute("session.id", session_id)
|
|
60
|
+
|
|
61
|
+
# Set tags as span attribute (JSON array for Langfuse compatibility)
|
|
62
|
+
tags = metadata.get("tags")
|
|
63
|
+
if tags and isinstance(tags, list):
|
|
64
|
+
# Langfuse expects tags as a JSON array string or individual attributes
|
|
65
|
+
span.set_attribute("tags", json.dumps(tags))
|
|
66
|
+
# Also set individual tag attributes for filtering
|
|
67
|
+
for i, tag in enumerate(tags):
|
|
68
|
+
span.set_attribute(f"tag.{i}", str(tag))
|
|
69
|
+
|
|
70
|
+
# Set environment as span attribute
|
|
71
|
+
environment = metadata.get("environment")
|
|
72
|
+
if environment:
|
|
73
|
+
span.set_attribute("environment", environment)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def instrument_litellm():
|
|
77
|
+
"""Instrument litellm to capture token IDs."""
|
|
78
|
+
OpenTelemetry.set_attributes = patched_set_attributes
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def uninstrument_litellm():
|
|
82
|
+
"""Uninstrument litellm to stop capturing token IDs."""
|
|
83
|
+
OpenTelemetry.set_attributes = original_set_attributes
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import warnings
|
|
6
|
+
from typing import Any, List
|
|
7
|
+
|
|
8
|
+
import vllm.entrypoints.openai.protocol
|
|
9
|
+
from vllm.entrypoints.openai.protocol import ChatCompletionResponse
|
|
10
|
+
from vllm.entrypoints.openai.serving_chat import OpenAIServingChat
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"instrument_vllm",
|
|
14
|
+
"uninstrument_vllm",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ChatCompletionResponsePatched(ChatCompletionResponse):
|
|
19
|
+
prompt_token_ids: List[int] | None = None
|
|
20
|
+
response_token_ids: List[int] | None = None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
original_chat_completion_full_generator = OpenAIServingChat.chat_completion_full_generator
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def chat_completion_full_generator(
|
|
27
|
+
self: Any,
|
|
28
|
+
request: Any,
|
|
29
|
+
result_generator: Any,
|
|
30
|
+
request_id: str,
|
|
31
|
+
model_name: str,
|
|
32
|
+
conversation: Any,
|
|
33
|
+
tokenizer: Any,
|
|
34
|
+
request_metadata: Any,
|
|
35
|
+
) -> Any:
|
|
36
|
+
prompt_token_ids: List[int] | None = None
|
|
37
|
+
response_token_ids: List[List[int]] | None = None
|
|
38
|
+
|
|
39
|
+
async def _generate_inceptor():
|
|
40
|
+
nonlocal prompt_token_ids, response_token_ids
|
|
41
|
+
async for res in result_generator:
|
|
42
|
+
yield res
|
|
43
|
+
prompt_token_ids = res.prompt_token_ids
|
|
44
|
+
response_token_ids = [output.token_ids for output in res.outputs]
|
|
45
|
+
|
|
46
|
+
response = await original_chat_completion_full_generator(
|
|
47
|
+
self,
|
|
48
|
+
request,
|
|
49
|
+
_generate_inceptor(),
|
|
50
|
+
request_id,
|
|
51
|
+
model_name,
|
|
52
|
+
conversation,
|
|
53
|
+
tokenizer,
|
|
54
|
+
request_metadata,
|
|
55
|
+
)
|
|
56
|
+
response = response.model_copy(
|
|
57
|
+
update={
|
|
58
|
+
"prompt_token_ids": prompt_token_ids,
|
|
59
|
+
"response_token_ids": response_token_ids,
|
|
60
|
+
}
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
return response
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def instrument_vllm():
|
|
67
|
+
"""Instrument vLLM to capture token IDs generated by engine.
|
|
68
|
+
|
|
69
|
+
This instrumentation has been merged to upstream vLLM since v0.10.2.
|
|
70
|
+
"""
|
|
71
|
+
if vllm.entrypoints.openai.protocol.ChatCompletionResponse is ChatCompletionResponsePatched:
|
|
72
|
+
warnings.warn("vllm is already instrumented. Skip the instrumentation.")
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
vllm.entrypoints.openai.protocol.ChatCompletionResponse = ChatCompletionResponsePatched
|
|
76
|
+
OpenAIServingChat.chat_completion_full_generator = chat_completion_full_generator
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def uninstrument_vllm():
|
|
80
|
+
"""Uninstrument vLLM to stop capturing token IDs generated by engine."""
|
|
81
|
+
OpenAIServingChat.chat_completion_full_generator = original_chat_completion_full_generator
|