cozeloop 0.1.0__tar.gz
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-0.1.0/LICENSE +21 -0
- cozeloop-0.1.0/PKG-INFO +97 -0
- cozeloop-0.1.0/README.md +72 -0
- cozeloop-0.1.0/cozeloop/__init__.py +33 -0
- cozeloop-0.1.0/cozeloop/_client.py +315 -0
- cozeloop-0.1.0/cozeloop/_noop.py +52 -0
- cozeloop-0.1.0/cozeloop/attribute/__init__.py +3 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/__init__.py +3 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/model.py +105 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/prompt.py +22 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/retriever.py +23 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/runtime.py +14 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/span_key.py +38 -0
- cozeloop-0.1.0/cozeloop/attribute/trace/span_value.py +38 -0
- cozeloop-0.1.0/cozeloop/client.py +28 -0
- cozeloop-0.1.0/cozeloop/entities/__init__.py +9 -0
- cozeloop-0.1.0/cozeloop/entities/prompt.py +89 -0
- cozeloop-0.1.0/cozeloop/integration/__init__.py +3 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/__init__.py +3 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/trace_callback.py +444 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/trace_model/__init__.py +32 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/trace_model/llm_model.py +175 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/trace_model/prompt_template.py +57 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/trace_model/runtime.py +41 -0
- cozeloop-0.1.0/cozeloop/integration/langchain/util.py +61 -0
- cozeloop-0.1.0/cozeloop/internal/__init__.py +5 -0
- cozeloop-0.1.0/cozeloop/internal/consts/__init__.py +120 -0
- cozeloop-0.1.0/cozeloop/internal/consts/error.py +138 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/__init__.py +8 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/auth.py +90 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/auth_client.py +207 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/base_model.py +9 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/client.py +120 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/http_client.py +58 -0
- cozeloop-0.1.0/cozeloop/internal/httpclient/user_agent.py +38 -0
- cozeloop-0.1.0/cozeloop/internal/idgen/__init__.py +8 -0
- cozeloop-0.1.0/cozeloop/internal/idgen/idgen.py +72 -0
- cozeloop-0.1.0/cozeloop/internal/prompt/__init__.py +4 -0
- cozeloop-0.1.0/cozeloop/internal/prompt/cache.py +118 -0
- cozeloop-0.1.0/cozeloop/internal/prompt/converter.py +180 -0
- cozeloop-0.1.0/cozeloop/internal/prompt/openapi.py +142 -0
- cozeloop-0.1.0/cozeloop/internal/prompt/prompt.py +253 -0
- cozeloop-0.1.0/cozeloop/internal/trace/__init__.py +9 -0
- cozeloop-0.1.0/cozeloop/internal/trace/exporter.py +405 -0
- cozeloop-0.1.0/cozeloop/internal/trace/model/__init__.py +3 -0
- cozeloop-0.1.0/cozeloop/internal/trace/model/model.py +25 -0
- cozeloop-0.1.0/cozeloop/internal/trace/noop_span.py +119 -0
- cozeloop-0.1.0/cozeloop/internal/trace/queue_manager.py +156 -0
- cozeloop-0.1.0/cozeloop/internal/trace/span.py +670 -0
- cozeloop-0.1.0/cozeloop/internal/trace/span_processor.py +140 -0
- cozeloop-0.1.0/cozeloop/internal/trace/trace.py +131 -0
- cozeloop-0.1.0/cozeloop/internal/utils/__init__.py +19 -0
- cozeloop-0.1.0/cozeloop/internal/utils/convert.py +111 -0
- cozeloop-0.1.0/cozeloop/internal/utils/get.py +24 -0
- cozeloop-0.1.0/cozeloop/internal/utils/goroutine.py +20 -0
- cozeloop-0.1.0/cozeloop/internal/utils/validation.py +35 -0
- cozeloop-0.1.0/cozeloop/internal/version.py +4 -0
- cozeloop-0.1.0/cozeloop/logger.py +34 -0
- cozeloop-0.1.0/cozeloop/prompt.py +37 -0
- cozeloop-0.1.0/cozeloop/span.py +220 -0
- cozeloop-0.1.0/cozeloop/trace.py +48 -0
- cozeloop-0.1.0/pyproject.toml +24 -0
cozeloop-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cozeloop-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: cozeloop
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: coze loop sdk
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: JiangQi715
|
|
7
|
+
Author-email: jiangqi.rrt@bytedance.com
|
|
8
|
+
Requires-Python: >=3.8,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Requires-Dist: apscheduler (>=3.11.0,<4.0.0)
|
|
18
|
+
Requires-Dist: authlib (>=1.2.0,<2.0.0)
|
|
19
|
+
Requires-Dist: cachetools (>=5.5.2,<6.0.0)
|
|
20
|
+
Requires-Dist: httpx (>=0.28.1,<0.29.0)
|
|
21
|
+
Requires-Dist: jinja2 (>=3.1.6,<4.0.0)
|
|
22
|
+
Requires-Dist: pydantic (>=2.10.6,<3.0.0)
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# Loop Go SDK
|
|
26
|
+
English | [简体中文](README.zh_CN.md)
|
|
27
|
+
|
|
28
|
+
## Overview
|
|
29
|
+
|
|
30
|
+
The Loop SDK is a Python client for interacting with [CozeLoop platform](https://loop.coze.cn).
|
|
31
|
+
Key features:
|
|
32
|
+
- Report trace
|
|
33
|
+
- Get and format prompt
|
|
34
|
+
|
|
35
|
+
## Requirements
|
|
36
|
+
- Python 3.8 or higher
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
`pip install CozeLoop`
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### Initialize
|
|
45
|
+
|
|
46
|
+
To get started, visit https://www.coze.cn/open/oauth/apps and create an OAuth app.
|
|
47
|
+
Then you can get your owner appid, public key and private key.
|
|
48
|
+
|
|
49
|
+
Set your environment variables:
|
|
50
|
+
```bash
|
|
51
|
+
export COZELOOP_WORKSPACE_ID=your workspace id
|
|
52
|
+
export COZELOOP_JWT_OAUTH_CLIENT_ID=your client id
|
|
53
|
+
export COZELOOP_JWT_OAUTH_PRIVATE_KEY=your private key
|
|
54
|
+
export COZELOOP_JWT_OAUTH_PUBLIC_KEY_ID=your public key id
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Report Trace
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
def main():
|
|
61
|
+
span = cozeloop.start_span("root", "custom")
|
|
62
|
+
|
|
63
|
+
span.set_input("Hello")
|
|
64
|
+
span.set_output("World")
|
|
65
|
+
|
|
66
|
+
span.finish()
|
|
67
|
+
|
|
68
|
+
cozeloop.close()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Get Prompt
|
|
72
|
+
```python
|
|
73
|
+
def main():
|
|
74
|
+
prompt = cozeloop.get_prompt(prompt_key="your_prompt_key")
|
|
75
|
+
messages = cozeloop.prompt_format(
|
|
76
|
+
prompt,
|
|
77
|
+
{"var1": "your content"},
|
|
78
|
+
)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
You can see more examples [here](examples).
|
|
82
|
+
|
|
83
|
+
## Contribution
|
|
84
|
+
|
|
85
|
+
Please check [Contributing](CONTRIBUTING.md) for more details.
|
|
86
|
+
|
|
87
|
+
## Security
|
|
88
|
+
|
|
89
|
+
If you discover a potential security issue in this project, or think you may
|
|
90
|
+
have discovered a security issue, we ask that you notify Bytedance Security via our [security center](https://security.bytedance.com/src) or [vulnerability reporting email](sec@bytedance.com).
|
|
91
|
+
|
|
92
|
+
Please do **not** create a public GitHub issue.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
97
|
+
|
cozeloop-0.1.0/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Loop Go SDK
|
|
2
|
+
English | [简体中文](README.zh_CN.md)
|
|
3
|
+
|
|
4
|
+
## Overview
|
|
5
|
+
|
|
6
|
+
The Loop SDK is a Python client for interacting with [CozeLoop platform](https://loop.coze.cn).
|
|
7
|
+
Key features:
|
|
8
|
+
- Report trace
|
|
9
|
+
- Get and format prompt
|
|
10
|
+
|
|
11
|
+
## Requirements
|
|
12
|
+
- Python 3.8 or higher
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
`pip install CozeLoop`
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Initialize
|
|
21
|
+
|
|
22
|
+
To get started, visit https://www.coze.cn/open/oauth/apps and create an OAuth app.
|
|
23
|
+
Then you can get your owner appid, public key and private key.
|
|
24
|
+
|
|
25
|
+
Set your environment variables:
|
|
26
|
+
```bash
|
|
27
|
+
export COZELOOP_WORKSPACE_ID=your workspace id
|
|
28
|
+
export COZELOOP_JWT_OAUTH_CLIENT_ID=your client id
|
|
29
|
+
export COZELOOP_JWT_OAUTH_PRIVATE_KEY=your private key
|
|
30
|
+
export COZELOOP_JWT_OAUTH_PUBLIC_KEY_ID=your public key id
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Report Trace
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
def main():
|
|
37
|
+
span = cozeloop.start_span("root", "custom")
|
|
38
|
+
|
|
39
|
+
span.set_input("Hello")
|
|
40
|
+
span.set_output("World")
|
|
41
|
+
|
|
42
|
+
span.finish()
|
|
43
|
+
|
|
44
|
+
cozeloop.close()
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Get Prompt
|
|
48
|
+
```python
|
|
49
|
+
def main():
|
|
50
|
+
prompt = cozeloop.get_prompt(prompt_key="your_prompt_key")
|
|
51
|
+
messages = cozeloop.prompt_format(
|
|
52
|
+
prompt,
|
|
53
|
+
{"var1": "your content"},
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can see more examples [here](examples).
|
|
58
|
+
|
|
59
|
+
## Contribution
|
|
60
|
+
|
|
61
|
+
Please check [Contributing](CONTRIBUTING.md) for more details.
|
|
62
|
+
|
|
63
|
+
## Security
|
|
64
|
+
|
|
65
|
+
If you discover a potential security issue in this project, or think you may
|
|
66
|
+
have discovered a security issue, we ask that you notify Bytedance Security via our [security center](https://security.bytedance.com/src) or [vulnerability reporting email](sec@bytedance.com).
|
|
67
|
+
|
|
68
|
+
Please do **not** create a public GitHub issue.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -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
|
|
@@ -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()
|
|
@@ -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}")
|