-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconftest.py
More file actions
172 lines (143 loc) · 5.78 KB
/
conftest.py
File metadata and controls
172 lines (143 loc) · 5.78 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
from typing import Generator
from unittest.mock import MagicMock
import pytest
from dotenv import load_dotenv
from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor
from opentelemetry.instrumentation.cohere import CohereInstrumentor
from opentelemetry.instrumentation.groq import GroqInstrumentor
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
from opentelemetry.instrumentation.replicate import ReplicateInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import Tracer
from humanloop.client import Humanloop
from humanloop.otel.exporter import HumanloopSpanExporter
from humanloop.otel.processor import HumanloopSpanProcessor
from tests.custom.types import GetHumanloopClientFn
@pytest.fixture(scope="function")
def opentelemetry_test_provider() -> TracerProvider:
"""Create a test TracerProvider with a resource.
This is similar to the created TracerProvider in the
Humanloop class.
"""
provider = TracerProvider(
resource=Resource.create(
{
"service": "humanloop.sdk",
"environment": "test",
}
)
)
return provider
@pytest.fixture(scope="function")
def test_span(opentelemetry_test_provider: TracerProvider):
exporter = InMemorySpanExporter()
processor = SimpleSpanProcessor(exporter)
opentelemetry_test_provider.add_span_processor(processor)
tracer = opentelemetry_test_provider.get_tracer("test")
return tracer.start_span("test_span")
@pytest.fixture(scope="function")
def opentelemetry_test_configuration(
opentelemetry_test_provider: TracerProvider,
) -> Generator[tuple[Tracer, InMemorySpanExporter], None, None]:
"""Configure OTel backend without HumanloopSpanProcessor.
Spans created by Instrumentors will not be used to enrich
Humanloop Spans.
"""
exporter = InMemorySpanExporter()
processor = SimpleSpanProcessor(exporter)
opentelemetry_test_provider.add_span_processor(processor)
instrumentors: list[BaseInstrumentor] = [
OpenAIInstrumentor(),
AnthropicInstrumentor(),
GroqInstrumentor(),
CohereInstrumentor(),
ReplicateInstrumentor(),
]
for instrumentor in instrumentors:
instrumentor.instrument(tracer_provider=opentelemetry_test_provider)
tracer = opentelemetry_test_provider.get_tracer("test")
# Circumvent configuration procedure
yield tracer, exporter
for instrumentor in instrumentors:
instrumentor.uninstrument()
@pytest.fixture(scope="session")
def get_humanloop_client() -> GetHumanloopClientFn:
load_dotenv()
if not os.getenv("HUMANLOOP_API_KEY"):
pytest.fail("HUMANLOOP_API_KEY is not set for integration tests")
def _get_humanloop_client(use_local_files: bool = False, local_files_directory: str = "humanloop") -> Humanloop:
return Humanloop(
api_key=os.getenv("HUMANLOOP_API_KEY"),
use_local_files=use_local_files,
local_files_directory=local_files_directory,
)
return _get_humanloop_client
@pytest.fixture(scope="function")
def opentelemetry_hl_test_configuration(
opentelemetry_test_provider: TracerProvider,
) -> Generator[tuple[Tracer, InMemorySpanExporter], None, None]:
"""Configure OTel backend with HumanloopSpanProcessor.
Spans created by Instrumentors will be used to enrich
Humanloop Spans.
"""
exporter = InMemorySpanExporter()
processor = HumanloopSpanProcessor(exporter=exporter)
opentelemetry_test_provider.add_span_processor(processor)
instrumentors: list[BaseInstrumentor] = [
OpenAIInstrumentor(),
AnthropicInstrumentor(),
GroqInstrumentor(),
CohereInstrumentor(),
ReplicateInstrumentor(),
AnthropicInstrumentor(),
]
for instrumentor in instrumentors:
instrumentor.instrument(
tracer_provider=opentelemetry_test_provider,
)
tracer = opentelemetry_test_provider.get_tracer("test")
yield tracer, exporter
for instrumentor in instrumentors:
instrumentor.uninstrument()
@pytest.fixture(scope="function")
def hl_test_exporter() -> HumanloopSpanExporter:
"""
Test Exporter where HTTP calls to Humanloop API
are mocked.
"""
client = MagicMock()
exporter = HumanloopSpanExporter(client=client)
return exporter
@pytest.fixture(scope="function")
def opentelemetry_hl_with_exporter_test_configuration(
hl_test_exporter: HumanloopSpanExporter,
opentelemetry_test_provider: TracerProvider,
) -> Generator[tuple[Tracer, HumanloopSpanExporter], None, None]:
"""Configure OTel backend with HumanloopSpanProcessor and
a HumanloopSpanExporter where HTTP calls are mocked.
"""
processor = HumanloopSpanProcessor(exporter=hl_test_exporter)
opentelemetry_test_provider.add_span_processor(processor)
instrumentor = OpenAIInstrumentor()
instrumentor.instrument(tracer_provider=opentelemetry_test_provider)
tracer = opentelemetry_test_provider.get_tracer("test")
yield tracer, hl_test_exporter
instrumentor.uninstrument()
@pytest.fixture(scope="session")
def call_llm_messages() -> list[ChatCompletionMessageParam]:
return [
{
"role": "system",
"content": "You are an assistant on the following topics: greetings in foreign languages.",
},
{
"role": "user",
"content": "Bonjour!",
},
]