cozeloop 0.1.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.
- cozeloop/__init__.py +33 -0
- cozeloop/_client.py +315 -0
- cozeloop/_noop.py +52 -0
- cozeloop/attribute/__init__.py +3 -0
- cozeloop/attribute/trace/__init__.py +3 -0
- cozeloop/attribute/trace/model.py +105 -0
- cozeloop/attribute/trace/prompt.py +22 -0
- cozeloop/attribute/trace/retriever.py +23 -0
- cozeloop/attribute/trace/runtime.py +14 -0
- cozeloop/attribute/trace/span_key.py +38 -0
- cozeloop/attribute/trace/span_value.py +38 -0
- cozeloop/client.py +28 -0
- cozeloop/entities/__init__.py +9 -0
- cozeloop/entities/prompt.py +89 -0
- cozeloop/integration/__init__.py +3 -0
- cozeloop/integration/langchain/__init__.py +3 -0
- cozeloop/integration/langchain/trace_callback.py +444 -0
- cozeloop/integration/langchain/trace_model/__init__.py +32 -0
- cozeloop/integration/langchain/trace_model/llm_model.py +175 -0
- cozeloop/integration/langchain/trace_model/prompt_template.py +57 -0
- cozeloop/integration/langchain/trace_model/runtime.py +41 -0
- cozeloop/integration/langchain/util.py +61 -0
- cozeloop/internal/__init__.py +5 -0
- cozeloop/internal/consts/__init__.py +120 -0
- cozeloop/internal/consts/error.py +138 -0
- cozeloop/internal/httpclient/__init__.py +8 -0
- cozeloop/internal/httpclient/auth.py +90 -0
- cozeloop/internal/httpclient/auth_client.py +207 -0
- cozeloop/internal/httpclient/base_model.py +9 -0
- cozeloop/internal/httpclient/client.py +120 -0
- cozeloop/internal/httpclient/http_client.py +58 -0
- cozeloop/internal/httpclient/user_agent.py +38 -0
- cozeloop/internal/idgen/__init__.py +8 -0
- cozeloop/internal/idgen/idgen.py +72 -0
- cozeloop/internal/prompt/__init__.py +4 -0
- cozeloop/internal/prompt/cache.py +118 -0
- cozeloop/internal/prompt/converter.py +180 -0
- cozeloop/internal/prompt/openapi.py +142 -0
- cozeloop/internal/prompt/prompt.py +253 -0
- cozeloop/internal/trace/__init__.py +9 -0
- cozeloop/internal/trace/exporter.py +405 -0
- cozeloop/internal/trace/model/__init__.py +3 -0
- cozeloop/internal/trace/model/model.py +25 -0
- cozeloop/internal/trace/noop_span.py +119 -0
- cozeloop/internal/trace/queue_manager.py +156 -0
- cozeloop/internal/trace/span.py +670 -0
- cozeloop/internal/trace/span_processor.py +140 -0
- cozeloop/internal/trace/trace.py +131 -0
- cozeloop/internal/utils/__init__.py +19 -0
- cozeloop/internal/utils/convert.py +111 -0
- cozeloop/internal/utils/get.py +24 -0
- cozeloop/internal/utils/goroutine.py +20 -0
- cozeloop/internal/utils/validation.py +35 -0
- cozeloop/internal/version.py +4 -0
- cozeloop/logger.py +34 -0
- cozeloop/prompt.py +37 -0
- cozeloop/span.py +220 -0
- cozeloop/trace.py +48 -0
- cozeloop-0.1.0.dist-info/LICENSE +21 -0
- cozeloop-0.1.0.dist-info/METADATA +97 -0
- cozeloop-0.1.0.dist-info/RECORD +62 -0
- cozeloop-0.1.0.dist-info/WHEEL +4 -0
cozeloop/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from .entities.prompt import Prompt, Message
|
|
5
|
+
import cozeloop.entities
|
|
6
|
+
from .logger import set_log_level, add_log_handler
|
|
7
|
+
|
|
8
|
+
from .internal import __version__
|
|
9
|
+
from .internal.consts import (
|
|
10
|
+
CN_BASE_URL,
|
|
11
|
+
)
|
|
12
|
+
from .internal.consts.error import *
|
|
13
|
+
|
|
14
|
+
from .client import Client
|
|
15
|
+
from ._client import (
|
|
16
|
+
new_client,
|
|
17
|
+
workspace_id,
|
|
18
|
+
close,
|
|
19
|
+
get_prompt,
|
|
20
|
+
prompt_format,
|
|
21
|
+
start_span,
|
|
22
|
+
get_span_from_context,
|
|
23
|
+
get_span_from_header,
|
|
24
|
+
flush,
|
|
25
|
+
ENV_API_BASE_URL,
|
|
26
|
+
ENV_WORKSPACE_ID,
|
|
27
|
+
ENV_API_TOKEN,
|
|
28
|
+
ENV_JWT_OAUTH_CLIENT_ID,
|
|
29
|
+
ENV_JWT_OAUTH_PRIVATE_KEY,
|
|
30
|
+
ENV_JWT_OAUTH_PUBLIC_KEY_ID
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from .span import SpanContext, Span
|
cozeloop/_client.py
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import atexit
|
|
5
|
+
import hashlib
|
|
6
|
+
import logging
|
|
7
|
+
import os
|
|
8
|
+
import threading
|
|
9
|
+
from datetime import datetime
|
|
10
|
+
from typing import Dict, Any, List, Optional
|
|
11
|
+
|
|
12
|
+
from cozeloop.client import Client
|
|
13
|
+
from cozeloop._noop import NOOP_SPAN, _NoopClient
|
|
14
|
+
from cozeloop.entities.prompt import Prompt, Message, PromptVariable
|
|
15
|
+
from cozeloop.internal import consts, httpclient
|
|
16
|
+
from cozeloop.internal.consts import ClientClosedError
|
|
17
|
+
from cozeloop.internal.httpclient import Auth
|
|
18
|
+
from cozeloop.internal.prompt import PromptProvider
|
|
19
|
+
from cozeloop.internal.trace import TraceProvider
|
|
20
|
+
from cozeloop.span import SpanContext, Span
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
# environment keys for loop client
|
|
25
|
+
ENV_API_BASE_URL = "COZELOOP_API_BASE_URL"
|
|
26
|
+
ENV_WORKSPACE_ID = "COZELOOP_WORKSPACE_ID"
|
|
27
|
+
ENV_API_TOKEN = "COZELOOP_API_TOKEN"
|
|
28
|
+
ENV_JWT_OAUTH_CLIENT_ID = "COZELOOP_JWT_OAUTH_CLIENT_ID"
|
|
29
|
+
ENV_JWT_OAUTH_PRIVATE_KEY = "COZELOOP_JWT_OAUTH_PRIVATE_KEY"
|
|
30
|
+
ENV_JWT_OAUTH_PUBLIC_KEY_ID = "COZELOOP_JWT_OAUTH_PUBLIC_KEY_ID"
|
|
31
|
+
|
|
32
|
+
_client_cache = {}
|
|
33
|
+
_cache_lock = threading.Lock()
|
|
34
|
+
|
|
35
|
+
_default_client = None
|
|
36
|
+
_client_lock = threading.Lock()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _generate_cache_key(*args) -> str:
|
|
40
|
+
key_str = "\t".join(str(arg) for arg in args)
|
|
41
|
+
return hashlib.md5(key_str.encode('utf-8')).hexdigest()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def new_client(
|
|
45
|
+
api_base_url: str = "",
|
|
46
|
+
workspace_id: str = "",
|
|
47
|
+
api_token: str = "",
|
|
48
|
+
jwt_oauth_client_id: str = "",
|
|
49
|
+
jwt_oauth_private_key: str = "",
|
|
50
|
+
jwt_oauth_public_key_id: str = "",
|
|
51
|
+
timeout: int = consts.DEFAULT_TIMEOUT,
|
|
52
|
+
upload_timeout: int = consts.DEFAULT_UPLOAD_TIMEOUT,
|
|
53
|
+
ultra_large_report: bool = False,
|
|
54
|
+
prompt_cache_max_count: int = consts.DEFAULT_PROMPT_CACHE_MAX_COUNT,
|
|
55
|
+
prompt_cache_refresh_interval: int = consts.DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL,
|
|
56
|
+
prompt_trace: bool = False,
|
|
57
|
+
) -> Client:
|
|
58
|
+
cache_key = _generate_cache_key(
|
|
59
|
+
api_base_url,
|
|
60
|
+
workspace_id,
|
|
61
|
+
api_token,
|
|
62
|
+
jwt_oauth_client_id,
|
|
63
|
+
jwt_oauth_private_key,
|
|
64
|
+
jwt_oauth_public_key_id,
|
|
65
|
+
timeout,
|
|
66
|
+
upload_timeout,
|
|
67
|
+
ultra_large_report,
|
|
68
|
+
prompt_cache_max_count,
|
|
69
|
+
prompt_cache_refresh_interval,
|
|
70
|
+
prompt_trace
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
with _cache_lock:
|
|
74
|
+
if cache_key in _client_cache:
|
|
75
|
+
logger.warning("You shouldn't creating a client with same options repeatedly, " +
|
|
76
|
+
"return the cached client instead.")
|
|
77
|
+
return _client_cache[cache_key]
|
|
78
|
+
client = _LoopClient(
|
|
79
|
+
api_base_url=api_base_url,
|
|
80
|
+
workspace_id=workspace_id,
|
|
81
|
+
api_token=api_token,
|
|
82
|
+
jwt_oauth_client_id=jwt_oauth_client_id,
|
|
83
|
+
jwt_oauth_private_key=jwt_oauth_private_key,
|
|
84
|
+
jwt_oauth_public_key_id=jwt_oauth_public_key_id,
|
|
85
|
+
timeout=timeout,
|
|
86
|
+
upload_timeout=upload_timeout,
|
|
87
|
+
ultra_large_report=ultra_large_report,
|
|
88
|
+
prompt_cache_max_count=prompt_cache_max_count,
|
|
89
|
+
prompt_cache_refresh_interval=prompt_cache_refresh_interval,
|
|
90
|
+
prompt_trace=prompt_trace,
|
|
91
|
+
)
|
|
92
|
+
_client_cache[cache_key] = client
|
|
93
|
+
return client
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class _LoopClient(Client):
|
|
97
|
+
_workspace_id: str
|
|
98
|
+
_trace_provider: TraceProvider
|
|
99
|
+
_prompt_provider: PromptProvider
|
|
100
|
+
|
|
101
|
+
_closed: bool = False
|
|
102
|
+
|
|
103
|
+
def __init__(
|
|
104
|
+
self,
|
|
105
|
+
api_base_url: str = "",
|
|
106
|
+
workspace_id: str = "",
|
|
107
|
+
api_token: str = "",
|
|
108
|
+
jwt_oauth_client_id: str = "",
|
|
109
|
+
jwt_oauth_private_key: str = "",
|
|
110
|
+
jwt_oauth_public_key_id: str = "",
|
|
111
|
+
timeout: int = consts.DEFAULT_TIMEOUT,
|
|
112
|
+
upload_timeout: int = consts.DEFAULT_UPLOAD_TIMEOUT,
|
|
113
|
+
ultra_large_report: bool = False,
|
|
114
|
+
prompt_cache_max_count: int = consts.DEFAULT_PROMPT_CACHE_MAX_COUNT,
|
|
115
|
+
prompt_cache_refresh_interval: int = consts.DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL,
|
|
116
|
+
prompt_trace: bool = False
|
|
117
|
+
):
|
|
118
|
+
workspace_id = self._get_from_env(workspace_id, ENV_WORKSPACE_ID)
|
|
119
|
+
api_base_url = self._get_from_env(api_base_url, ENV_API_BASE_URL)
|
|
120
|
+
api_token = self._get_from_env(api_token, ENV_API_TOKEN)
|
|
121
|
+
jwt_oauth_client_id = self._get_from_env(jwt_oauth_client_id, ENV_JWT_OAUTH_CLIENT_ID)
|
|
122
|
+
jwt_oauth_private_key = self._get_from_env(jwt_oauth_private_key, ENV_JWT_OAUTH_PRIVATE_KEY)
|
|
123
|
+
jwt_oauth_public_key_id = self._get_from_env(jwt_oauth_public_key_id, ENV_JWT_OAUTH_PUBLIC_KEY_ID)
|
|
124
|
+
|
|
125
|
+
api_base_url = consts.CN_BASE_URL if not api_base_url else api_base_url
|
|
126
|
+
api_base_url = api_base_url.strip().rstrip("/")
|
|
127
|
+
|
|
128
|
+
if not api_base_url:
|
|
129
|
+
raise consts.InvalidParamError("api_base_url is required.")
|
|
130
|
+
if not workspace_id:
|
|
131
|
+
raise consts.InvalidParamError("workspace_id is required.")
|
|
132
|
+
if prompt_cache_max_count <= 0:
|
|
133
|
+
prompt_cache_max_count = consts.DEFAULT_PROMPT_CACHE_MAX_COUNT
|
|
134
|
+
if prompt_cache_refresh_interval <= 0:
|
|
135
|
+
prompt_cache_refresh_interval = consts.DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL
|
|
136
|
+
|
|
137
|
+
self._workspace_id = workspace_id
|
|
138
|
+
inner_client = httpclient.HTTPClient()
|
|
139
|
+
auth = self._build_auth(
|
|
140
|
+
api_base_url=api_base_url,
|
|
141
|
+
http_client=inner_client,
|
|
142
|
+
api_token=api_token,
|
|
143
|
+
jwt_oauth_client_id=jwt_oauth_client_id,
|
|
144
|
+
jwt_oauth_private_key=jwt_oauth_private_key,
|
|
145
|
+
jwt_oauth_public_key_id=jwt_oauth_public_key_id
|
|
146
|
+
)
|
|
147
|
+
http_client = httpclient.Client(
|
|
148
|
+
api_base_url=api_base_url,
|
|
149
|
+
http_client=inner_client,
|
|
150
|
+
auth=auth,
|
|
151
|
+
timeout=timeout,
|
|
152
|
+
upload_timeout=upload_timeout
|
|
153
|
+
)
|
|
154
|
+
self._trace_provider = TraceProvider(
|
|
155
|
+
http_client=http_client,
|
|
156
|
+
workspace_id=workspace_id,
|
|
157
|
+
ultra_large_report=ultra_large_report
|
|
158
|
+
)
|
|
159
|
+
self._prompt_provider = PromptProvider(
|
|
160
|
+
workspace_id=workspace_id,
|
|
161
|
+
http_client=http_client,
|
|
162
|
+
trace_provider=self._trace_provider,
|
|
163
|
+
prompt_cache_max_count=prompt_cache_max_count,
|
|
164
|
+
prompt_cache_refresh_interval=prompt_cache_refresh_interval,
|
|
165
|
+
prompt_trace=prompt_trace
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
def _get_from_env(self, val: str, env_key: str) -> str:
|
|
169
|
+
if val:
|
|
170
|
+
return val
|
|
171
|
+
env_val = os.environ.get(env_key)
|
|
172
|
+
if env_val:
|
|
173
|
+
return env_val
|
|
174
|
+
else:
|
|
175
|
+
return ""
|
|
176
|
+
|
|
177
|
+
def _build_auth(
|
|
178
|
+
self,
|
|
179
|
+
api_base_url: str,
|
|
180
|
+
http_client: httpclient.HTTPClient,
|
|
181
|
+
api_token: str,
|
|
182
|
+
jwt_oauth_client_id: str,
|
|
183
|
+
jwt_oauth_private_key: str,
|
|
184
|
+
jwt_oauth_public_key_id: str
|
|
185
|
+
) -> Auth:
|
|
186
|
+
if jwt_oauth_client_id and jwt_oauth_private_key and jwt_oauth_public_key_id:
|
|
187
|
+
return httpclient.JWTAuth(
|
|
188
|
+
client_id=jwt_oauth_client_id,
|
|
189
|
+
private_key=jwt_oauth_private_key,
|
|
190
|
+
public_key_id=jwt_oauth_public_key_id,
|
|
191
|
+
base_url=api_base_url,
|
|
192
|
+
http_client=http_client
|
|
193
|
+
)
|
|
194
|
+
if api_token:
|
|
195
|
+
return httpclient.TokenAuth(api_token)
|
|
196
|
+
raise consts.AuthInfoRequiredError
|
|
197
|
+
|
|
198
|
+
def workspace_id(self) -> str:
|
|
199
|
+
# Return the space ID
|
|
200
|
+
return self._workspace_id
|
|
201
|
+
|
|
202
|
+
def close(self):
|
|
203
|
+
if self._closed:
|
|
204
|
+
return
|
|
205
|
+
# Perform cleanup by flushing and closing the trace client
|
|
206
|
+
self._trace_provider.close_trace()
|
|
207
|
+
self._closed = True
|
|
208
|
+
|
|
209
|
+
def get_prompt(self, prompt_key: str, version: str = '') -> Optional[Prompt]:
|
|
210
|
+
if self._closed:
|
|
211
|
+
raise ClientClosedError()
|
|
212
|
+
return self._prompt_provider.get_prompt(prompt_key, version)
|
|
213
|
+
|
|
214
|
+
def prompt_format(self, prompt: Prompt, variables: Dict[str, PromptVariable]) -> List[Message]:
|
|
215
|
+
if self._closed:
|
|
216
|
+
raise ClientClosedError()
|
|
217
|
+
return self._prompt_provider.prompt_format(prompt, variables)
|
|
218
|
+
|
|
219
|
+
def start_span(
|
|
220
|
+
self,
|
|
221
|
+
name: str,
|
|
222
|
+
span_type: str,
|
|
223
|
+
*,
|
|
224
|
+
start_time: Optional[datetime] = None,
|
|
225
|
+
child_of: Optional[SpanContext] = None,
|
|
226
|
+
start_new_trace: bool = False,
|
|
227
|
+
) -> Span:
|
|
228
|
+
if self._closed:
|
|
229
|
+
return NOOP_SPAN
|
|
230
|
+
try:
|
|
231
|
+
if child_of is None:
|
|
232
|
+
return self._trace_provider.start_span(name=name, span_type=span_type, start_time=start_time,
|
|
233
|
+
start_new_trace=start_new_trace)
|
|
234
|
+
else:
|
|
235
|
+
return self._trace_provider.start_span(name=name, span_type=span_type, start_time=start_time,
|
|
236
|
+
parent_span_id=child_of.span_id, trace_id=child_of.trace_id,
|
|
237
|
+
baggage=child_of.baggage, start_new_trace=start_new_trace)
|
|
238
|
+
except Exception as e:
|
|
239
|
+
logger.warning(f"Start span failed, returning noop span. Error: {e}")
|
|
240
|
+
return NOOP_SPAN
|
|
241
|
+
|
|
242
|
+
def get_span_from_context(self) -> Span:
|
|
243
|
+
if self._closed:
|
|
244
|
+
return NOOP_SPAN
|
|
245
|
+
span = self._trace_provider.get_span_from_context()
|
|
246
|
+
if span is None:
|
|
247
|
+
return NOOP_SPAN
|
|
248
|
+
return span
|
|
249
|
+
|
|
250
|
+
def get_span_from_header(self, header: dict) -> SpanContext:
|
|
251
|
+
if self._closed:
|
|
252
|
+
return NOOP_SPAN
|
|
253
|
+
return self._trace_provider.get_span_from_header(header)
|
|
254
|
+
|
|
255
|
+
def flush(self):
|
|
256
|
+
if self._closed:
|
|
257
|
+
return
|
|
258
|
+
self._trace_provider.flush()
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def get_default_client() -> Client:
|
|
262
|
+
global _default_client
|
|
263
|
+
if _default_client is None:
|
|
264
|
+
with _client_lock:
|
|
265
|
+
if _default_client is None:
|
|
266
|
+
try:
|
|
267
|
+
_default_client = new_client()
|
|
268
|
+
atexit.register(_graceful_shutdown)
|
|
269
|
+
except Exception as e:
|
|
270
|
+
new_exception = e
|
|
271
|
+
_default_client = _NoopClient(new_exception)
|
|
272
|
+
return _default_client
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _graceful_shutdown():
|
|
276
|
+
global _default_client
|
|
277
|
+
with _client_lock:
|
|
278
|
+
if _default_client is not None:
|
|
279
|
+
logger.info("Starting graceful shutdown...")
|
|
280
|
+
_default_client.close()
|
|
281
|
+
_default_client = _NoopClient(ClientClosedError())
|
|
282
|
+
logger.info("Graceful shutdown finished.")
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def workspace_id() -> str:
|
|
286
|
+
return get_default_client().workspace_id
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def close():
|
|
290
|
+
return get_default_client().close()
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def get_prompt(prompt_key: str, version: str = '') -> Prompt:
|
|
294
|
+
return get_default_client().get_prompt(prompt_key, version)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def prompt_format(prompt: Prompt, variables: Dict[str, Any]) -> List[Message]:
|
|
298
|
+
return get_default_client().prompt_format(prompt, variables)
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def start_span(name: str, span_type: str, *, start_time: Optional[int] = None,
|
|
302
|
+
child_of: Optional[SpanContext] = None) -> Span:
|
|
303
|
+
return get_default_client().start_span(name, span_type, start_time=start_time, child_of=child_of)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def get_span_from_context() -> Span:
|
|
307
|
+
return get_default_client().get_span_from_context()
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def get_span_from_header(header: Dict[str, str]) -> SpanContext:
|
|
311
|
+
return get_default_client().get_span_from_header(header)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def flush() -> None:
|
|
315
|
+
return get_default_client().flush()
|
cozeloop/_noop.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
from typing import Dict, Optional, List
|
|
6
|
+
|
|
7
|
+
from cozeloop.client import Client
|
|
8
|
+
from cozeloop.entities.prompt import Prompt, Message, PromptVariable
|
|
9
|
+
from cozeloop.internal.trace.noop_span import NoopSpan
|
|
10
|
+
from cozeloop.span import SpanContext, Span
|
|
11
|
+
|
|
12
|
+
NOOP_SPAN = NoopSpan()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class _NoopClient(Client):
|
|
19
|
+
def __init__(self, e: Exception):
|
|
20
|
+
self.new_exception = e
|
|
21
|
+
|
|
22
|
+
@property
|
|
23
|
+
def workspace_id(self) -> str:
|
|
24
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
25
|
+
return ""
|
|
26
|
+
|
|
27
|
+
def close(self):
|
|
28
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
29
|
+
|
|
30
|
+
def get_prompt(self, prompt_key: str, version: str = '') -> Optional[Prompt]:
|
|
31
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
32
|
+
raise self.new_exception
|
|
33
|
+
|
|
34
|
+
def prompt_format(self, prompt: Prompt, variables: Dict[str, PromptVariable]) -> List[Message]:
|
|
35
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
36
|
+
raise self.new_exception
|
|
37
|
+
|
|
38
|
+
def start_span(self, name: str, span_type: str, *, start_time: Optional[int] = None,
|
|
39
|
+
child_of: Optional[SpanContext] = None, start_new_trace: bool = False) -> Span:
|
|
40
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
41
|
+
return NOOP_SPAN
|
|
42
|
+
|
|
43
|
+
def get_span_from_context(self) -> Span:
|
|
44
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
45
|
+
return NOOP_SPAN
|
|
46
|
+
|
|
47
|
+
def get_span_from_header(self, header: Dict[str, str]) -> SpanContext:
|
|
48
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
49
|
+
return NOOP_SPAN
|
|
50
|
+
|
|
51
|
+
def flush(self) -> None:
|
|
52
|
+
logger.warning(f"Noop client not supported. {self.new_exception}")
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from typing import List, Optional, Dict, Any
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel
|
|
8
|
+
|
|
9
|
+
# ModelInput is the input for model span, for tag key: input
|
|
10
|
+
class ModelInput(BaseModel):
|
|
11
|
+
messages: Optional[List['ModelMessage']] = None
|
|
12
|
+
tools: Optional[List['ModelTool']] = None
|
|
13
|
+
model_tool_choice: Optional['ModelToolChoice'] = None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# ModelOutput is the output for model span, for tag key: output
|
|
17
|
+
class ModelOutput(BaseModel):
|
|
18
|
+
choices: List['ModelChoice'] = []
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# ModelCallOption is the option for model span, for tag key: call_options
|
|
22
|
+
class ModelCallOption(BaseModel):
|
|
23
|
+
temperature: float = 0.0
|
|
24
|
+
max_tokens: Optional[int] = None
|
|
25
|
+
stop: Optional[List[str]] = None
|
|
26
|
+
top_p: Optional[float] = None
|
|
27
|
+
n: Optional[int] = None
|
|
28
|
+
top_k: Optional[int] = None
|
|
29
|
+
presence_penalty: Optional[float] = None
|
|
30
|
+
frequency_penalty: Optional[float] = None
|
|
31
|
+
reasoning_effort: Optional[str] = None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ModelMessage(BaseModel):
|
|
35
|
+
role: str = "" # from enum VRole in span_value
|
|
36
|
+
content: Optional[str] = None # single content
|
|
37
|
+
reasoning_content: Optional[str] = None # only for output
|
|
38
|
+
parts: Optional[List['ModelMessagePart']] = None # multi-modality content
|
|
39
|
+
name: Optional[str] = None
|
|
40
|
+
tool_calls: Optional[List['ModelToolCall']] = None
|
|
41
|
+
tool_call_id: Optional[str] = None
|
|
42
|
+
metadata: Optional[Dict[str, str]] = None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ModelMessagePartType(str, Enum):
|
|
46
|
+
TEXT = "text"
|
|
47
|
+
IMAGE = "image_url"
|
|
48
|
+
FILE = "file_url"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class ModelMessagePart(BaseModel):
|
|
52
|
+
type: ModelMessagePartType
|
|
53
|
+
text: Optional[str] = None
|
|
54
|
+
image_url: Optional['ModelImageURL'] = None
|
|
55
|
+
file_url: Optional['ModelFileURL'] = None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ModelImageURL(BaseModel):
|
|
59
|
+
name: Optional[str] = None
|
|
60
|
+
# Required. You can enter a valid image URL or MDN Base64 data of an image().
|
|
61
|
+
# MDN: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data#syntax
|
|
62
|
+
url: str = ""
|
|
63
|
+
detail: Optional[str] = None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class ModelFileURL(BaseModel):
|
|
67
|
+
name: Optional[str] = None
|
|
68
|
+
# Required. You can enter a valid file URL or MDN Base64 data of file.
|
|
69
|
+
# MDN: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data#syntax
|
|
70
|
+
url: str = ""
|
|
71
|
+
detail: Optional[str] = None
|
|
72
|
+
suffix: Optional[str] = None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class ModelToolCall(BaseModel):
|
|
76
|
+
id: Optional[str] = None
|
|
77
|
+
type: Optional[str] = None
|
|
78
|
+
function: Optional['ModelToolCallFunction'] = None
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ModelToolCallFunction(BaseModel):
|
|
82
|
+
name: str = ""
|
|
83
|
+
arguments: Optional[str] = None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class ModelTool(BaseModel):
|
|
87
|
+
type: str = "function"
|
|
88
|
+
function: Optional['ModelToolFunction'] = None
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class ModelToolFunction(BaseModel):
|
|
92
|
+
name: str = ""
|
|
93
|
+
description: str = ""
|
|
94
|
+
parameters: Optional[dict] = None
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class ModelChoice(BaseModel):
|
|
98
|
+
finish_reason: str = ""
|
|
99
|
+
index: int = 0
|
|
100
|
+
message: Optional['ModelMessage'] = None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class ModelToolChoice(BaseModel):
|
|
104
|
+
type: str = "" # from enum VToolChoice in span_value
|
|
105
|
+
function: Optional['ModelToolCallFunction'] = None # field name only.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from typing import List, Optional, Any
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
from cozeloop.attribute.trace.model import ModelMessage
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PromptInput(BaseModel):
|
|
12
|
+
templates: Optional[List['ModelMessage']] = None
|
|
13
|
+
arguments: Optional[List['PromptArgument']] = None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PromptArgument(BaseModel):
|
|
17
|
+
key: str = ""
|
|
18
|
+
value: Optional[Any] = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class PromptOutput(BaseModel):
|
|
22
|
+
prompts: Optional[List['ModelMessage']] = None
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from typing import List, Optional
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
|
|
7
|
+
class RetrieverInput(BaseModel):
|
|
8
|
+
query: Optional[str] = None
|
|
9
|
+
|
|
10
|
+
class RetrieverDocument(BaseModel):
|
|
11
|
+
id: Optional[str] = None
|
|
12
|
+
index: Optional[str] = None
|
|
13
|
+
content: str
|
|
14
|
+
vector: Optional[List[float]] = None
|
|
15
|
+
score: float
|
|
16
|
+
|
|
17
|
+
class RetrieverOutput(BaseModel):
|
|
18
|
+
documents: Optional[List[RetrieverDocument]] = None
|
|
19
|
+
|
|
20
|
+
class RetrieverCallOption(BaseModel):
|
|
21
|
+
top_k: Optional[int] = None
|
|
22
|
+
min_score: Optional[float] = None
|
|
23
|
+
filter: Optional[str] = None
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from typing import Optional
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
|
|
7
|
+
class Runtime(BaseModel):
|
|
8
|
+
language: Optional[str] = None # from enum VLang in span_value.go
|
|
9
|
+
library: Optional[str] = None # integration library, from enum VLib in span_value.go
|
|
10
|
+
scene: Optional[str] = None # usage scene, from enum VScene in span_value.go
|
|
11
|
+
|
|
12
|
+
# Dependency Versions.
|
|
13
|
+
library_version: Optional[str] = None
|
|
14
|
+
loop_sdk_version: Optional[str] = None
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
# Tags for model-type span.
|
|
5
|
+
CALL_OPTIONS = "call_options" # Used to identify option for model, like temperature, etc. Recommend use ModelCallOption class.
|
|
6
|
+
STREAM = "stream" # Used to identify whether it is a streaming output.
|
|
7
|
+
REASONING_TOKENS = "reasoning_tokens" # The token usage during the reasoning process.
|
|
8
|
+
REASONING_DURATION = "reasoning_duration" # The duration during the reasoning process, unit: microseconds.
|
|
9
|
+
|
|
10
|
+
# Tags for retriever-type span
|
|
11
|
+
RETRIEVER_PROVIDER = "retriever_provider" # Data retrieval providers, such as Elasticsearch (ES), VikingDB, etc.
|
|
12
|
+
VIKINGDB_NAME = "vikingdb_name" # When using VikingDB to provide retrieval capabilities, db name.
|
|
13
|
+
VIKINGDB_REGION = "vikingdb_region" # When using VikingDB to provide retrieval capabilities, db region.
|
|
14
|
+
ES_NAME = "es_name" # When using ES to provide retrieval capabilities, es name.
|
|
15
|
+
ES_INDEX = "es_index" # When using ES to provide retrieval capabilities, es index.
|
|
16
|
+
ES_CLUSTER = "es_cluster" # When using ES to provide retrieval capabilities, es cluster.
|
|
17
|
+
|
|
18
|
+
# Tags for prompt-type span.
|
|
19
|
+
PROMPT_PROVIDER = "prompt_provider" # Prompt providers, such as Loop, Langsmith, etc.
|
|
20
|
+
PROMPT_KEY = "prompt_key"
|
|
21
|
+
PROMPT_VERSION = "prompt_version"
|
|
22
|
+
|
|
23
|
+
# Internal experimental field.
|
|
24
|
+
# It is not recommended to use for the time being. Instead, use the corresponding Set method.
|
|
25
|
+
SPAN_TYPE = "span_type"
|
|
26
|
+
INPUT = "input"
|
|
27
|
+
OUTPUT = "output"
|
|
28
|
+
ERROR = "error"
|
|
29
|
+
RUNTIME_ = "runtime"
|
|
30
|
+
|
|
31
|
+
MODEL_PROVIDER = "model_provider"
|
|
32
|
+
MODEL_NAME = "model_name"
|
|
33
|
+
INPUT_TOKENS = "input_tokens"
|
|
34
|
+
OUTPUT_TOKENS = "output_tokens"
|
|
35
|
+
TOKENS = "tokens"
|
|
36
|
+
MODEL_PLATFORM = "model_platform"
|
|
37
|
+
MODEL_IDENTIFICATION = "model_identification"
|
|
38
|
+
TOKEN_USAGE_BACKUP = "token_usage_backup"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
# SpanType tag builtin values
|
|
5
|
+
V_PROMPT_SPAN_TYPE = "prompt"
|
|
6
|
+
V_MODEL_SPAN_TYPE = "model"
|
|
7
|
+
V_RETRIEVER_SPAN_TYPE = "retriever"
|
|
8
|
+
V_TOOL_SPAN_TYPE = "tool"
|
|
9
|
+
|
|
10
|
+
V_ERR_DEFAULT = -1 # Default StatusCode for errors.
|
|
11
|
+
|
|
12
|
+
# Tag values for model messages.
|
|
13
|
+
V_ROLE_USER = "user"
|
|
14
|
+
V_ROLE_SYSTEM = "system"
|
|
15
|
+
V_ROLE_ASSISTANT = "assistant"
|
|
16
|
+
V_ROLE_TOOL = "tool"
|
|
17
|
+
|
|
18
|
+
# VToolChoiceNone Reference: https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages
|
|
19
|
+
V_TOOL_CHOICE_NONE = "none" # Means the model will not call any tool and instead generates a message.
|
|
20
|
+
V_TOOL_CHOICE_AUTO = "auto" # Means the model can pick between generating a message or calling one or more tools.
|
|
21
|
+
V_TOOL_CHOICE_REQUIRED = "required" # Means the model must call one or more tools.
|
|
22
|
+
V_TOOL_CHOICE_FUNCTION = "function" # Forces the model to call that tool.
|
|
23
|
+
|
|
24
|
+
# Tag values for runtime tags.
|
|
25
|
+
V_LANG_GO = "go"
|
|
26
|
+
V_LANG_PYTHON = "python"
|
|
27
|
+
V_LANG_TYPESCRIPT = "ts"
|
|
28
|
+
|
|
29
|
+
V_LIB_EINO = "eino"
|
|
30
|
+
V_LIB_LANGCHAIN = "langchain"
|
|
31
|
+
|
|
32
|
+
V_SCENE_CUSTOM = "custom" # user custom, it has the same meaning as blank.
|
|
33
|
+
V_SCENE_PROMPT_HUB = "prompt_hub" # get_prompt
|
|
34
|
+
V_SCENE_PROMPT_TEMPLATE = "prompt_template" # prompt_template
|
|
35
|
+
|
|
36
|
+
# Tag values for prompt input.
|
|
37
|
+
V_PROMPT_ARG_SOURCE_INPUT = "input"
|
|
38
|
+
V_PROMPT_ARG_SOURCE_PARTIAL = "partial"
|
cozeloop/client.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
|
|
6
|
+
from cozeloop.prompt import PromptClient
|
|
7
|
+
from cozeloop.trace import TraceClient
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Client(PromptClient, TraceClient, ABC):
|
|
11
|
+
"""
|
|
12
|
+
Abstract base class for a thread-safe loop client.
|
|
13
|
+
Do not create multiple instances.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
@property
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def workspace_id(self) -> str:
|
|
19
|
+
"""
|
|
20
|
+
Return the workspace ID.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def close(self):
|
|
25
|
+
"""
|
|
26
|
+
Close the client. Should be called before program exit.
|
|
27
|
+
"""
|
|
28
|
+
|