-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_llamaindex.py
More file actions
314 lines (217 loc) · 9.4 KB
/
Copy pathtest_llamaindex.py
File metadata and controls
314 lines (217 loc) · 9.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""Tests for the LlamaIndex integration."""
import pytest
from braintrust import logger
from braintrust.integrations.llamaindex import BraintrustSpanHandler, LlamaIndexIntegration
from braintrust.test_helpers import init_test_logger
PROJECT_NAME = "llamaindex-py"
def _find_spans_by_attributes(spans, **attributes):
result = []
for span in spans:
span_attrs = span.get("span_attributes") or {}
if all(span_attrs.get(k) == v for k, v in attributes.items()):
result.append(span)
return result
@pytest.fixture
def logger_memory_logger():
test_logger = init_test_logger(PROJECT_NAME)
with logger._internal_with_memory_background_logger() as bgl:
yield (test_logger, bgl)
@pytest.fixture(autouse=True)
def setup_and_cleanup():
from llama_index.core.instrumentation import get_dispatcher
LlamaIndexIntegration.setup()
yield
dispatcher = get_dispatcher()
dispatcher.span_handlers = [h for h in dispatcher.span_handlers if not isinstance(h, BraintrustSpanHandler)]
def test_integration_setup():
from llama_index.core.instrumentation import get_dispatcher
dispatcher = get_dispatcher()
handler_types = [type(h).__name__ for h in dispatcher.span_handlers]
assert "BraintrustSpanHandler" in handler_types
def test_integration_idempotent():
from llama_index.core.instrumentation import get_dispatcher
LlamaIndexIntegration.setup()
LlamaIndexIntegration.setup()
dispatcher = get_dispatcher()
bt_handlers = [h for h in dispatcher.span_handlers if isinstance(h, BraintrustSpanHandler)]
assert len(bt_handlers) == 1
def test_auto_instrument_includes_llamaindex():
from braintrust.auto import auto_instrument
result = auto_instrument()
assert "llamaindex" in result
assert result["llamaindex"] is True
@pytest.mark.asyncio
async def test_streaming_outputs_are_not_stringified():
from braintrust.integrations.llamaindex.tracing import _extract_response_output
def stream():
yield "chunk"
async def async_stream():
yield "chunk"
async_gen = async_stream()
try:
assert _extract_response_output(stream()) is None
assert _extract_response_output(async_gen) is None
finally:
await async_gen.aclose()
@pytest.mark.asyncio
async def test_coroutine_outputs_are_not_stringified():
from braintrust.integrations.llamaindex.tracing import _extract_response_output
async def coroutine():
return "result"
coro = coroutine()
try:
assert _extract_response_output(coro) is None
finally:
getattr(coro, "close")()
@pytest.mark.vcr
def test_llm_complete(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-4o-mini", temperature=0)
with test_logger.start_span(name="test-complete"):
llm.complete("What is 2+2? Answer with just the number.")
spans = memory_logger.pop()
assert len(spans) >= 2
llm_spans = _find_spans_by_attributes(spans, type="llm")
assert len(llm_spans) >= 1
llm_span = llm_spans[0]
assert llm_span["span_attributes"]["name"] == "OpenAI"
assert llm_span["input"] is not None
assert llm_span["output"] is not None
assert llm_span["metadata"]["class"] == "OpenAI"
assert llm_span["metadata"]["model"] == "gpt-4o-mini"
@pytest.mark.vcr
def test_llm_chat(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.core.base.llms.types import ChatMessage, MessageRole
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-4o-mini", temperature=0)
messages = [
ChatMessage(role=MessageRole.SYSTEM, content="You are a helpful assistant."),
ChatMessage(role=MessageRole.USER, content="What is the capital of France?"),
]
with test_logger.start_span(name="test-chat"):
llm.chat(messages)
spans = memory_logger.pop()
assert len(spans) >= 2
llm_spans = _find_spans_by_attributes(spans, type="llm")
assert len(llm_spans) >= 1
llm_span = llm_spans[0]
assert llm_span["input"] is not None
assert llm_span["output"] is not None
assert isinstance(llm_span["output"], dict)
assert "content" in llm_span["output"] or "role" in llm_span["output"]
def test_document_processing(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter
docs = [
Document(text="Paris is the capital of France. The Eiffel Tower is in Paris."),
Document(text="Berlin is the capital of Germany. The Brandenburg Gate is in Berlin."),
]
splitter = SentenceSplitter(chunk_size=64, chunk_overlap=10)
with test_logger.start_span(name="test-docproc"):
splitter.get_nodes_from_documents(docs)
spans = memory_logger.pop()
assert len(spans) >= 2
func_spans = _find_spans_by_attributes(spans, type="function")
assert len(func_spans) >= 1
assert "SentenceSplitter" in func_spans[0]["span_attributes"]["name"]
@pytest.mark.vcr
def test_embedding(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding(model="text-embedding-3-small")
with test_logger.start_span(name="test-embedding"):
embed_model.get_text_embedding("Hello world")
spans = memory_logger.pop()
assert len(spans) >= 2
func_spans = _find_spans_by_attributes(spans, type="function")
assert len(func_spans) >= 1
assert "OpenAIEmbedding" in func_spans[0]["span_attributes"]["name"]
@pytest.mark.vcr
def test_query_engine(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.core import Document, VectorStoreIndex
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
docs = [
Document(text="The capital of France is Paris. Paris has a population of 2.1 million."),
Document(text="The Eiffel Tower is located in Paris, France. It was built in 1889."),
]
embed_model = OpenAIEmbedding(model="text-embedding-3-small")
llm = OpenAI(model="gpt-4o-mini", temperature=0)
with test_logger.start_span(name="test-query-engine"):
index = VectorStoreIndex.from_documents(docs, embed_model=embed_model)
query_engine = index.as_query_engine(llm=llm)
query_engine.query("What is the capital of France?")
spans = memory_logger.pop()
assert len(spans) >= 4
span_types = {s.get("span_attributes", {}).get("type") for s in spans}
assert "task" in span_types
assert "llm" in span_types or "function" in span_types
def test_span_hierarchy(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter
docs = [Document(text="Hello world. This is a test document with some content.")]
splitter = SentenceSplitter(chunk_size=256, chunk_overlap=10)
with test_logger.start_span(name="test-hierarchy"):
splitter.get_nodes_from_documents(docs)
spans = memory_logger.pop()
assert len(spans) >= 2
root_span_id = spans[0]["root_span_id"]
for span in spans:
assert span["root_span_id"] == root_span_id
@pytest.mark.vcr
def test_llm_error_handling(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-4o-mini", api_key="sk-invalid-key")
with test_logger.start_span(name="test-error"):
try:
llm.complete("Hello")
except Exception:
pass
spans = memory_logger.pop()
assert len(spans) >= 2
llm_spans = _find_spans_by_attributes(spans, type="llm")
assert len(llm_spans) >= 1
assert llm_spans[0].get("error") is not None
@pytest.mark.vcr
@pytest.mark.asyncio
async def test_async_llm_complete(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-4o-mini", temperature=0)
with test_logger.start_span(name="test-async-complete"):
await llm.acomplete("What is 2+2? Answer with just the number.")
spans = memory_logger.pop()
assert len(spans) >= 2
llm_spans = _find_spans_by_attributes(spans, type="llm")
assert len(llm_spans) >= 1
@pytest.mark.vcr
@pytest.mark.asyncio
async def test_async_llm_chat(logger_memory_logger):
test_logger, memory_logger = logger_memory_logger
assert not memory_logger.pop()
from llama_index.core.base.llms.types import ChatMessage, MessageRole
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-4o-mini", temperature=0)
messages = [
ChatMessage(role=MessageRole.USER, content="Say hello"),
]
with test_logger.start_span(name="test-async-chat"):
await llm.achat(messages)
spans = memory_logger.pop()
assert len(spans) >= 2
llm_spans = _find_spans_by_attributes(spans, type="llm")
assert len(llm_spans) >= 1