tracdap-ext-openai 0.10.0.dev15__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.
- tracdap_ext_openai-0.10.0.dev15/LICENSE +202 -0
- tracdap_ext_openai-0.10.0.dev15/PKG-INFO +215 -0
- tracdap_ext_openai-0.10.0.dev15/README.md +184 -0
- tracdap_ext_openai-0.10.0.dev15/pyproject.toml +59 -0
- tracdap_ext_openai-0.10.0.dev15/requirements.txt +3 -0
- tracdap_ext_openai-0.10.0.dev15/setup.cfg +4 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap/ext/openai/__init__.py +16 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap/ext/openai/openai_plugin.py +261 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap_ext_openai.egg-info/PKG-INFO +215 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap_ext_openai.egg-info/SOURCES.txt +11 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap_ext_openai.egg-info/dependency_links.txt +1 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap_ext_openai.egg-info/requires.txt +2 -0
- tracdap_ext_openai-0.10.0.dev15/src/tracdap_ext_openai.egg-info/top_level.txt +1 -0
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
@@ -0,0 +1,215 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: tracdap-ext-openai
|
3
|
+
Version: 0.10.0.dev15
|
4
|
+
Summary: An extension for TRAC D.A.P. that lets models connect to OpenAI endpoints
|
5
|
+
Author-email: Martin Traverse <martin@fintrac.co.uk>
|
6
|
+
License-Expression: Apache-2.0
|
7
|
+
Project-URL: Homepage, https://tracdap.finos.org/
|
8
|
+
Project-URL: Documentation, https://tracdap.readthedocs.io/
|
9
|
+
Project-URL: Source Code, https://github.com/finos/tracdap
|
10
|
+
Project-URL: Issue Tracker, https://github.com/finos/tracdap/issues
|
11
|
+
Classifier: Operating System :: OS Independent
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
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
|
+
Classifier: Intended Audience :: Developers
|
18
|
+
Classifier: Intended Audience :: End Users/Desktop
|
19
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
20
|
+
Classifier: Topic :: Office/Business
|
21
|
+
Classifier: Topic :: Office/Business :: Financial
|
22
|
+
Classifier: Topic :: Software Development
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
25
|
+
Requires-Python: >=3.10
|
26
|
+
Description-Content-Type: text/markdown
|
27
|
+
License-File: LICENSE
|
28
|
+
Requires-Dist: tracdap-runtime>=0.10.0-dev.15
|
29
|
+
Requires-Dist: openai~=1.0
|
30
|
+
Dynamic: license-file
|
31
|
+
|
32
|
+
<h1 align="center">
|
33
|
+
|
34
|
+

|
35
|
+
|
36
|
+
</h1>
|
37
|
+
|
38
|
+
<p align="center">
|
39
|
+
<a href="https://pypi.org/project/tracdap-ext-openai"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/tracdap-ext-openai.svg?maxAge=86400" /></a>
|
40
|
+
<a href="https://pypi.org/project/tracdap-ext-openai"><img alt="Python Versions" src="https://img.shields.io/pypi/pyversions/tracdap-ext-openai.svg?maxAge=86400" /></a>
|
41
|
+
<a href="https://github.com/finos/tracdap/actions/workflows/packaging.yaml?query=branch%3Amain"><img alt="Packaging status" src="https://github.com/finos/tracdap/actions/workflows/packaging.yaml/badge.svg?branch:main&workflow:CI" /></a>
|
42
|
+
<a href="https://github.com/finos/tracdap/actions/workflows/compliance.yaml?query=branch%3Amain"><img alt="Compliance status" src="https://github.com/finos/tracdap/actions/workflows/compliance.yaml/badge.svg?branch:main&workflow:CI" /></a>
|
43
|
+
<a href="https://community.finos.org/docs/governance/software-projects/stages/incubating/"><img alt="FINOS - Incubating" src="https://cdn.jsdelivr.net/gh/finos/contrib-toolbox@master/images/badge-incubating.svg" /></a>
|
44
|
+
</p>
|
45
|
+
|
46
|
+
|
47
|
+
# OpenAI Extension for the TRAC Model Runtime
|
48
|
+
|
49
|
+
This extension makes the OpenAI Python SDK available to use from inside a TRAC model.
|
50
|
+
|
51
|
+
- Use the native OpenAI client classes directly in TRAC model code
|
52
|
+
- Connection settings managed by TRAC for both local and deployed models
|
53
|
+
- Supports both OpenAI and AzureOpenAI clients
|
54
|
+
- Everything available in the OpenAI SDK, also works in TRAC
|
55
|
+
|
56
|
+
Models that make external calls are not considered repeatable,
|
57
|
+
and will be flagged as non-repeatable when they run on the TRAC platform.
|
58
|
+
|
59
|
+
This extension is a pre-release and will be finalized inTRAC 0.10.
|
60
|
+
|
61
|
+
|
62
|
+
## Installing
|
63
|
+
|
64
|
+
The OpenAI extension can be installed with [pip](https://pip.pypa.io):
|
65
|
+
|
66
|
+
```shell
|
67
|
+
$ pip install tracdap-ext-openai
|
68
|
+
```
|
69
|
+
|
70
|
+
The package has the following dependencies:
|
71
|
+
|
72
|
+
- tracdap-runtime (version 0.10.0-beta1 or later)
|
73
|
+
- openai (version 1.x)
|
74
|
+
|
75
|
+
|
76
|
+
## Using the OpenAI client
|
77
|
+
|
78
|
+
Here is a minimum working example of a TRAC model using the OpenAI client:
|
79
|
+
|
80
|
+
```python
|
81
|
+
import tracdap.rt.api as trac
|
82
|
+
import openai
|
83
|
+
|
84
|
+
class OpenAIModel(trac.TracModel):
|
85
|
+
|
86
|
+
# ... define parameters, inputs and outputs
|
87
|
+
|
88
|
+
def define_resources(self):
|
89
|
+
|
90
|
+
return {
|
91
|
+
"openai": trac.define_external_system("openai", openai.OpenAI),
|
92
|
+
}
|
93
|
+
|
94
|
+
def run_model(self, ctx: trac.TracContext):
|
95
|
+
|
96
|
+
with ctx.get_external_system("openai", openai.OpenAI) as client:
|
97
|
+
|
98
|
+
response = client.responses.create(
|
99
|
+
model="gpt-4o",
|
100
|
+
instructions="You are a coding assistant that talks like a pirate.",
|
101
|
+
input="How do I check if a Python object is an instance of a class?",
|
102
|
+
)
|
103
|
+
|
104
|
+
ctx.log.info(response.output_text)
|
105
|
+
|
106
|
+
if __name__ == '__main__':
|
107
|
+
import tracdap.rt.launch as launch
|
108
|
+
launch.launch_model(OpenAIModel, "config/job_config.yaml", "config/sys_config.yaml")
|
109
|
+
```
|
110
|
+
|
111
|
+
To make this example work, you will need to add ``openai`` as a resource in the system config file:
|
112
|
+
|
113
|
+
```yaml
|
114
|
+
resources:
|
115
|
+
|
116
|
+
openai:
|
117
|
+
resourceType: EXTERNAL_SYSTEM
|
118
|
+
protocol: openai
|
119
|
+
```
|
120
|
+
|
121
|
+
The client can be customized by setting additional properties on the resource,
|
122
|
+
which are passed through to the OpenAI client.
|
123
|
+
|
124
|
+
```yaml
|
125
|
+
resources:
|
126
|
+
|
127
|
+
openai:
|
128
|
+
resourceType: EXTERNAL_SYSTEM
|
129
|
+
protocol: openai
|
130
|
+
properties:
|
131
|
+
project: proj_xxxxxxxxxxxxx
|
132
|
+
```
|
133
|
+
|
134
|
+
The following configuration properties are supported:
|
135
|
+
|
136
|
+
- api_key, string, required
|
137
|
+
- organization, string, optional
|
138
|
+
- project, string, optional
|
139
|
+
- base_url, string, default = https://api.openai.com/v1/
|
140
|
+
- timeout, float, defeault = openai.DEFAULT_TIMEOUT.read (currently 600 seconds)
|
141
|
+
- max_retries, int, default = openai.DEFAULT_MAX_RETRIES (currently 2)
|
142
|
+
|
143
|
+
The ``api_key`` should not be put into a config file in plain text,
|
144
|
+
for local development it is recommended to set the OPENAI_API_KEY environment variable instead.
|
145
|
+
If both the config property and the environment variable are set, the config property takes precedence.
|
146
|
+
|
147
|
+
|
148
|
+
## Using the AzureOpenAI client
|
149
|
+
|
150
|
+
Here is a minimum working example of a TRAC model using the AzureOpenAI client.
|
151
|
+
This assumes the required resources and deployments have been set up in Azure.
|
152
|
+
|
153
|
+
```python
|
154
|
+
import tracdap.rt.api as trac
|
155
|
+
import openai
|
156
|
+
|
157
|
+
class TestModel(trac.TracModel):
|
158
|
+
|
159
|
+
# ... define parameters, inputs and outputs
|
160
|
+
|
161
|
+
def define_resources(self):
|
162
|
+
|
163
|
+
return {
|
164
|
+
"openai_azure": trac.define_external_system("openai", openai.AzureOpenAI)
|
165
|
+
}
|
166
|
+
|
167
|
+
def run_model(self, ctx: trac.TracContext):
|
168
|
+
|
169
|
+
with ctx.get_external_system("openai_azure", openai.AzureOpenAI) as client:
|
170
|
+
|
171
|
+
completion = client.chat.completions.create(
|
172
|
+
model="gpt-4.1-mini",
|
173
|
+
messages=[
|
174
|
+
{ "role": "system", "content": "You are a coding assistant that talks like a pirate."},
|
175
|
+
{ "role": "user", "content": "How do I check if a Python object is an instance of a class?" },
|
176
|
+
]
|
177
|
+
)
|
178
|
+
|
179
|
+
ctx.log.info(completion.choices[0].message.content)
|
180
|
+
|
181
|
+
if __name__ == '__main__':
|
182
|
+
import tracdap.rt.launch as launch
|
183
|
+
launch.launch_model(TestModel, "config/job_config.yaml", "config/sys_config.yaml")
|
184
|
+
```
|
185
|
+
|
186
|
+
To make this example work, you will need to add ``openai_azure`` as a resource in the system config file:
|
187
|
+
|
188
|
+
```yaml
|
189
|
+
resources:
|
190
|
+
|
191
|
+
openai_azure:
|
192
|
+
resourceType: EXTERNAL_SYSTEM
|
193
|
+
protocol: openai
|
194
|
+
subProtocol: azure
|
195
|
+
properties:
|
196
|
+
api_version: 2025-04-01-preview
|
197
|
+
azure_endpoint: https://my-azure-endpoint.cognitiveservices.azure.com/
|
198
|
+
```
|
199
|
+
|
200
|
+
Setting ``supProtcol: azure`` is required for to create an Azure client.
|
201
|
+
The ``api_version`` and ``azure_endpoint`` properties must be specified,
|
202
|
+
and ``model`` parameter in the client call must refer to live model deployment on that endpoint.
|
203
|
+
|
204
|
+
All the configuration properties supported by the regular client are also supported by the Azure client.
|
205
|
+
Additionally, the Azure client supports these extra properties:
|
206
|
+
|
207
|
+
- api_version, string, required
|
208
|
+
- azure_endpoint, string, required
|
209
|
+
- azure_deployment, string, optional
|
210
|
+
- azure_ad_token, string, optional
|
211
|
+
|
212
|
+
For he Azure client, if ``api_key`` is not specified in the config file
|
213
|
+
it is read from the environment variable ``AZURE_OPENAI_API_KEY``.
|
214
|
+
Similarly, ``azure_ad_token`` can be read from the environment variable ``AZURE_OPENAI_AD_TOKEN``.
|
215
|
+
If both the config property and the environment variable are set, the config property takes precedence.
|
@@ -0,0 +1,184 @@
|
|
1
|
+
<h1 align="center">
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
</h1>
|
6
|
+
|
7
|
+
<p align="center">
|
8
|
+
<a href="https://pypi.org/project/tracdap-ext-openai"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/tracdap-ext-openai.svg?maxAge=86400" /></a>
|
9
|
+
<a href="https://pypi.org/project/tracdap-ext-openai"><img alt="Python Versions" src="https://img.shields.io/pypi/pyversions/tracdap-ext-openai.svg?maxAge=86400" /></a>
|
10
|
+
<a href="https://github.com/finos/tracdap/actions/workflows/packaging.yaml?query=branch%3Amain"><img alt="Packaging status" src="https://github.com/finos/tracdap/actions/workflows/packaging.yaml/badge.svg?branch:main&workflow:CI" /></a>
|
11
|
+
<a href="https://github.com/finos/tracdap/actions/workflows/compliance.yaml?query=branch%3Amain"><img alt="Compliance status" src="https://github.com/finos/tracdap/actions/workflows/compliance.yaml/badge.svg?branch:main&workflow:CI" /></a>
|
12
|
+
<a href="https://community.finos.org/docs/governance/software-projects/stages/incubating/"><img alt="FINOS - Incubating" src="https://cdn.jsdelivr.net/gh/finos/contrib-toolbox@master/images/badge-incubating.svg" /></a>
|
13
|
+
</p>
|
14
|
+
|
15
|
+
|
16
|
+
# OpenAI Extension for the TRAC Model Runtime
|
17
|
+
|
18
|
+
This extension makes the OpenAI Python SDK available to use from inside a TRAC model.
|
19
|
+
|
20
|
+
- Use the native OpenAI client classes directly in TRAC model code
|
21
|
+
- Connection settings managed by TRAC for both local and deployed models
|
22
|
+
- Supports both OpenAI and AzureOpenAI clients
|
23
|
+
- Everything available in the OpenAI SDK, also works in TRAC
|
24
|
+
|
25
|
+
Models that make external calls are not considered repeatable,
|
26
|
+
and will be flagged as non-repeatable when they run on the TRAC platform.
|
27
|
+
|
28
|
+
This extension is a pre-release and will be finalized inTRAC 0.10.
|
29
|
+
|
30
|
+
|
31
|
+
## Installing
|
32
|
+
|
33
|
+
The OpenAI extension can be installed with [pip](https://pip.pypa.io):
|
34
|
+
|
35
|
+
```shell
|
36
|
+
$ pip install tracdap-ext-openai
|
37
|
+
```
|
38
|
+
|
39
|
+
The package has the following dependencies:
|
40
|
+
|
41
|
+
- tracdap-runtime (version 0.10.0-beta1 or later)
|
42
|
+
- openai (version 1.x)
|
43
|
+
|
44
|
+
|
45
|
+
## Using the OpenAI client
|
46
|
+
|
47
|
+
Here is a minimum working example of a TRAC model using the OpenAI client:
|
48
|
+
|
49
|
+
```python
|
50
|
+
import tracdap.rt.api as trac
|
51
|
+
import openai
|
52
|
+
|
53
|
+
class OpenAIModel(trac.TracModel):
|
54
|
+
|
55
|
+
# ... define parameters, inputs and outputs
|
56
|
+
|
57
|
+
def define_resources(self):
|
58
|
+
|
59
|
+
return {
|
60
|
+
"openai": trac.define_external_system("openai", openai.OpenAI),
|
61
|
+
}
|
62
|
+
|
63
|
+
def run_model(self, ctx: trac.TracContext):
|
64
|
+
|
65
|
+
with ctx.get_external_system("openai", openai.OpenAI) as client:
|
66
|
+
|
67
|
+
response = client.responses.create(
|
68
|
+
model="gpt-4o",
|
69
|
+
instructions="You are a coding assistant that talks like a pirate.",
|
70
|
+
input="How do I check if a Python object is an instance of a class?",
|
71
|
+
)
|
72
|
+
|
73
|
+
ctx.log.info(response.output_text)
|
74
|
+
|
75
|
+
if __name__ == '__main__':
|
76
|
+
import tracdap.rt.launch as launch
|
77
|
+
launch.launch_model(OpenAIModel, "config/job_config.yaml", "config/sys_config.yaml")
|
78
|
+
```
|
79
|
+
|
80
|
+
To make this example work, you will need to add ``openai`` as a resource in the system config file:
|
81
|
+
|
82
|
+
```yaml
|
83
|
+
resources:
|
84
|
+
|
85
|
+
openai:
|
86
|
+
resourceType: EXTERNAL_SYSTEM
|
87
|
+
protocol: openai
|
88
|
+
```
|
89
|
+
|
90
|
+
The client can be customized by setting additional properties on the resource,
|
91
|
+
which are passed through to the OpenAI client.
|
92
|
+
|
93
|
+
```yaml
|
94
|
+
resources:
|
95
|
+
|
96
|
+
openai:
|
97
|
+
resourceType: EXTERNAL_SYSTEM
|
98
|
+
protocol: openai
|
99
|
+
properties:
|
100
|
+
project: proj_xxxxxxxxxxxxx
|
101
|
+
```
|
102
|
+
|
103
|
+
The following configuration properties are supported:
|
104
|
+
|
105
|
+
- api_key, string, required
|
106
|
+
- organization, string, optional
|
107
|
+
- project, string, optional
|
108
|
+
- base_url, string, default = https://api.openai.com/v1/
|
109
|
+
- timeout, float, defeault = openai.DEFAULT_TIMEOUT.read (currently 600 seconds)
|
110
|
+
- max_retries, int, default = openai.DEFAULT_MAX_RETRIES (currently 2)
|
111
|
+
|
112
|
+
The ``api_key`` should not be put into a config file in plain text,
|
113
|
+
for local development it is recommended to set the OPENAI_API_KEY environment variable instead.
|
114
|
+
If both the config property and the environment variable are set, the config property takes precedence.
|
115
|
+
|
116
|
+
|
117
|
+
## Using the AzureOpenAI client
|
118
|
+
|
119
|
+
Here is a minimum working example of a TRAC model using the AzureOpenAI client.
|
120
|
+
This assumes the required resources and deployments have been set up in Azure.
|
121
|
+
|
122
|
+
```python
|
123
|
+
import tracdap.rt.api as trac
|
124
|
+
import openai
|
125
|
+
|
126
|
+
class TestModel(trac.TracModel):
|
127
|
+
|
128
|
+
# ... define parameters, inputs and outputs
|
129
|
+
|
130
|
+
def define_resources(self):
|
131
|
+
|
132
|
+
return {
|
133
|
+
"openai_azure": trac.define_external_system("openai", openai.AzureOpenAI)
|
134
|
+
}
|
135
|
+
|
136
|
+
def run_model(self, ctx: trac.TracContext):
|
137
|
+
|
138
|
+
with ctx.get_external_system("openai_azure", openai.AzureOpenAI) as client:
|
139
|
+
|
140
|
+
completion = client.chat.completions.create(
|
141
|
+
model="gpt-4.1-mini",
|
142
|
+
messages=[
|
143
|
+
{ "role": "system", "content": "You are a coding assistant that talks like a pirate."},
|
144
|
+
{ "role": "user", "content": "How do I check if a Python object is an instance of a class?" },
|
145
|
+
]
|
146
|
+
)
|
147
|
+
|
148
|
+
ctx.log.info(completion.choices[0].message.content)
|
149
|
+
|
150
|
+
if __name__ == '__main__':
|
151
|
+
import tracdap.rt.launch as launch
|
152
|
+
launch.launch_model(TestModel, "config/job_config.yaml", "config/sys_config.yaml")
|
153
|
+
```
|
154
|
+
|
155
|
+
To make this example work, you will need to add ``openai_azure`` as a resource in the system config file:
|
156
|
+
|
157
|
+
```yaml
|
158
|
+
resources:
|
159
|
+
|
160
|
+
openai_azure:
|
161
|
+
resourceType: EXTERNAL_SYSTEM
|
162
|
+
protocol: openai
|
163
|
+
subProtocol: azure
|
164
|
+
properties:
|
165
|
+
api_version: 2025-04-01-preview
|
166
|
+
azure_endpoint: https://my-azure-endpoint.cognitiveservices.azure.com/
|
167
|
+
```
|
168
|
+
|
169
|
+
Setting ``supProtcol: azure`` is required for to create an Azure client.
|
170
|
+
The ``api_version`` and ``azure_endpoint`` properties must be specified,
|
171
|
+
and ``model`` parameter in the client call must refer to live model deployment on that endpoint.
|
172
|
+
|
173
|
+
All the configuration properties supported by the regular client are also supported by the Azure client.
|
174
|
+
Additionally, the Azure client supports these extra properties:
|
175
|
+
|
176
|
+
- api_version, string, required
|
177
|
+
- azure_endpoint, string, required
|
178
|
+
- azure_deployment, string, optional
|
179
|
+
- azure_ad_token, string, optional
|
180
|
+
|
181
|
+
For he Azure client, if ``api_key`` is not specified in the config file
|
182
|
+
it is read from the environment variable ``AZURE_OPENAI_API_KEY``.
|
183
|
+
Similarly, ``azure_ad_token`` can be read from the environment variable ``AZURE_OPENAI_AD_TOKEN``.
|
184
|
+
If both the config property and the environment variable are set, the config property takes precedence.
|
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
[build-system]
|
4
|
+
|
5
|
+
requires = ["setuptools >= 77.0.3"]
|
6
|
+
build-backend = "setuptools.build_meta"
|
7
|
+
|
8
|
+
[project]
|
9
|
+
|
10
|
+
dynamic = ["version", "dependencies"]
|
11
|
+
|
12
|
+
name = "tracdap-ext-openai"
|
13
|
+
description = "An extension for TRAC D.A.P. that lets models connect to OpenAI endpoints"
|
14
|
+
readme = "README.md"
|
15
|
+
|
16
|
+
license = "Apache-2.0"
|
17
|
+
license-files = ["LICENSE"]
|
18
|
+
|
19
|
+
authors = [
|
20
|
+
{name = "Martin Traverse", email = "martin@fintrac.co.uk"}
|
21
|
+
]
|
22
|
+
|
23
|
+
classifiers = [
|
24
|
+
"Operating System :: OS Independent",
|
25
|
+
"Programming Language :: Python :: 3",
|
26
|
+
"Programming Language :: Python :: 3.10",
|
27
|
+
"Programming Language :: Python :: 3.11",
|
28
|
+
"Programming Language :: Python :: 3.12",
|
29
|
+
"Programming Language :: Python :: 3.13",
|
30
|
+
"Intended Audience :: Developers",
|
31
|
+
"Intended Audience :: End Users/Desktop",
|
32
|
+
"Intended Audience :: Financial and Insurance Industry",
|
33
|
+
"Topic :: Office/Business",
|
34
|
+
"Topic :: Office/Business :: Financial",
|
35
|
+
"Topic :: Software Development",
|
36
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
37
|
+
"Topic :: Software Development :: Libraries :: Python Modules"
|
38
|
+
]
|
39
|
+
|
40
|
+
requires-python = ">= 3.10"
|
41
|
+
|
42
|
+
|
43
|
+
[project.urls]
|
44
|
+
|
45
|
+
Homepage = "https://tracdap.finos.org/"
|
46
|
+
Documentation = "https://tracdap.readthedocs.io/"
|
47
|
+
"Source Code" = "https://github.com/finos/tracdap"
|
48
|
+
"Issue Tracker" = "https://github.com/finos/tracdap/issues"
|
49
|
+
|
50
|
+
[tool.setuptools.packages.find]
|
51
|
+
|
52
|
+
where = ["src"]
|
53
|
+
namespaces = true
|
54
|
+
|
55
|
+
|
56
|
+
[tool.setuptools.dynamic]
|
57
|
+
|
58
|
+
version = { attr = "tracdap.ext.openai.__version__" }
|
59
|
+
dependencies = { file = ["requirements.txt"] }
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Licensed to the Fintech Open Source Foundation (FINOS) under one or
|
2
|
+
# more contributor license agreements. See the NOTICE file distributed
|
3
|
+
# with this work for additional information regarding copyright ownership.
|
4
|
+
# FINOS licenses this file to you under the Apache License, Version 2.0
|
5
|
+
# (the "License"); you may not use this file except in compliance with the
|
6
|
+
# License. You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
__version__ = "0.10.0.dev15"
|
@@ -0,0 +1,261 @@
|
|
1
|
+
# Licensed to the Fintech Open Source Foundation (FINOS) under one or
|
2
|
+
# more contributor license agreements. See the NOTICE file distributed
|
3
|
+
# with this work for additional information regarding copyright ownership.
|
4
|
+
# FINOS licenses this file to you under the Apache License, Version 2.0
|
5
|
+
# (the "License"); you may not use this file except in compliance with the
|
6
|
+
# License. You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
import os
|
17
|
+
import threading
|
18
|
+
import concurrent.futures as fut
|
19
|
+
import queue
|
20
|
+
import logging
|
21
|
+
|
22
|
+
|
23
|
+
try:
|
24
|
+
import openai # noqa
|
25
|
+
except ModuleNotFoundError:
|
26
|
+
openai = None
|
27
|
+
|
28
|
+
import tracdap.rt.config as _cfg
|
29
|
+
import tracdap.rt.exceptions as _ex
|
30
|
+
import tracdap.rt.ext.external as _external
|
31
|
+
import tracdap.rt.ext.plugins as _plugins
|
32
|
+
import tracdap.rt.ext.util as _util
|
33
|
+
|
34
|
+
|
35
|
+
class OpenAIPlugin(_external.IExternalSystem):
|
36
|
+
|
37
|
+
API_KEY_KEY = "api_key"
|
38
|
+
ORGANIZATION_KEY = "organization"
|
39
|
+
PROJECT_KEY = "project"
|
40
|
+
BASE_URL_KEY = "base_url"
|
41
|
+
TIMEOUT_KEY = "timeout"
|
42
|
+
MAX_RETRIES_KEY = "max_retries"
|
43
|
+
|
44
|
+
AZURE_SUB_PROTOCOL = "azure"
|
45
|
+
API_VERSION_KEY = "api_version"
|
46
|
+
AZURE_ENDPOINT_KEY = "azure_endpoint"
|
47
|
+
AZURE_DEPLOYMENT_KEY = "azure_deployment"
|
48
|
+
AZURE_AD_TOKEN_KEY = "azure_ad_token"
|
49
|
+
|
50
|
+
OPENAI_API_KEY = "OPENAI_API_KEY"
|
51
|
+
AZURE_OPENAI_API_KEY = "AZURE_OPENAI_API_KEY"
|
52
|
+
AZURE_OPENAI_AD_TOKEN = "AZURE_OPENAI_AD_TOKEN"
|
53
|
+
|
54
|
+
def __init__(self, resource_name: str, config: _cfg.PluginConfig):
|
55
|
+
|
56
|
+
log_name = f"{OpenAIPlugin.__module__}.{OpenAIPlugin.__name__}"
|
57
|
+
self.__log = logging.getLogger(log_name)
|
58
|
+
|
59
|
+
self.__resource_name = resource_name
|
60
|
+
self.__protocol = config.protocol
|
61
|
+
self.__sub_protocol = config.subProtocol
|
62
|
+
|
63
|
+
self.__organization = _util.read_plugin_config(config, self.ORGANIZATION_KEY, optional=True)
|
64
|
+
self.__project = _util.read_plugin_config(config, self.PROJECT_KEY, optional=True)
|
65
|
+
self.__base_url = _util.read_plugin_config(config, self.BASE_URL_KEY, optional=True)
|
66
|
+
self.__timeout = _util.read_plugin_config(config, self.TIMEOUT_KEY, default=openai.DEFAULT_TIMEOUT.read, convert=float)
|
67
|
+
self.__max_retries = _util.read_plugin_config(config, self.MAX_RETRIES_KEY, default=openai.DEFAULT_MAX_RETRIES, convert=int)
|
68
|
+
|
69
|
+
self.__api_version = _util.read_plugin_config(config, self.API_VERSION_KEY, optional=True)
|
70
|
+
self.__azure_endpoint = _util.read_plugin_config(config, self.AZURE_ENDPOINT_KEY, optional=True)
|
71
|
+
self.__azure_deployment = _util.read_plugin_config(config, self.AZURE_DEPLOYMENT_KEY, optional=True)
|
72
|
+
|
73
|
+
if _util.has_plugin_config(config, self.API_KEY_KEY):
|
74
|
+
api_key = _util.read_plugin_config(config, self.API_KEY_KEY)
|
75
|
+
self.__api_key_func = lambda: api_key
|
76
|
+
else:
|
77
|
+
self.__api_key_func = None
|
78
|
+
|
79
|
+
if _util.has_plugin_config(config, self.AZURE_AD_TOKEN_KEY):
|
80
|
+
ad_token = _util.read_plugin_config(config, self.AZURE_AD_TOKEN_KEY)
|
81
|
+
self.__azure_ad_token_func = lambda: ad_token
|
82
|
+
else:
|
83
|
+
self.__azure_ad_token_func = None
|
84
|
+
|
85
|
+
self.__factory_queue = queue.Queue()
|
86
|
+
self.__factory_thread = threading.Thread(name="openai-factory", target=self.__factory_main, daemon=True)
|
87
|
+
self.__factory_thread.start()
|
88
|
+
self.__warmed_up = False
|
89
|
+
|
90
|
+
# Do not print info-level logs from the low-level frameworks
|
91
|
+
# HTTPX logs every request by default
|
92
|
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
93
|
+
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
94
|
+
logging.getLogger("openai").setLevel(logging.WARNING)
|
95
|
+
|
96
|
+
def supported_types(self) -> list[type]:
|
97
|
+
|
98
|
+
supported_types = [openai.OpenAI]
|
99
|
+
|
100
|
+
if openai.AzureOpenAI is not None:
|
101
|
+
supported_types.append(openai.AzureOpenAI)
|
102
|
+
|
103
|
+
return supported_types
|
104
|
+
|
105
|
+
def supported_args(self) -> dict[str, type] | None:
|
106
|
+
|
107
|
+
return {
|
108
|
+
self.TIMEOUT_KEY: float,
|
109
|
+
self.MAX_RETRIES_KEY: int
|
110
|
+
}
|
111
|
+
|
112
|
+
def create_client(self, client_type: type, **client_args) -> object:
|
113
|
+
|
114
|
+
future = fut.Future()
|
115
|
+
msg = (lambda: self._create_client_internal(client_type, **client_args), future)
|
116
|
+
|
117
|
+
self.__factory_queue.put(msg)
|
118
|
+
|
119
|
+
return future.result()
|
120
|
+
|
121
|
+
def _create_client_internal(self, client_type: type, **client_args) -> object:
|
122
|
+
|
123
|
+
if client_type == openai.OpenAI:
|
124
|
+
if self.__sub_protocol:
|
125
|
+
detail = f"The resource [{self.__resource_name }] is configured with sub protocol = [{self.__sub_protocol}]"
|
126
|
+
raise _ex.ERuntimeValidation(f"Cannot create OpenAI client: {detail}")
|
127
|
+
else:
|
128
|
+
return self._create_client_std(**client_args)
|
129
|
+
|
130
|
+
if openai.AzureOpenAI and client_type == openai.AzureOpenAI:
|
131
|
+
if self.__sub_protocol != self.AZURE_SUB_PROTOCOL:
|
132
|
+
detail = f"The resource [{self.__resource_name }] is configured with sub protocol = [{self.__sub_protocol}]"
|
133
|
+
raise _ex.ERuntimeValidation(f"Cannot create Azure OpenAI client: {detail}")
|
134
|
+
else:
|
135
|
+
return self._create_client_azure(**client_args)
|
136
|
+
|
137
|
+
raise _ex.EPluginNotAvailable(f"Client type [{client_type.__qualname__}] is not available in {self.__class__.__name__}")
|
138
|
+
|
139
|
+
def _create_client_std(self, **client_args):
|
140
|
+
|
141
|
+
std_args = self._build_std_args(**client_args)
|
142
|
+
return openai.OpenAI(**std_args)
|
143
|
+
|
144
|
+
def _create_client_azure(self, **client_args):
|
145
|
+
|
146
|
+
azure_args = self._build_azure_args(**client_args)
|
147
|
+
return openai.AzureOpenAI(**azure_args)
|
148
|
+
|
149
|
+
def _build_std_args(self, **client_args):
|
150
|
+
|
151
|
+
args = self._build_common_args(**client_args)
|
152
|
+
|
153
|
+
if self.__api_key_func:
|
154
|
+
api_key = self.__api_key_func()
|
155
|
+
else:
|
156
|
+
api_key = os.getenv(self.OPENAI_API_KEY)
|
157
|
+
|
158
|
+
if api_key:
|
159
|
+
args[self.API_KEY_KEY] = lambda: api_key
|
160
|
+
|
161
|
+
return args
|
162
|
+
|
163
|
+
def _build_azure_args(self, **client_args):
|
164
|
+
|
165
|
+
args = self._build_common_args(**client_args)
|
166
|
+
|
167
|
+
self._optional_arg(args, self.API_VERSION_KEY, self.__api_version)
|
168
|
+
self._optional_arg(args, self.AZURE_ENDPOINT_KEY, self.__azure_endpoint)
|
169
|
+
self._optional_arg(args, self.AZURE_DEPLOYMENT_KEY, self.__azure_deployment)
|
170
|
+
|
171
|
+
if self.__api_key_func:
|
172
|
+
api_key = self.__api_key_func()
|
173
|
+
else:
|
174
|
+
api_key = os.getenv(self.AZURE_OPENAI_API_KEY)
|
175
|
+
|
176
|
+
if api_key:
|
177
|
+
args[self.API_KEY_KEY] = api_key
|
178
|
+
|
179
|
+
if self.__azure_ad_token_func:
|
180
|
+
azure_ad_token = self.__azure_ad_token_func()
|
181
|
+
else:
|
182
|
+
azure_ad_token = os.getenv(self.AZURE_OPENAI_AD_TOKEN)
|
183
|
+
|
184
|
+
if azure_ad_token is not None:
|
185
|
+
args[self.AZURE_AD_TOKEN_KEY] = azure_ad_token
|
186
|
+
|
187
|
+
return args
|
188
|
+
|
189
|
+
def _build_common_args(self, **client_args):
|
190
|
+
|
191
|
+
args = dict()
|
192
|
+
|
193
|
+
self._optional_arg(args, self.ORGANIZATION_KEY, self.__organization)
|
194
|
+
self._optional_arg(args, self.PROJECT_KEY, self.__project)
|
195
|
+
self._optional_arg(args, self.BASE_URL_KEY, self.__base_url)
|
196
|
+
|
197
|
+
if self.TIMEOUT_KEY in client_args:
|
198
|
+
args[self.TIMEOUT_KEY] = min(client_args[self.TIMEOUT_KEY], self.__timeout)
|
199
|
+
else:
|
200
|
+
args[self.TIMEOUT_KEY] = self.__timeout
|
201
|
+
|
202
|
+
if self.MAX_RETRIES_KEY in client_args:
|
203
|
+
args[self.MAX_RETRIES_KEY] = min(client_args[self.MAX_RETRIES_KEY], self.__max_retries)
|
204
|
+
else:
|
205
|
+
args[self.MAX_RETRIES_KEY] = self.__max_retries
|
206
|
+
|
207
|
+
return args
|
208
|
+
|
209
|
+
@staticmethod
|
210
|
+
def _optional_arg(args, key, value):
|
211
|
+
|
212
|
+
if value is not None:
|
213
|
+
args[key] = value
|
214
|
+
|
215
|
+
def close_client(self, client: object):
|
216
|
+
|
217
|
+
client.close() # noqa
|
218
|
+
|
219
|
+
def __factory_main(self):
|
220
|
+
|
221
|
+
# OpenAI uses asyncio under the hood, even for synchronous clients
|
222
|
+
# TRAC Actor threads have their own synchronization logic that interferes with AIO
|
223
|
+
# These issues can be avoided by creating OpenAI clients on a dedicated factory thread
|
224
|
+
# Initialization also happens on first use, so there has to be a warmup call as well
|
225
|
+
|
226
|
+
while True:
|
227
|
+
|
228
|
+
create_func, future = self.__factory_queue.get()
|
229
|
+
client = None
|
230
|
+
|
231
|
+
try:
|
232
|
+
|
233
|
+
client = create_func()
|
234
|
+
|
235
|
+
if not self.__warmed_up:
|
236
|
+
self.__warmup_client(client)
|
237
|
+
self.__warmed_up = True
|
238
|
+
|
239
|
+
future.set_result(client)
|
240
|
+
|
241
|
+
except Exception as e:
|
242
|
+
|
243
|
+
if client is not None:
|
244
|
+
client.close()
|
245
|
+
|
246
|
+
future.set_exception(e)
|
247
|
+
|
248
|
+
def __warmup_client(self, client):
|
249
|
+
|
250
|
+
self.__log.info("Warming up the OpenAI client...")
|
251
|
+
|
252
|
+
model_list = client.models.list()
|
253
|
+
model_count = len(model_list.data)
|
254
|
+
|
255
|
+
self.__log.info(f"Warmup complete: Found {model_count} models (there may be more)")
|
256
|
+
|
257
|
+
|
258
|
+
if openai:
|
259
|
+
_plugins.PluginManager.register_plugin(
|
260
|
+
_external.IExternalSystem, OpenAIPlugin,
|
261
|
+
protocols=["openai"])
|
@@ -0,0 +1,215 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: tracdap-ext-openai
|
3
|
+
Version: 0.10.0.dev15
|
4
|
+
Summary: An extension for TRAC D.A.P. that lets models connect to OpenAI endpoints
|
5
|
+
Author-email: Martin Traverse <martin@fintrac.co.uk>
|
6
|
+
License-Expression: Apache-2.0
|
7
|
+
Project-URL: Homepage, https://tracdap.finos.org/
|
8
|
+
Project-URL: Documentation, https://tracdap.readthedocs.io/
|
9
|
+
Project-URL: Source Code, https://github.com/finos/tracdap
|
10
|
+
Project-URL: Issue Tracker, https://github.com/finos/tracdap/issues
|
11
|
+
Classifier: Operating System :: OS Independent
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
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
|
+
Classifier: Intended Audience :: Developers
|
18
|
+
Classifier: Intended Audience :: End Users/Desktop
|
19
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
20
|
+
Classifier: Topic :: Office/Business
|
21
|
+
Classifier: Topic :: Office/Business :: Financial
|
22
|
+
Classifier: Topic :: Software Development
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
25
|
+
Requires-Python: >=3.10
|
26
|
+
Description-Content-Type: text/markdown
|
27
|
+
License-File: LICENSE
|
28
|
+
Requires-Dist: tracdap-runtime>=0.10.0-dev.15
|
29
|
+
Requires-Dist: openai~=1.0
|
30
|
+
Dynamic: license-file
|
31
|
+
|
32
|
+
<h1 align="center">
|
33
|
+
|
34
|
+

|
35
|
+
|
36
|
+
</h1>
|
37
|
+
|
38
|
+
<p align="center">
|
39
|
+
<a href="https://pypi.org/project/tracdap-ext-openai"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/tracdap-ext-openai.svg?maxAge=86400" /></a>
|
40
|
+
<a href="https://pypi.org/project/tracdap-ext-openai"><img alt="Python Versions" src="https://img.shields.io/pypi/pyversions/tracdap-ext-openai.svg?maxAge=86400" /></a>
|
41
|
+
<a href="https://github.com/finos/tracdap/actions/workflows/packaging.yaml?query=branch%3Amain"><img alt="Packaging status" src="https://github.com/finos/tracdap/actions/workflows/packaging.yaml/badge.svg?branch:main&workflow:CI" /></a>
|
42
|
+
<a href="https://github.com/finos/tracdap/actions/workflows/compliance.yaml?query=branch%3Amain"><img alt="Compliance status" src="https://github.com/finos/tracdap/actions/workflows/compliance.yaml/badge.svg?branch:main&workflow:CI" /></a>
|
43
|
+
<a href="https://community.finos.org/docs/governance/software-projects/stages/incubating/"><img alt="FINOS - Incubating" src="https://cdn.jsdelivr.net/gh/finos/contrib-toolbox@master/images/badge-incubating.svg" /></a>
|
44
|
+
</p>
|
45
|
+
|
46
|
+
|
47
|
+
# OpenAI Extension for the TRAC Model Runtime
|
48
|
+
|
49
|
+
This extension makes the OpenAI Python SDK available to use from inside a TRAC model.
|
50
|
+
|
51
|
+
- Use the native OpenAI client classes directly in TRAC model code
|
52
|
+
- Connection settings managed by TRAC for both local and deployed models
|
53
|
+
- Supports both OpenAI and AzureOpenAI clients
|
54
|
+
- Everything available in the OpenAI SDK, also works in TRAC
|
55
|
+
|
56
|
+
Models that make external calls are not considered repeatable,
|
57
|
+
and will be flagged as non-repeatable when they run on the TRAC platform.
|
58
|
+
|
59
|
+
This extension is a pre-release and will be finalized inTRAC 0.10.
|
60
|
+
|
61
|
+
|
62
|
+
## Installing
|
63
|
+
|
64
|
+
The OpenAI extension can be installed with [pip](https://pip.pypa.io):
|
65
|
+
|
66
|
+
```shell
|
67
|
+
$ pip install tracdap-ext-openai
|
68
|
+
```
|
69
|
+
|
70
|
+
The package has the following dependencies:
|
71
|
+
|
72
|
+
- tracdap-runtime (version 0.10.0-beta1 or later)
|
73
|
+
- openai (version 1.x)
|
74
|
+
|
75
|
+
|
76
|
+
## Using the OpenAI client
|
77
|
+
|
78
|
+
Here is a minimum working example of a TRAC model using the OpenAI client:
|
79
|
+
|
80
|
+
```python
|
81
|
+
import tracdap.rt.api as trac
|
82
|
+
import openai
|
83
|
+
|
84
|
+
class OpenAIModel(trac.TracModel):
|
85
|
+
|
86
|
+
# ... define parameters, inputs and outputs
|
87
|
+
|
88
|
+
def define_resources(self):
|
89
|
+
|
90
|
+
return {
|
91
|
+
"openai": trac.define_external_system("openai", openai.OpenAI),
|
92
|
+
}
|
93
|
+
|
94
|
+
def run_model(self, ctx: trac.TracContext):
|
95
|
+
|
96
|
+
with ctx.get_external_system("openai", openai.OpenAI) as client:
|
97
|
+
|
98
|
+
response = client.responses.create(
|
99
|
+
model="gpt-4o",
|
100
|
+
instructions="You are a coding assistant that talks like a pirate.",
|
101
|
+
input="How do I check if a Python object is an instance of a class?",
|
102
|
+
)
|
103
|
+
|
104
|
+
ctx.log.info(response.output_text)
|
105
|
+
|
106
|
+
if __name__ == '__main__':
|
107
|
+
import tracdap.rt.launch as launch
|
108
|
+
launch.launch_model(OpenAIModel, "config/job_config.yaml", "config/sys_config.yaml")
|
109
|
+
```
|
110
|
+
|
111
|
+
To make this example work, you will need to add ``openai`` as a resource in the system config file:
|
112
|
+
|
113
|
+
```yaml
|
114
|
+
resources:
|
115
|
+
|
116
|
+
openai:
|
117
|
+
resourceType: EXTERNAL_SYSTEM
|
118
|
+
protocol: openai
|
119
|
+
```
|
120
|
+
|
121
|
+
The client can be customized by setting additional properties on the resource,
|
122
|
+
which are passed through to the OpenAI client.
|
123
|
+
|
124
|
+
```yaml
|
125
|
+
resources:
|
126
|
+
|
127
|
+
openai:
|
128
|
+
resourceType: EXTERNAL_SYSTEM
|
129
|
+
protocol: openai
|
130
|
+
properties:
|
131
|
+
project: proj_xxxxxxxxxxxxx
|
132
|
+
```
|
133
|
+
|
134
|
+
The following configuration properties are supported:
|
135
|
+
|
136
|
+
- api_key, string, required
|
137
|
+
- organization, string, optional
|
138
|
+
- project, string, optional
|
139
|
+
- base_url, string, default = https://api.openai.com/v1/
|
140
|
+
- timeout, float, defeault = openai.DEFAULT_TIMEOUT.read (currently 600 seconds)
|
141
|
+
- max_retries, int, default = openai.DEFAULT_MAX_RETRIES (currently 2)
|
142
|
+
|
143
|
+
The ``api_key`` should not be put into a config file in plain text,
|
144
|
+
for local development it is recommended to set the OPENAI_API_KEY environment variable instead.
|
145
|
+
If both the config property and the environment variable are set, the config property takes precedence.
|
146
|
+
|
147
|
+
|
148
|
+
## Using the AzureOpenAI client
|
149
|
+
|
150
|
+
Here is a minimum working example of a TRAC model using the AzureOpenAI client.
|
151
|
+
This assumes the required resources and deployments have been set up in Azure.
|
152
|
+
|
153
|
+
```python
|
154
|
+
import tracdap.rt.api as trac
|
155
|
+
import openai
|
156
|
+
|
157
|
+
class TestModel(trac.TracModel):
|
158
|
+
|
159
|
+
# ... define parameters, inputs and outputs
|
160
|
+
|
161
|
+
def define_resources(self):
|
162
|
+
|
163
|
+
return {
|
164
|
+
"openai_azure": trac.define_external_system("openai", openai.AzureOpenAI)
|
165
|
+
}
|
166
|
+
|
167
|
+
def run_model(self, ctx: trac.TracContext):
|
168
|
+
|
169
|
+
with ctx.get_external_system("openai_azure", openai.AzureOpenAI) as client:
|
170
|
+
|
171
|
+
completion = client.chat.completions.create(
|
172
|
+
model="gpt-4.1-mini",
|
173
|
+
messages=[
|
174
|
+
{ "role": "system", "content": "You are a coding assistant that talks like a pirate."},
|
175
|
+
{ "role": "user", "content": "How do I check if a Python object is an instance of a class?" },
|
176
|
+
]
|
177
|
+
)
|
178
|
+
|
179
|
+
ctx.log.info(completion.choices[0].message.content)
|
180
|
+
|
181
|
+
if __name__ == '__main__':
|
182
|
+
import tracdap.rt.launch as launch
|
183
|
+
launch.launch_model(TestModel, "config/job_config.yaml", "config/sys_config.yaml")
|
184
|
+
```
|
185
|
+
|
186
|
+
To make this example work, you will need to add ``openai_azure`` as a resource in the system config file:
|
187
|
+
|
188
|
+
```yaml
|
189
|
+
resources:
|
190
|
+
|
191
|
+
openai_azure:
|
192
|
+
resourceType: EXTERNAL_SYSTEM
|
193
|
+
protocol: openai
|
194
|
+
subProtocol: azure
|
195
|
+
properties:
|
196
|
+
api_version: 2025-04-01-preview
|
197
|
+
azure_endpoint: https://my-azure-endpoint.cognitiveservices.azure.com/
|
198
|
+
```
|
199
|
+
|
200
|
+
Setting ``supProtcol: azure`` is required for to create an Azure client.
|
201
|
+
The ``api_version`` and ``azure_endpoint`` properties must be specified,
|
202
|
+
and ``model`` parameter in the client call must refer to live model deployment on that endpoint.
|
203
|
+
|
204
|
+
All the configuration properties supported by the regular client are also supported by the Azure client.
|
205
|
+
Additionally, the Azure client supports these extra properties:
|
206
|
+
|
207
|
+
- api_version, string, required
|
208
|
+
- azure_endpoint, string, required
|
209
|
+
- azure_deployment, string, optional
|
210
|
+
- azure_ad_token, string, optional
|
211
|
+
|
212
|
+
For he Azure client, if ``api_key`` is not specified in the config file
|
213
|
+
it is read from the environment variable ``AZURE_OPENAI_API_KEY``.
|
214
|
+
Similarly, ``azure_ad_token`` can be read from the environment variable ``AZURE_OPENAI_AD_TOKEN``.
|
215
|
+
If both the config property and the environment variable are set, the config property takes precedence.
|
@@ -0,0 +1,11 @@
|
|
1
|
+
LICENSE
|
2
|
+
README.md
|
3
|
+
pyproject.toml
|
4
|
+
requirements.txt
|
5
|
+
src/tracdap/ext/openai/__init__.py
|
6
|
+
src/tracdap/ext/openai/openai_plugin.py
|
7
|
+
src/tracdap_ext_openai.egg-info/PKG-INFO
|
8
|
+
src/tracdap_ext_openai.egg-info/SOURCES.txt
|
9
|
+
src/tracdap_ext_openai.egg-info/dependency_links.txt
|
10
|
+
src/tracdap_ext_openai.egg-info/requires.txt
|
11
|
+
src/tracdap_ext_openai.egg-info/top_level.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
tracdap
|