-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathloki_test.py
258 lines (217 loc) · 8.55 KB
/
loki_test.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
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
import json
import os
from typing import Any
from litellm.types.utils import ModelResponse
import pytest
from langevals import expect
from langevals_langevals.llm_boolean import (
CustomLLMBooleanEvaluator,
CustomLLMBooleanSettings,
)
from litellm import ChatCompletionMessageToolCall, Choices, Message, acompletion
from mcp.types import TextContent, Tool
from mcp import ClientSession
from mcp.client.sse import sse_client
from dotenv import load_dotenv
load_dotenv()
DEFAULT_GRAFANA_URL = "http://localhost:3000"
DEFAULT_MCP_URL = "http://localhost:8000/sse"
models = ["gpt-4o", "claude-3-5-sonnet-20240620"]
pytestmark = pytest.mark.anyio
@pytest.fixture
def mcp_url():
return os.environ.get("MCP_GRAFANA_URL", DEFAULT_MCP_URL)
@pytest.fixture
def grafana_headers():
headers = {
"X-Grafana-URL": os.environ.get("GRAFANA_URL", DEFAULT_GRAFANA_URL),
}
if key := os.environ.get("GRAFANA_API_KEY"):
headers["X-Grafana-API-Key"] = key
return headers
@pytest.fixture
async def mcp_client(mcp_url, grafana_headers):
async with sse_client(mcp_url, headers=grafana_headers) as (
read,
write,
):
async with ClientSession(read, write) as session:
await session.initialize()
yield session
@pytest.mark.parametrize("model", models)
@pytest.mark.flaky(reruns=3)
@pytest.mark.pass_rate(0.6)
async def test_loki_logs_tool(model: str, mcp_client: ClientSession):
tools = await mcp_client.list_tools()
prompt = "Can you list the last 10 log lines from all containers using any available Loki datasource? Give me the raw log lines. Please use only the necessary tools to get this information."
messages: list[Message] = [
Message(role="system", content="You are a helpful assistant."),
Message(role="user", content=prompt),
]
tools = [convert_tool(t) for t in tools.tools]
response = await acompletion(
model=model,
messages=messages,
tools=tools,
)
# Check that there's a datasources tool call.
assert isinstance(response, ModelResponse)
messages.extend(
await assert_and_handle_tool_call(response, mcp_client, "list_datasources")
)
datasources_response = messages[-1].content
datasources_data = json.loads(datasources_response)
assert len(datasources_data) > 0, "Should have at least one datasource"
# Verify Loki datasource exists
loki_datasources = [ds for ds in datasources_data if ds.get("type") == "loki"]
assert len(loki_datasources) > 0, "No Loki datasource found"
print(
f"\nFound Loki datasource: {loki_datasources[0]['name']} (uid: {loki_datasources[0]['uid']})"
)
# Call the LLM including the tool call result.
response = await acompletion(
model=model,
messages=messages,
tools=tools,
)
# Check that there's a loki logstool call.
assert isinstance(response, ModelResponse)
messages.extend(
await assert_and_handle_tool_call(
response,
mcp_client,
"query_loki_logs",
{"datasourceUid": "loki"},
)
)
# Call the LLM including the tool call result.
response = await acompletion(
model=model,
messages=messages,
tools=tools,
)
# Check that the response has some log lines.
content = response.choices[0].message.content
log_lines_checker = CustomLLMBooleanEvaluator(
settings=CustomLLMBooleanSettings(
prompt="Does the response contain specific information that could only come from a Loki datasource? This could be actual log lines with timestamps, container names, or a summary that references specific log data. The response should show evidence of real data rather than generic statements.",
)
)
print("content", content)
expect(input=prompt, output=content).to_pass(log_lines_checker)
@pytest.mark.parametrize("model", models)
@pytest.mark.flaky(reruns=3)
@pytest.mark.pass_rate(0.6)
async def test_loki_container_labels(model: str, mcp_client: ClientSession):
tools = await mcp_client.list_tools()
prompt = "Can you list the values for the label container in any available loki datasource? Please use only the necessary tools to get this information."
messages: list[Message] = [
Message(role="system", content="You are a helpful assistant."),
Message(role="user", content=prompt),
]
tools = [convert_tool(t) for t in tools.tools]
response = await acompletion(
model=model,
messages=messages,
tools=tools,
)
# Check that there's a datasources tool call.
assert isinstance(response, ModelResponse)
messages.extend(
await assert_and_handle_tool_call(response, mcp_client, "list_datasources")
)
datasources_response = messages[-1].content
datasources_data = json.loads(datasources_response)
assert len(datasources_data) > 0, "Should have at least one datasource"
# Verify Loki datasource exists
loki_datasources = [ds for ds in datasources_data if ds.get("type") == "loki"]
assert len(loki_datasources) > 0, "No Loki datasource found"
print(
f"\nFound Loki datasource: {loki_datasources[0]['name']} (uid: {loki_datasources[0]['uid']})"
)
# Call the LLM including the tool call result.
response = await acompletion(
model=model,
messages=messages,
tools=tools,
)
# Check that there's a list_loki_label_values tool call.
assert isinstance(response, ModelResponse)
messages.extend(
await assert_and_handle_tool_call(
response,
mcp_client,
"list_loki_label_values",
{"datasourceUid": "loki", "labelName": "container"},
)
)
# Call the LLM including the tool call result.
response = await acompletion(
model=model,
messages=messages,
tools=tools,
)
# Check that the response provides a meaningful summary of container labels
content = response.choices[0].message.content
label_checker = CustomLLMBooleanEvaluator(
settings=CustomLLMBooleanSettings(
prompt="Does the response provide a clear and organized list of container names found in the logs? It should present the container names in a readable format and may include additional context about their usage.",
)
)
expect(input=prompt, output=content).to_pass(label_checker)
async def assert_and_handle_tool_call(
response: ModelResponse,
mcp_client: ClientSession,
expected_tool: str,
expected_args: dict[str, Any] | None = None,
) -> list[Message]:
messages: list[Message] = []
tool_calls: list[ChatCompletionMessageToolCall] = []
for c in response.choices:
assert isinstance(c, Choices)
tool_calls.extend(c.message.tool_calls or [])
# Add the message to the list of messages.
# We'll need to send these back to the LLM with the tool call result.
messages.append(c.message)
# Check that the expected tool call is in the response.
assert len(tool_calls) == 1
# Call the tool(s) with the requested args.
for tool_call in tool_calls:
assert isinstance(tool_call.function.name, str)
arguments = (
{}
if len(tool_call.function.arguments) == 0
else json.loads(tool_call.function.arguments)
)
assert tool_call.function.name == expected_tool
if expected_args:
for key, value in expected_args.items():
assert key in arguments, (
f"Missing required argument '{key}' in tool call"
)
assert arguments[key] == value, (
f"Argument '{key}' has wrong value. Expected: {value}, Got: {arguments[key]}"
)
print(f"calling tool: {tool_call.function.name}({arguments})")
result = await mcp_client.call_tool(tool_call.function.name, arguments)
# Assume each tool returns a single text content for now
assert len(result.content) == 1
assert isinstance(result.content[0], TextContent)
messages.append(
Message(
role="tool", tool_call_id=tool_call.id, content=result.content[0].text
)
)
return messages
def convert_tool(tool: Tool) -> dict:
return {
"type": "function",
"function": {
"name": tool.name,
"description": tool.description,
"parameters": {
**tool.inputSchema,
"properties": tool.inputSchema.get("properties", {}),
},
},
}