-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharize_instrument.py
72 lines (56 loc) · 2.62 KB
/
arize_instrument.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# import os
# import arize-otel
# # Import open-telemetry dependencies
# from arize_otel import register_otel, Endpoints
# SPACE_ID = "SPACE_ID" # Change this line
# API_KEY = "API_KEY" # Change this line
# model_id = "tutorial-otlp-tracing-langchain-rag"
# model_version = "1.0"
# if SPACE_ID == "SPACE_ID" or API_KEY == "API_KEY":
# raise ValueError("❌ CHANGE SPACE_ID AND/OR API_KEY")
# else:
# print("✅ Import and Setup Arize Client Done! Now we can start using Arize!")
# # Setup OTEL via our convenience function
# register_otel(
# endpoints = Endpoints.ARIZE,
# space_id = "your-space-id", # in app Space settings page
# api_key = "your-api-key", # in app Space settings page
# model_id = "your-model-id",
# )
# # Import the automatic instrumentor from OpenInference
# from openinference.instrumentation.langchain import LangChainInstrumentor
# # Finish automatic instrumentation
# LangChainInstrumentor().instrument()
import os
from opentelemetry.sdk.resources import Resource
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry import trace as trace_api
from openinference.instrumentation.langchain import LangChainInstrumentor
# import arize-otel
def arize_instrument(space_id, api_key, model_id, model_version):
# Set the Space and API keys as headers
os.environ['OTEL_EXPORTER_OTLP_TRACES_HEADERS'] = f"space_id={space_id},api_key={api_key}"
# Set the model id and version as resource attributes
resource = Resource(
attributes={
"model_id": model_id,
"model_version": model_version,
}
)
endpoint = "https://otlp.arize.com/v1"
otlp_span_exporter = OTLPSpanExporter(endpoint=endpoint)
console_span_exporter = ConsoleSpanExporter()
otlp_span_processor = SimpleSpanProcessor(span_exporter=otlp_span_exporter)
console_span_processor = SimpleSpanProcessor(span_exporter=console_span_exporter)
tracer_provider = trace_sdk.TracerProvider(resource=resource)
tracer_provider.add_span_processor(span_processor=otlp_span_processor)
tracer_provider.add_span_processor(span_processor=console_span_processor)
trace_api.set_tracer_provider(tracer_provider=tracer_provider)
# Instrument LangChain
LangChainInstrumentor().instrument()
print("Arize setup completed successfully.")
if __name__ == "__main__":
# This allows you to run the setup directly if needed
arize_instrument("YOUR_SPACE_ID", "YOUR_API_KEY", "your-model-id", "your-model-version")