lmnr 0.4.66__py3-none-any.whl → 0.5.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.
Files changed (37) hide show
  1. lmnr/__init__.py +30 -0
  2. lmnr/openllmetry_sdk/__init__.py +4 -15
  3. lmnr/openllmetry_sdk/tracing/attributes.py +0 -1
  4. lmnr/openllmetry_sdk/tracing/tracing.py +24 -9
  5. lmnr/sdk/browser/browser_use_otel.py +4 -4
  6. lmnr/sdk/browser/playwright_otel.py +213 -228
  7. lmnr/sdk/browser/pw_utils.py +289 -0
  8. lmnr/sdk/browser/utils.py +18 -53
  9. lmnr/sdk/client/asynchronous/async_client.py +157 -0
  10. lmnr/sdk/client/asynchronous/resources/__init__.py +13 -0
  11. lmnr/sdk/client/asynchronous/resources/agent.py +215 -0
  12. lmnr/sdk/client/asynchronous/resources/base.py +32 -0
  13. lmnr/sdk/client/asynchronous/resources/browser_events.py +40 -0
  14. lmnr/sdk/client/asynchronous/resources/evals.py +64 -0
  15. lmnr/sdk/client/asynchronous/resources/pipeline.py +89 -0
  16. lmnr/sdk/client/asynchronous/resources/semantic_search.py +60 -0
  17. lmnr/sdk/client/synchronous/resources/__init__.py +7 -0
  18. lmnr/sdk/client/synchronous/resources/agent.py +209 -0
  19. lmnr/sdk/client/synchronous/resources/base.py +32 -0
  20. lmnr/sdk/client/synchronous/resources/browser_events.py +40 -0
  21. lmnr/sdk/client/synchronous/resources/evals.py +102 -0
  22. lmnr/sdk/client/synchronous/resources/pipeline.py +89 -0
  23. lmnr/sdk/client/synchronous/resources/semantic_search.py +60 -0
  24. lmnr/sdk/client/synchronous/sync_client.py +170 -0
  25. lmnr/sdk/datasets.py +7 -2
  26. lmnr/sdk/evaluations.py +53 -27
  27. lmnr/sdk/laminar.py +22 -175
  28. lmnr/sdk/types.py +121 -23
  29. lmnr/sdk/utils.py +10 -0
  30. lmnr/version.py +6 -6
  31. {lmnr-0.4.66.dist-info → lmnr-0.5.0.dist-info}/METADATA +88 -38
  32. lmnr-0.5.0.dist-info/RECORD +55 -0
  33. lmnr/sdk/client.py +0 -313
  34. lmnr-0.4.66.dist-info/RECORD +0 -39
  35. {lmnr-0.4.66.dist-info → lmnr-0.5.0.dist-info}/LICENSE +0 -0
  36. {lmnr-0.4.66.dist-info → lmnr-0.5.0.dist-info}/WHEEL +0 -0
  37. {lmnr-0.4.66.dist-info → lmnr-0.5.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,209 @@
1
+ """Agent resource for interacting with Laminar agents."""
2
+
3
+ from typing import Generator, Literal, Optional, Union, overload
4
+ import uuid
5
+
6
+ from lmnr.sdk.client.synchronous.resources.base import BaseResource
7
+ from opentelemetry import trace
8
+
9
+ from lmnr.sdk.types import (
10
+ AgentOutput,
11
+ LaminarSpanContext,
12
+ ModelProvider,
13
+ RunAgentRequest,
14
+ RunAgentResponseChunk,
15
+ )
16
+
17
+
18
+ class Agent(BaseResource):
19
+ """Resource for interacting with Laminar agents."""
20
+
21
+ @overload
22
+ def run(
23
+ self,
24
+ prompt: str,
25
+ stream: Literal[True],
26
+ parent_span_context: Optional[Union[LaminarSpanContext, str]] = None,
27
+ model_provider: Optional[ModelProvider] = None,
28
+ model: Optional[str] = None,
29
+ enable_thinking: bool = True,
30
+ ) -> Generator[RunAgentResponseChunk, None, None]:
31
+ """Run Laminar index agent in streaming mode.
32
+
33
+ Args:
34
+ prompt (str): prompt for the agent
35
+ stream (Literal[True]): whether to stream the agent's response
36
+ parent_span_context (Optional[Union[LaminarSpanContext, str]], optional): span context if the agent is part of a trace
37
+ model_provider (Optional[ModelProvider], optional): LLM model provider
38
+ model (Optional[str], optional): LLM model name
39
+ enable_thinking (bool, optional): whether to enable thinking on the underlying LLM. Default to True.
40
+
41
+ Returns:
42
+ Generator[RunAgentResponseChunk, None, None]: a generator of response chunks
43
+ """
44
+ pass
45
+
46
+ @overload
47
+ def run(
48
+ self,
49
+ prompt: str,
50
+ parent_span_context: Optional[Union[LaminarSpanContext, str]] = None,
51
+ model_provider: Optional[ModelProvider] = None,
52
+ model: Optional[str] = None,
53
+ enable_thinking: bool = True,
54
+ ) -> AgentOutput:
55
+ """Run Laminar index agent.
56
+
57
+ Args:
58
+ prompt (str): prompt for the agent
59
+ parent_span_context (Optional[Union[LaminarSpanContext, str]], optional): span context if the agent is part of a trace
60
+ model_provider (Optional[ModelProvider], optional): LLM model provider
61
+ model (Optional[str], optional): LLM model name
62
+ enable_thinking (bool, optional): whether to enable thinking on the underlying LLM. Default to True.
63
+
64
+ Returns:
65
+ AgentOutput: agent output
66
+ """
67
+ pass
68
+
69
+ @overload
70
+ def run(
71
+ self,
72
+ prompt: str,
73
+ parent_span_context: Optional[Union[LaminarSpanContext, str]] = None,
74
+ model_provider: Optional[ModelProvider] = None,
75
+ model: Optional[str] = None,
76
+ stream: Literal[False] = False,
77
+ enable_thinking: bool = True,
78
+ ) -> AgentOutput:
79
+ """Run Laminar index agent.
80
+
81
+ Args:
82
+ prompt (str): prompt for the agent
83
+ parent_span_context (Optional[Union[LaminarSpanContext, str]], optional): span context if the agent is part of a trace
84
+ model_provider (Optional[ModelProvider], optional): LLM model provider
85
+ model (Optional[str], optional): LLM model name
86
+ stream (Literal[False], optional): whether to stream the agent's response
87
+ enable_thinking (bool, optional): whether to enable thinking on the underlying LLM. Default to True.
88
+ cdp_url (Optional[str], optional): CDP URL to connect to an existing browser session.
89
+
90
+ Returns:
91
+ AgentOutput: agent output
92
+ """
93
+ pass
94
+
95
+ def run(
96
+ self,
97
+ prompt: str,
98
+ parent_span_context: Optional[Union[LaminarSpanContext, str]] = None,
99
+ model_provider: Optional[ModelProvider] = None,
100
+ model: Optional[str] = None,
101
+ stream: bool = False,
102
+ enable_thinking: bool = True,
103
+ ) -> Union[AgentOutput, Generator[RunAgentResponseChunk, None, None]]:
104
+ """Run Laminar index agent.
105
+
106
+ Args:
107
+ prompt (str): prompt for the agent
108
+ parent_span_context (Optional[Union[LaminarSpanContext, str]], optional): span context if the agent is part of a trace
109
+ model_provider (Optional[ModelProvider], optional): LLM model provider
110
+ model (Optional[str], optional): LLM model name
111
+ stream (bool, optional): whether to stream the agent's response
112
+ enable_thinking (bool, optional): whether to enable thinking on the underlying LLM. Default to True.
113
+ cdp_url (Optional[str], optional): CDP URL to connect to an existing browser session.
114
+
115
+ Returns:
116
+ Union[AgentOutput, Generator[RunAgentResponseChunk, None, None]]: agent output or a generator of response chunks
117
+ """
118
+ if parent_span_context is None:
119
+ span = trace.get_current_span()
120
+ if span != trace.INVALID_SPAN:
121
+ parent_span_context = LaminarSpanContext(
122
+ trace_id=uuid.UUID(int=span.get_span_context().trace_id),
123
+ span_id=uuid.UUID(int=span.get_span_context().span_id),
124
+ is_remote=span.get_span_context().is_remote,
125
+ )
126
+ if parent_span_context is not None and isinstance(
127
+ parent_span_context, LaminarSpanContext
128
+ ):
129
+ parent_span_context = str(parent_span_context)
130
+ request = RunAgentRequest(
131
+ prompt=prompt,
132
+ parent_span_context=parent_span_context,
133
+ model_provider=model_provider,
134
+ model=model,
135
+ # We always connect to stream, because our TLS listeners on AWS
136
+ # Network load balancers have a hard fixed idle timeout of 350 seconds.
137
+ # This means that if we don't stream, the connection will be closed.
138
+ # For now, we just return the content of the final chunk if `stream` is
139
+ # `False`.
140
+ # https://aws.amazon.com/blogs/networking-and-content-delivery/introducing-nlb-tcp-configurable-idle-timeout/
141
+ stream=True,
142
+ enable_thinking=enable_thinking,
143
+ )
144
+
145
+ # For streaming case, use a generator function
146
+ if stream:
147
+ return self.__run_streaming(request)
148
+ else:
149
+ # For non-streaming case, process all chunks and return the final result
150
+ return self.__run_non_streaming(request)
151
+
152
+ def __run_streaming(
153
+ self, request: RunAgentRequest
154
+ ) -> Generator[RunAgentResponseChunk, None, None]:
155
+ """Run agent in streaming mode.
156
+
157
+ Args:
158
+ request (RunAgentRequest): The request to run the agent with.
159
+
160
+ Yields:
161
+ RunAgentResponseChunk: Chunks of the agent's response.
162
+ """
163
+ with self._client.stream(
164
+ "POST",
165
+ self._base_url + "/v1/agent/run",
166
+ json=request.to_dict(),
167
+ headers=self._headers(),
168
+ ) as response:
169
+ for line in response.iter_lines():
170
+ line = str(line)
171
+ if line.startswith("[DONE]"):
172
+ break
173
+ if not line.startswith("data: "):
174
+ continue
175
+ line = line[6:]
176
+ if line:
177
+ chunk = RunAgentResponseChunk.model_validate_json(line)
178
+ yield chunk.root
179
+
180
+ def __run_non_streaming(self, request: RunAgentRequest) -> AgentOutput:
181
+ """Run agent in non-streaming mode.
182
+
183
+ Args:
184
+ request (RunAgentRequest): The request to run the agent with.
185
+
186
+ Returns:
187
+ AgentOutput: The agent's output.
188
+ """
189
+ final_chunk = None
190
+
191
+ with self._client.stream(
192
+ "POST",
193
+ self._base_url + "/v1/agent/run",
194
+ json=request.to_dict(),
195
+ headers=self._headers(),
196
+ ) as response:
197
+ for line in response.iter_lines():
198
+ line = str(line)
199
+ if line.startswith("[DONE]"):
200
+ break
201
+ if not line.startswith("data: "):
202
+ continue
203
+ line = line[6:]
204
+ if line:
205
+ chunk = RunAgentResponseChunk.model_validate_json(line)
206
+ if chunk.root.chunkType == "finalOutput":
207
+ final_chunk = chunk.root
208
+
209
+ return final_chunk.content if final_chunk is not None else AgentOutput()
@@ -0,0 +1,32 @@
1
+ """Base class for resource objects."""
2
+
3
+ import httpx
4
+
5
+
6
+ class BaseResource:
7
+ """Base class for all API resources."""
8
+
9
+ def __init__(self, client: httpx.Client, base_url: str, project_api_key: str):
10
+ """Initialize the resource.
11
+
12
+ Args:
13
+ client (httpx.Client): HTTP client instance
14
+ base_url (str): Base URL for the API
15
+ project_api_key (str): Project API key
16
+ """
17
+ self._client = client
18
+ self._base_url = base_url
19
+ self._project_api_key = project_api_key
20
+
21
+ def _headers(self) -> dict[str, str]:
22
+ """Generate request headers with authentication.
23
+
24
+ Returns:
25
+ dict[str, str]: Headers dictionary
26
+ """
27
+ assert self._project_api_key is not None, "Project API key is not set"
28
+ return {
29
+ "Authorization": "Bearer " + self._project_api_key,
30
+ "Content-Type": "application/json",
31
+ "Accept": "application/json",
32
+ }
@@ -0,0 +1,40 @@
1
+ """Resource for sending browser events."""
2
+
3
+ import gzip
4
+ import json
5
+
6
+ from lmnr.sdk.client.synchronous.resources.base import BaseResource
7
+
8
+ from lmnr.version import PYTHON_VERSION, __version__
9
+
10
+
11
+ class BrowserEvents(BaseResource):
12
+ """Resource for sending browser events."""
13
+
14
+ def send(
15
+ self,
16
+ session_id: str,
17
+ trace_id: str,
18
+ events: list[dict],
19
+ ):
20
+ url = self._base_url + "/v1/browser-sessions/events"
21
+ payload = {
22
+ "sessionId": session_id,
23
+ "traceId": trace_id,
24
+ "events": events,
25
+ "source": f"python@{PYTHON_VERSION}",
26
+ "sdkVersion": __version__,
27
+ }
28
+ compressed_payload = gzip.compress(json.dumps(payload).encode("utf-8"))
29
+ response = self._client.post(
30
+ url,
31
+ content=compressed_payload,
32
+ headers={
33
+ **self._headers(),
34
+ "Content-Encoding": "gzip",
35
+ },
36
+ )
37
+ if response.status_code != 200:
38
+ raise ValueError(
39
+ f"Failed to send events: [{response.status_code}] {response.text}"
40
+ )
@@ -0,0 +1,102 @@
1
+ """Evals resource for interacting with Laminar evaluations API."""
2
+
3
+ import uuid
4
+ import urllib.parse
5
+ from typing import Optional
6
+
7
+ from lmnr.sdk.client.synchronous.resources.base import BaseResource
8
+ from lmnr.sdk.types import (
9
+ InitEvaluationResponse,
10
+ EvaluationResultDatapoint,
11
+ GetDatapointsResponse,
12
+ )
13
+
14
+
15
+ class Evals(BaseResource):
16
+ """Resource for interacting with Laminar evaluations API."""
17
+
18
+ def init(
19
+ self, name: Optional[str] = None, group_name: Optional[str] = None
20
+ ) -> InitEvaluationResponse:
21
+ """Initialize a new evaluation.
22
+
23
+ Args:
24
+ name (Optional[str], optional): Name of the evaluation. Defaults to None.
25
+ group_name (Optional[str], optional): Group name for the evaluation. Defaults to None.
26
+
27
+ Returns:
28
+ InitEvaluationResponse: The response from the initialization request.
29
+ """
30
+ response = self._client.post(
31
+ self._base_url + "/v1/evals",
32
+ json={
33
+ "name": name,
34
+ "groupName": group_name,
35
+ },
36
+ headers=self._headers(),
37
+ )
38
+ resp_json = response.json()
39
+ return InitEvaluationResponse.model_validate(resp_json)
40
+
41
+ def save_datapoints(
42
+ self,
43
+ eval_id: uuid.UUID,
44
+ datapoints: list[EvaluationResultDatapoint],
45
+ group_name: Optional[str] = None,
46
+ ):
47
+ """Save evaluation datapoints.
48
+
49
+ Args:
50
+ eval_id (uuid.UUID): The evaluation ID.
51
+ datapoints (list[EvaluationResultDatapoint]): The datapoints to save.
52
+ group_name (Optional[str], optional): Group name for the datapoints. Defaults to None.
53
+
54
+ Raises:
55
+ ValueError: If there's an error saving the datapoints.
56
+ """
57
+ response = self._client.post(
58
+ self._base_url + f"/v1/evals/{eval_id}/datapoints",
59
+ json={
60
+ "points": [datapoint.to_dict() for datapoint in datapoints],
61
+ "groupName": group_name,
62
+ },
63
+ headers=self._headers(),
64
+ )
65
+ if response.status_code != 200:
66
+ raise ValueError(f"Error saving evaluation datapoints: {response.text}")
67
+
68
+ def get_datapoints(
69
+ self,
70
+ dataset_name: str,
71
+ offset: int,
72
+ limit: int,
73
+ ) -> GetDatapointsResponse:
74
+ """Get datapoints from a dataset.
75
+
76
+ Args:
77
+ dataset_name (str): The name of the dataset.
78
+ offset (int): The offset to start from.
79
+ limit (int): The maximum number of datapoints to return.
80
+
81
+ Returns:
82
+ GetDatapointsResponse: The response containing the datapoints.
83
+
84
+ Raises:
85
+ ValueError: If there's an error fetching the datapoints.
86
+ """
87
+ params = {"name": dataset_name, "offset": offset, "limit": limit}
88
+ url = (
89
+ self._base_url + "/v1/datasets/datapoints?" + urllib.parse.urlencode(params)
90
+ )
91
+ response = self._client.get(url, headers=self._headers())
92
+ if response.status_code != 200:
93
+ try:
94
+ resp_json = response.json()
95
+ raise ValueError(
96
+ f"Error fetching datapoints: [{response.status_code}] {resp_json}"
97
+ )
98
+ except Exception:
99
+ raise ValueError(
100
+ f"Error fetching datapoints: [{response.status_code}] {response.text}"
101
+ )
102
+ return GetDatapointsResponse.model_validate(response.json())
@@ -0,0 +1,89 @@
1
+ """Pipeline resource for running Laminar pipelines."""
2
+
3
+ import uuid
4
+ from typing import Optional
5
+ from opentelemetry import trace
6
+
7
+ from lmnr.sdk.client.synchronous.resources.base import BaseResource
8
+ from lmnr.sdk.types import (
9
+ NodeInput,
10
+ PipelineRunError,
11
+ PipelineRunRequest,
12
+ PipelineRunResponse,
13
+ )
14
+
15
+
16
+ class Pipeline(BaseResource):
17
+ """Resource for interacting with Laminar pipelines."""
18
+
19
+ def run(
20
+ self,
21
+ pipeline: str,
22
+ inputs: dict[str, NodeInput],
23
+ env: dict[str, str] = {},
24
+ metadata: dict[str, str] = {},
25
+ parent_span_id: Optional[uuid.UUID] = None,
26
+ trace_id: Optional[uuid.UUID] = None,
27
+ ) -> PipelineRunResponse:
28
+ """Run a pipeline with the given inputs and environment variables.
29
+
30
+ Args:
31
+ pipeline (str): pipeline name
32
+ inputs (dict[str, NodeInput]): input values for the pipeline
33
+ env (dict[str, str], optional): environment variables for the pipeline
34
+ metadata (dict[str, str], optional): metadata for the pipeline run
35
+ parent_span_id (Optional[uuid.UUID], optional): parent span id for the pipeline
36
+ trace_id (Optional[uuid.UUID], optional): trace id for the pipeline
37
+
38
+ Raises:
39
+ ValueError: if the project API key is not set
40
+ PipelineRunError: if the pipeline run fails
41
+
42
+ Returns:
43
+ PipelineRunResponse: response from the pipeline run
44
+ """
45
+ if self._project_api_key is None:
46
+ raise ValueError(
47
+ "Please initialize the Laminar object with your project "
48
+ "API key or set the LMNR_PROJECT_API_KEY environment variable"
49
+ )
50
+
51
+ current_span = trace.get_current_span()
52
+ if current_span != trace.INVALID_SPAN:
53
+ parent_span_id = parent_span_id or uuid.UUID(
54
+ int=current_span.get_span_context().span_id
55
+ )
56
+ trace_id = trace_id or uuid.UUID(
57
+ int=current_span.get_span_context().trace_id
58
+ )
59
+
60
+ request = PipelineRunRequest(
61
+ inputs=inputs,
62
+ pipeline=pipeline,
63
+ env=env or {},
64
+ metadata=metadata,
65
+ parent_span_id=parent_span_id,
66
+ trace_id=trace_id,
67
+ )
68
+
69
+ response = self._client.post(
70
+ self._base_url + "/v1/pipeline/run",
71
+ json=request.to_dict(),
72
+ headers=self._headers(),
73
+ )
74
+
75
+ if response.status_code != 200:
76
+ raise PipelineRunError(response)
77
+
78
+ try:
79
+ from pydantic.alias_generators import to_snake
80
+
81
+ resp_json = response.json()
82
+ keys = list(resp_json.keys())
83
+ for key in keys:
84
+ value = resp_json[key]
85
+ del resp_json[key]
86
+ resp_json[to_snake(key)] = value
87
+ return PipelineRunResponse(**resp_json)
88
+ except Exception:
89
+ raise PipelineRunError(response)
@@ -0,0 +1,60 @@
1
+ """SemanticSearch resource for interacting with Laminar semantic search API."""
2
+
3
+ import uuid
4
+ from typing import Optional
5
+
6
+ from lmnr.sdk.client.synchronous.resources.base import BaseResource
7
+ from lmnr.sdk.types import (
8
+ SemanticSearchRequest,
9
+ SemanticSearchResponse,
10
+ )
11
+
12
+
13
+ class SemanticSearch(BaseResource):
14
+ """Resource for interacting with Laminar semantic search API."""
15
+
16
+ def search(
17
+ self,
18
+ query: str,
19
+ dataset_id: uuid.UUID,
20
+ limit: Optional[int] = None,
21
+ threshold: Optional[float] = None,
22
+ ) -> SemanticSearchResponse:
23
+ """Perform a semantic search on the given dataset.
24
+
25
+ Args:
26
+ query (str): query to search for
27
+ dataset_id (uuid.UUID): dataset ID created in the UI
28
+ limit (Optional[int], optional): maximum number of results to return
29
+ threshold (Optional[float], optional): lowest similarity score to return
30
+
31
+ Raises:
32
+ ValueError: if an error happens while performing the semantic search
33
+
34
+ Returns:
35
+ SemanticSearchResponse: response from the semantic search
36
+ """
37
+ request = SemanticSearchRequest(
38
+ query=query,
39
+ dataset_id=dataset_id,
40
+ limit=limit,
41
+ threshold=threshold,
42
+ )
43
+ response = self._client.post(
44
+ self._base_url + "/v1/semantic-search",
45
+ json=request.to_dict(),
46
+ headers=self._headers(),
47
+ )
48
+ if response.status_code != 200:
49
+ raise ValueError(
50
+ f"Error performing semantic search: [{response.status_code}] {response.text}"
51
+ )
52
+ try:
53
+ resp_json = response.json()
54
+ for result in resp_json["results"]:
55
+ result["dataset_id"] = uuid.UUID(result["datasetId"])
56
+ return SemanticSearchResponse(**resp_json)
57
+ except Exception as e:
58
+ raise ValueError(
59
+ f"Error parsing semantic search response: status={response.status_code} error={e}"
60
+ )