@memberjunction/ai-systemone 0.0.0 → 6.2.0-edge.4
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.
- package/LICENSE +183 -0
- package/README.md +153 -28
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/models/perplexityDecision.d.ts +47 -0
- package/dist/models/perplexityDecision.d.ts.map +1 -0
- package/dist/models/perplexityDecision.js +94 -0
- package/dist/models/perplexityDecision.js.map +1 -0
- package/dist/models/systemOneDecision.d.ts +62 -0
- package/dist/models/systemOneDecision.d.ts.map +1 -0
- package/dist/models/systemOneDecision.js +135 -0
- package/dist/models/systemOneDecision.js.map +1 -0
- package/package.json +28 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
License text copyright (c) 2024 MariaDB plc, All Rights Reserved.
|
|
4
|
+
"Business Source License" is a trademark of MariaDB plc.
|
|
5
|
+
|
|
6
|
+
-----------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
Parameters
|
|
9
|
+
|
|
10
|
+
Licensor: Blue Cypress, Inc.
|
|
11
|
+
|
|
12
|
+
Licensed Work: MemberJunction.
|
|
13
|
+
The Licensed Work is (c) 2023-2026 Blue Cypress, Inc.
|
|
14
|
+
|
|
15
|
+
Additional Use Grant: Subject to the terms of this License, Licensor grants
|
|
16
|
+
you the following additional rights to make Production
|
|
17
|
+
Use of the Licensed Work.
|
|
18
|
+
|
|
19
|
+
1. Internal Use
|
|
20
|
+
|
|
21
|
+
You may make production use of the Licensed Work for
|
|
22
|
+
your own internal business or organizational operations.
|
|
23
|
+
|
|
24
|
+
2. Nonprofit Use
|
|
25
|
+
|
|
26
|
+
If you are a Nonprofit, you may make production use of
|
|
27
|
+
the Licensed Work for the operations and activities of
|
|
28
|
+
your Organizational Family.
|
|
29
|
+
|
|
30
|
+
3. MemberJunction Certified Program Use
|
|
31
|
+
|
|
32
|
+
If you are authorized by Licensor under the
|
|
33
|
+
MemberJunction Certified Program to provide professional
|
|
34
|
+
services using the Licensed Work, you may make
|
|
35
|
+
production use of the Licensed Work in providing such
|
|
36
|
+
professional services to a client, provided that:
|
|
37
|
+
|
|
38
|
+
(a) the Licensed Work is deployed in, and the applicable
|
|
39
|
+
production use occurs within, an environment owned,
|
|
40
|
+
leased, licensed, subscribed to, or otherwise controlled
|
|
41
|
+
by that client; and
|
|
42
|
+
|
|
43
|
+
(b) the production use is for that client's own internal
|
|
44
|
+
business or organizational operations or is otherwise
|
|
45
|
+
independently permitted to that client under this
|
|
46
|
+
Additional Use Grant.
|
|
47
|
+
|
|
48
|
+
4. Definitions Applicable to the Additional Use Grant
|
|
49
|
+
|
|
50
|
+
"Affiliate" means, with respect to a specified Person,
|
|
51
|
+
any other Person that directly or indirectly Controls,
|
|
52
|
+
is Controlled by, or is under common Control with such
|
|
53
|
+
specified Person.
|
|
54
|
+
|
|
55
|
+
"Control" (including the terms "Controls," "Controlled
|
|
56
|
+
by," and "under common Control with") means the direct
|
|
57
|
+
or indirect possession of the power to direct or cause
|
|
58
|
+
the direction of the management and policies of a
|
|
59
|
+
Person, whether through ownership of voting interests,
|
|
60
|
+
by contract, or otherwise.
|
|
61
|
+
|
|
62
|
+
"Organizational Family" means, with respect to a Person,
|
|
63
|
+
(a) such Person and its Affiliates, and (b) any
|
|
64
|
+
nonprofit organization, governmental entity, chapter,
|
|
65
|
+
division, local affiliate, regional affiliate, state
|
|
66
|
+
affiliate, national affiliate, or other entity that is
|
|
67
|
+
formally affiliated with such Person through governing
|
|
68
|
+
documents, a charter, bylaws, a membership agreement, or
|
|
69
|
+
another written organizational instrument, and is
|
|
70
|
+
recognized under such documents as part of the same
|
|
71
|
+
organizational structure.
|
|
72
|
+
|
|
73
|
+
"Nonprofit" means a Person recognized by the Internal
|
|
74
|
+
Revenue Service as exempt from federal income taxation
|
|
75
|
+
under Section 501(c)(3), 501(c)(4), 501(c)(5), or
|
|
76
|
+
501(c)(6) of the Internal Revenue Code, or a foreign
|
|
77
|
+
organization recognized under substantially equivalent
|
|
78
|
+
laws.
|
|
79
|
+
|
|
80
|
+
A Person claiming eligibility as a Nonprofit shall, upon
|
|
81
|
+
Licensor's reasonable request, provide documentation
|
|
82
|
+
reasonably sufficient to demonstrate that it qualifies
|
|
83
|
+
as a Nonprofit. If such Person materially misrepresents,
|
|
84
|
+
or is unable to demonstrate, its qualification as a
|
|
85
|
+
Nonprofit, the rights granted to such Person under
|
|
86
|
+
Section 2 of this Additional Use Grant shall terminate.
|
|
87
|
+
|
|
88
|
+
"MemberJunction Certified Program" means Licensor's
|
|
89
|
+
then-current program for certifying and authorizing a
|
|
90
|
+
Person to provide professional services using the
|
|
91
|
+
Licensed Work.
|
|
92
|
+
|
|
93
|
+
"Person" means any individual, corporation, limited
|
|
94
|
+
liability company, partnership, association, nonprofit
|
|
95
|
+
organization, governmental entity, or other legal or
|
|
96
|
+
organizational entity.
|
|
97
|
+
|
|
98
|
+
Change Date: Four (4) years from the date the Licensed Work is first
|
|
99
|
+
made available.
|
|
100
|
+
|
|
101
|
+
Change License: MIT License.
|
|
102
|
+
|
|
103
|
+
For information about alternative licensing arrangements for the Licensed
|
|
104
|
+
Work, please contact Blue Cypress, Inc.
|
|
105
|
+
|
|
106
|
+
-----------------------------------------------------------------------------
|
|
107
|
+
|
|
108
|
+
Terms
|
|
109
|
+
|
|
110
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
111
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
112
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
113
|
+
production use.
|
|
114
|
+
|
|
115
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
116
|
+
available distribution of a specific version of the Licensed Work under this
|
|
117
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
118
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
119
|
+
above terminate.
|
|
120
|
+
|
|
121
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
122
|
+
currently in effect as described in this License, you must purchase a
|
|
123
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
124
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
125
|
+
|
|
126
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
127
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
128
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
129
|
+
for each version of the Licensed Work released by Licensor.
|
|
130
|
+
|
|
131
|
+
You must conspicuously display this License on each original or modified copy
|
|
132
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
133
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
134
|
+
License apply to your use of that work.
|
|
135
|
+
|
|
136
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
137
|
+
terminate your rights under this License for the current and all other
|
|
138
|
+
versions of the Licensed Work.
|
|
139
|
+
|
|
140
|
+
This License does not grant you any right in any trademark or logo of
|
|
141
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
142
|
+
Licensor as expressly required by this License).
|
|
143
|
+
|
|
144
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
145
|
+
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
146
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
147
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
148
|
+
TITLE.
|
|
149
|
+
|
|
150
|
+
MariaDB hereby grants you permission to use this License's text to license
|
|
151
|
+
your works, and to refer to it using the trademark "Business Source License",
|
|
152
|
+
as long as you comply with the Covenants of Licensor below.
|
|
153
|
+
|
|
154
|
+
-----------------------------------------------------------------------------
|
|
155
|
+
|
|
156
|
+
Covenants of Licensor
|
|
157
|
+
|
|
158
|
+
In consideration of the right to use this License's text and the "Business
|
|
159
|
+
Source License" name and trademark, Licensor covenants to MariaDB, and to all
|
|
160
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
161
|
+
|
|
162
|
+
1. To specify as the Change License the GPL Version 2.0 or any later version,
|
|
163
|
+
or a license that is compatible with GPL Version 2.0 or a later version,
|
|
164
|
+
where "compatible" means that software provided under the Change License
|
|
165
|
+
can be included in a program with software provided under GPL Version 2.0
|
|
166
|
+
or a later version. Licensor may specify additional Change Licenses without
|
|
167
|
+
limitation.
|
|
168
|
+
|
|
169
|
+
2. To either: (a) specify an additional grant of rights to use that does not
|
|
170
|
+
impose any additional restriction on the right granted in this License, as
|
|
171
|
+
the Additional Use Grant; or (b) insert the text "None".
|
|
172
|
+
|
|
173
|
+
3. To specify a Change Date.
|
|
174
|
+
|
|
175
|
+
4. Not to modify this License in any other way.
|
|
176
|
+
|
|
177
|
+
-----------------------------------------------------------------------------
|
|
178
|
+
|
|
179
|
+
Notice
|
|
180
|
+
|
|
181
|
+
The Business Source License (this document, or the "License") is not an Open
|
|
182
|
+
Source license. However, the Licensed Work will eventually be made available
|
|
183
|
+
under an Open Source License, as stated in this License.
|
package/README.md
CHANGED
|
@@ -1,45 +1,170 @@
|
|
|
1
1
|
# @memberjunction/ai-systemone
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
MemberJunction drivers for decision APIs that speak TypeSafe's System One decisions format. It ships two `BaseDecision` drivers:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- **`SystemOneDecision`**, for any server that answers `POST {base}/v1/systemone` (below);
|
|
6
|
+
- **`PerplexityDecision`**, for Perplexity's Decisions API and its `pplx-decider-v1-27b` model (see [Perplexity's Decider](#perplexitydecision-perplexitys-decider)).
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
`SystemOneDecision` covers three kinds of server:
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
| Server | How you run it | Auth |
|
|
11
|
+
|---|---|---|
|
|
12
|
+
| **Kev's own server** | `modal deploy kev_serve.py` gives `https://<workspace>--kev-api.modal.run`. One deployment serves one Kev size; it scales to zero, and the first request after 5 idle minutes waits 35 to 55 seconds for a container. | A bearer token when `KEV_API_KEY` is set on the server |
|
|
13
|
+
| **llama.cpp's `llama-server`** | `llama-server -hf ggml-org/Kev-4B-GGUF` (other sizes exist), on any machine, cloud VM or Hugging Face Inference Endpoint | Open by default; a bearer token with `--api-key` |
|
|
14
|
+
| **TypeSafe's own API** | Hosted by TypeSafe | A bearer token |
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
2. Enable secure, token-less publishing from CI/CD workflows
|
|
14
|
-
3. Establish provenance for packages published under this name
|
|
16
|
+
With `llama-server`, pass `--alias <name>` (for example `--alias kev-4b`), so runs record that name as the resolved model rather than the server's GGUF file path.
|
|
17
|
+
As of 2026-10-02 no llama.cpp release includes `/v1/systemone` (release b11349 predates the merge), so build `llama-server` from source until one does.
|
|
15
18
|
|
|
16
|
-
|
|
19
|
+
The models it serves in MJ's catalog are Jared Palmer's open-weight Kev 1.0 decision models: `Kev-0.8B`, `Kev-4B`, `Kev-9B` and `Kev-27B`, each with a row on the `System One Endpoint` vendor. `Kev-4B` is also on OpenRouter, through `OpenRouterDecision` in `@memberjunction/ai-openrouter`; that row has the higher priority, so a host with an OpenRouter key uses it first.
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
## Architecture
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
```mermaid
|
|
24
|
+
graph TD
|
|
25
|
+
A["SystemOneDecision<br/>(@memberjunction/ai-systemone)"] -->|extends| B["BaseSystemOneDecision<br/>(@memberjunction/ai)"]
|
|
26
|
+
C["CloudflareDecision<br/>(@memberjunction/ai-cloudflare)"] -->|extends| B
|
|
27
|
+
D["OpenRouterDecision<br/>(@memberjunction/ai-openrouter)"] -->|extends| B
|
|
28
|
+
P["PerplexityDecision<br/>(@memberjunction/ai-systemone)"] -->|extends| B
|
|
29
|
+
B -->|extends| E["BaseDecision<br/>(@memberjunction/ai)"]
|
|
30
|
+
A -->|POST /v1/systemone| F["Kev on Modal · llama-server · TypeSafe"]
|
|
31
|
+
P -->|POST /v1/decisions| G["Perplexity Decisions API<br/>(pplx-decider-v1-27b)"]
|
|
32
|
+
```
|
|
21
33
|
|
|
22
|
-
|
|
34
|
+
`BaseSystemOneDecision` owns the System One request and answer mapping. `SystemOneDecision` adds the endpoint, the optional token and error messages that name what the server said. `PerplexityDecision` adds Perplexity's endpoint, the key check and Perplexity's error messages.
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
2. Configure the trusted publisher (e.g., GitHub Actions)
|
|
26
|
-
3. Specify the repository and workflow that should be allowed to publish
|
|
27
|
-
4. Use the configured workflow to publish your actual package
|
|
36
|
+
## Configuration
|
|
28
37
|
|
|
29
|
-
|
|
38
|
+
**The endpoint** is read in this order:
|
|
30
39
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- Should not be installed as a dependency
|
|
35
|
-
- Exists only for administrative purposes
|
|
40
|
+
1. the constructor's second argument;
|
|
41
|
+
2. the credential's `endpoint`: bind an AI Credential of type **API Key with Endpoint** to the model's `System One Endpoint` row;
|
|
42
|
+
3. the `SYSTEMONE_BASE_URL` environment variable.
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
`/v1/systemone` is appended unless the URL already ends with it (a URL ending in `/v1` gets `/systemone`). With no endpoint, the call fails before any request with a `NoCredentials` error naming both options. It allows failover, so an unconfigured Kev size does not stop the runner from trying the next decision model, including another Kev size with its own endpoint.
|
|
38
45
|
|
|
39
|
-
|
|
40
|
-
- [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
|
|
41
|
-
- [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
|
|
46
|
+
**One server per model.** A Kev deployment serves exactly one size, and every Kev size's row shares the one `System One Endpoint` vendor, so bind each credential to a **model-vendor row**: one `API Key with Endpoint` credential on `Kev-4B`'s `System One Endpoint` row, another on `Kev-27B`'s. The runner looks for a binding on the prompt-model row first, then the model-vendor row, then the vendor, then a default credential of the vendor's type, then the legacy environment variable.
|
|
42
47
|
|
|
43
|
-
|
|
48
|
+
> **Do not bind a Kev endpoint at the vendor level, or as the type's default credential, or through `SYSTEMONE_BASE_URL` alone, unless you run one Kev size only.** Each of those resolves for *every* Kev row, so every size would be sent to the same server, and the runs would be recorded against sizes that never answered. The server cannot tell you: Kev's server accepts the same model names (`kev-latest`, `jev-latest`) whatever size it serves, and echoes the name it was sent. If you do run one size only, deactivate the other sizes' `System One Endpoint` rows.
|
|
44
49
|
|
|
45
|
-
**
|
|
50
|
+
**The token** is optional. With one, the request carries `Authorization: Bearer <token>`. With none, it carries no `Authorization` header, because a local `llama-server` or Kev server is open by default. Leave the credential's `apiKey` empty for an open server.
|
|
51
|
+
|
|
52
|
+
**Legacy environment variables.** `AIDecisionRunner` only selects a candidate it has a credential for, so an environment-only setup needs the key variable too, even for an open server (an open server ignores the header):
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
SYSTEMONE_BASE_URL=https://acme--kev-api.modal.run
|
|
56
|
+
AI_VENDOR_API_KEY__SYSTEMONEDECISION=your-kev-api-key # any placeholder for an open server
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**The model.** The body's `model` is the model-vendor row's `APIName`, `kev-latest` by default. Kev's server accepts `kev-latest` and `jev-latest`; one deployment serves whichever Kev size it was started with.
|
|
60
|
+
|
|
61
|
+
**Timeouts.** The driver sets none of its own, so a cold start is not cut short. The caller's cancellation and timeout (`AIDecisionParams.cancellationToken`, `timeoutMS`) apply.
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { SystemOneDecision } from '@memberjunction/ai-systemone';
|
|
67
|
+
|
|
68
|
+
// An open llama-server on this machine.
|
|
69
|
+
const kev = new SystemOneDecision('', 'http://127.0.0.1:8080');
|
|
70
|
+
const result = await kev.Decide({
|
|
71
|
+
Model: 'kev-latest',
|
|
72
|
+
State: 'Checkout has been failing for every customer for the last hour.',
|
|
73
|
+
Questions: {
|
|
74
|
+
urgent: { Kind: 'Likelihood', Instructions: 'Is this support request urgent?' },
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
// result.Answers.urgent → { Kind: 'Likelihood', Probability: 0.87 }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Through `AIDecisionRunner`, name the model and the vendor:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
params.override = { modelId: kev4b.ID, vendorId: systemOneEndpoint.ID };
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The shipped `Default Decision` prompt does not bind any Kev model.
|
|
87
|
+
|
|
88
|
+
**As a fallback.** Like every active `Decision` model, each Kev size joins the power-matched fallback candidates of any Decision prompt that does not set `RequireSpecificModels`, ranked by closeness to the prompt's `PowerRank`, and only when its credential resolves. For the shipped `Default Decision` that means after Jev and `LLM Decision`. `Default Decision` sets no `FailoverStrategy`, so it has the column default, `SameModelDifferentVendor`, and failover is on. `AIDecisionRunner` reads any strategy but `None` as "walk the whole candidate list": it moves from model to model, not only between vendors of one model, and skips candidates with no credential. So a `Default Decision` call reaches a Kev size only when every credentialed candidate ahead of it has failed with an error that allows failover. `LLM Decision` needs no credential, so in practice that means `LLM Decision` has failed too. A `401` stops the loop before it gets that far. To keep Kev out of a prompt's fallbacks, set `RequireSpecificModels` on it. `Kev-4B`'s OpenRouter row uses the same OpenRouter key as Jev, so a host already set up for Jev has Kev-4B among those fallbacks with no further setup.
|
|
89
|
+
|
|
90
|
+
## Errors and failover
|
|
91
|
+
|
|
92
|
+
- A `429` or a `5xx` is thrown with its HTTP status, so `AIDecisionRunner` fails over to the next decision model.
|
|
93
|
+
- A missing endpoint fails before any request with a `NoCredentials` error that allows failover.
|
|
94
|
+
- A `401` is an `Authentication` error. `ErrorAnalyzer` rates it fatal, which stops the failover loop.
|
|
95
|
+
- The message names the server's own text: Kev's `detail` (a string, or a list of `{ msg }`) or llama.cpp's `error.message`.
|
|
96
|
+
- A response that cannot be mapped fails with a `ModelError` that allows failover.
|
|
97
|
+
|
|
98
|
+
## `PerplexityDecision`: Perplexity's Decider
|
|
99
|
+
|
|
100
|
+
`PerplexityDecision` calls Perplexity's Decisions API, `POST https://api.perplexity.ai/v1/decisions`, which serves Perplexity's open-weight decision model `pplx-decider-v1-27b` (Apache-2.0, released 2026-10-01, fine-tuned from Qwen3.8-27B). In MJ's catalog it is the `Perplexity Decider v1 27B` model on the `Perplexity` vendor.
|
|
101
|
+
|
|
102
|
+
That API is the model's only managed host. As of 2026-10-02 it is not on OpenRouter or any Hugging Face inference provider, and llama.cpp cannot run it. Running the weights yourself takes a CUDA GPU with about 49 GiB for the weights, plus Perplexity's own Python inference code, which has no HTTP server.
|
|
103
|
+
|
|
104
|
+
The API speaks the System One format and returns the response bare, so the driver adds only the endpoint, a check that there is a key, and Perplexity's error messages.
|
|
105
|
+
|
|
106
|
+
| | |
|
|
107
|
+
|---|---|
|
|
108
|
+
| **Endpoint** | `https://api.perplexity.ai/v1/decisions`. A URL passed to the constructor wins, then a credential's `endpoint` (an `API Key with Endpoint` credential; the vendor's `API Key` type has no endpoint field). Either is the full request URL, not a base URL as for `SystemOneDecision`. Trailing slashes are removed, because the API answers a path with one with `404`. |
|
|
109
|
+
| **Key** | A Perplexity API key, sent as `Authorization: Bearer <key>`. The API does not read `x-api-key`. |
|
|
110
|
+
| **Model** | `pplx-decider-v1-27b`, the only model the API serves. It is the row's `APIName` and the driver's default. |
|
|
111
|
+
| **Limits** | 1 to 128 questions per call, up to 255 options per Choice, up to 10 levels per Score, under 262,144 input tokens (the state and every question), and a 32 MiB body. 10 requests per second per organization. |
|
|
112
|
+
| **Price** | $0.04 per million input tokens. Output is free, and there is no per-request fee. |
|
|
113
|
+
|
|
114
|
+
**Configuration.** Bind an `API Key` AI Credential to the Decider's `Perplexity` row (or to the `Perplexity` vendor), or set the legacy variable. `AIDecisionRunner` only selects a candidate it has a credential for.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
AI_VENDOR_API_KEY__PERPLEXITYDECISION=pplx-...
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**Usage.** Name the model and the vendor through `AIDecisionRunner`:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
params.override = { modelId: perplexityDecider.ID, vendorId: perplexity.ID };
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Or call the driver directly. The state can be a string or an object:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { PerplexityDecision } from '@memberjunction/ai-systemone';
|
|
130
|
+
|
|
131
|
+
const decider = new PerplexityDecision('your-perplexity-api-key');
|
|
132
|
+
const result = await decider.Decide({
|
|
133
|
+
Model: 'pplx-decider-v1-27b',
|
|
134
|
+
State: { title: 'Battery died after two weeks', review: 'The headphones sound great, but the battery stopped charging after two weeks.' },
|
|
135
|
+
Questions: {
|
|
136
|
+
defect: { Kind: 'Likelihood', Instructions: 'Does the review report a product defect?' },
|
|
137
|
+
severity: { Kind: 'Score', Instructions: 'How severe is the reported problem?', Levels: ['Cosmetic', 'Inconvenient', 'Product unusable'] },
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
// result.Answers.defect → { Kind: 'Likelihood', Probability: 0.94 }
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**As a fallback.** The shipped `Default Decision` prompt does not bind the Decider. Like every active `Decision` model, it joins `Default Decision`'s power-matched fallbacks once its key resolves, after Jev and `LLM Decision`, exactly as the Kev sizes do (see [Usage](#usage)). Its `PowerRank` is 59, below Jev's 60, until MJ measures it. Perplexity reports 85.71% accuracy overall on 11 public benchmarks it chose, against 84.51% for Jev, with Jev ahead on 6 of the 11. Those are Perplexity's figures.
|
|
144
|
+
|
|
145
|
+
**Score probabilities.** Perplexity keys a Score answer's `probabilities` by level index (`"0"`, `"1"`, …), like its `legend`, as Jev does. MJ re-keys them by level name.
|
|
146
|
+
|
|
147
|
+
**Errors and failover.**
|
|
148
|
+
|
|
149
|
+
- A missing key fails before any request with a `NoCredentials` error that allows failover.
|
|
150
|
+
- The message names Perplexity's own `error.message`. A `404` or `405` has an empty body, and a `504` can be an HTML page; for those the message has the status and the start of the body.
|
|
151
|
+
- A `401` (a missing or invalid key, or one sent as `x-api-key`) is an `Authentication` error. `ErrorAnalyzer` rates it fatal, which stops the failover loop.
|
|
152
|
+
- A `429` (over the request or token rate) is a `RateLimit`: the runner retries it as the prompt's `MaxRetries` allows, then fails over. The runner waits `ErrorAnalyzer`'s default 30 seconds; the driver does not pass on Perplexity's `Retry-After`.
|
|
153
|
+
- A `5xx` fails over. The API returns `504` when the model has not answered after about a minute.
|
|
154
|
+
- A `400` is classified from its message. An unknown model is an `InvalidRequest`, which stops the loop.
|
|
155
|
+
- A `413` (a body over 32 MiB) is an `Unknown` error, which fails over. MJ sends no images, so its requests stay far below that size.
|
|
156
|
+
|
|
157
|
+
## Not supported yet
|
|
158
|
+
|
|
159
|
+
- **Images.** llama.cpp, Clef and Perplexity's API accept images with the state; MJ's `DecisionParams` has none, so neither driver sends them.
|
|
160
|
+
|
|
161
|
+
## Class Registration
|
|
162
|
+
|
|
163
|
+
Registered as `SystemOneDecision` via `@RegisterClass(BaseDecision, 'SystemOneDecision')`. The Kev models' `System One Endpoint` rows name it as their `DriverClass`.
|
|
164
|
+
|
|
165
|
+
Registered as `PerplexityDecision` via `@RegisterClass(BaseDecision, 'PerplexityDecision')`. The `Perplexity Decider v1 27B` model's `Perplexity` row names it as its `DriverClass`.
|
|
166
|
+
|
|
167
|
+
## Dependencies
|
|
168
|
+
|
|
169
|
+
- `@memberjunction/ai` - Core AI abstractions, including `BaseSystemOneDecision`
|
|
170
|
+
- `@memberjunction/global` - Class registration
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { BaseSystemOneDecision, SystemOneConfigurationError } from '@memberjunction/ai';
|
|
2
|
+
/**
|
|
3
|
+
* A `BaseDecision` driver for Perplexity's Decisions API, `POST https://api.perplexity.ai/v1/decisions`,
|
|
4
|
+
* which serves Perplexity's `pplx-decider-v1-27b` decision model. The API speaks the System One decisions
|
|
5
|
+
* format and returns the response bare, so the wire mapping comes from {@link BaseSystemOneDecision}
|
|
6
|
+
* unchanged; this class supplies the endpoint, the default model, the missing-key check and Perplexity's
|
|
7
|
+
* error messages.
|
|
8
|
+
*
|
|
9
|
+
* **Key.** A Perplexity API key, sent as `Authorization: Bearer <key>` (the API does not read
|
|
10
|
+
* `x-api-key`). A raw key or an `API Key` AI Credential's values in JSON (`{"apiKey":"…"}`).
|
|
11
|
+
*
|
|
12
|
+
* **Endpoint.** The constructor's URL, else the credential's `endpoint` (an `API Key with Endpoint`
|
|
13
|
+
* credential), else {@link PerplexityDecision.DEFAULT_ENDPOINT}. Each is the full request URL, unlike
|
|
14
|
+
* `SystemOneDecision`'s base URL. Trailing slashes are removed: the API answers `404` to a path with one.
|
|
15
|
+
*
|
|
16
|
+
* **Model.** `DecisionParams.Model`, the model-vendor row's `APIName`, defaulting to
|
|
17
|
+
* `pplx-decider-v1-27b`, the only model the API serves.
|
|
18
|
+
*/
|
|
19
|
+
export declare class PerplexityDecision extends BaseSystemOneDecision {
|
|
20
|
+
/** The Perplexity Decisions API endpoint. */
|
|
21
|
+
static readonly DEFAULT_ENDPOINT = "https://api.perplexity.ai/v1/decisions";
|
|
22
|
+
/** The model used when DecisionParams.Model is empty. */
|
|
23
|
+
static readonly DEFAULT_MODEL = "pplx-decider-v1-27b";
|
|
24
|
+
private _endpointURL;
|
|
25
|
+
/**
|
|
26
|
+
* @param apiKey A Perplexity API key, or an AI Credential's values in JSON.
|
|
27
|
+
* @param endpointURL Overrides the credential's `endpoint` and the default endpoint.
|
|
28
|
+
*/
|
|
29
|
+
constructor(apiKey: string, endpointURL?: string);
|
|
30
|
+
/** The configured Decisions API endpoint URL. */
|
|
31
|
+
get EndpointURL(): string;
|
|
32
|
+
protected get ServiceName(): string;
|
|
33
|
+
protected get DefaultModel(): string;
|
|
34
|
+
/** The one endpoint, whatever the model: the model travels in the body. */
|
|
35
|
+
protected GetEndpointURL(_model: string): string;
|
|
36
|
+
/**
|
|
37
|
+
* Names Perplexity's `error.message` when the body has one. A `404` or `405` has an empty body and a
|
|
38
|
+
* `504` can be an HTML page, so those keep the default: the status and the start of the body.
|
|
39
|
+
*/
|
|
40
|
+
protected DescribeHTTPError(status: number, bodyText: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* No API key stops the call before any request. It is a `NoCredentials` error that allows failover,
|
|
43
|
+
* as for the other System One drivers: another candidate may be configured.
|
|
44
|
+
*/
|
|
45
|
+
protected GetConfigurationError(): SystemOneConfigurationError | undefined;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=perplexityDecision.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"perplexityDecision.d.ts","sourceRoot":"","sources":["../../src/models/perplexityDecision.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,qBAAqB,EAGrB,2BAA2B,EAE9B,MAAM,oBAAoB,CAAC;AAG5B;;;;;;;;;;;;;;;;GAgBG;AACH,qBACa,kBAAmB,SAAQ,qBAAqB;IACzD,6CAA6C;IAC7C,gBAAuB,gBAAgB,4CAA4C;IAEnF,yDAAyD;IACzD,gBAAuB,aAAa,yBAAyB;IAE7D,OAAO,CAAC,YAAY,CAAS;IAE7B;;;OAGG;gBACS,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM;IAMhD,iDAAiD;IACjD,IAAW,WAAW,IAAI,MAAM,CAE/B;IAED,SAAS,KAAK,WAAW,IAAI,MAAM,CAElC;IAED,SAAS,KAAK,YAAY,IAAI,MAAM,CAEnC;IAED,2EAA2E;IAC3E,SAAS,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAIhD;;;OAGG;IACH,SAAS,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAKrE;;;OAGG;IACH,SAAS,CAAC,qBAAqB,IAAI,2BAA2B,GAAG,SAAS;CAS7E"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var PerplexityDecision_1;
|
|
11
|
+
import { BaseDecision, BaseSystemOneDecision, IsSystemOneWireObject, NonEmptyString, TrimTrailingSlashes, } from '@memberjunction/ai';
|
|
12
|
+
import { RegisterClass, SafeJSONParse } from '@memberjunction/global';
|
|
13
|
+
/**
|
|
14
|
+
* A `BaseDecision` driver for Perplexity's Decisions API, `POST https://api.perplexity.ai/v1/decisions`,
|
|
15
|
+
* which serves Perplexity's `pplx-decider-v1-27b` decision model. The API speaks the System One decisions
|
|
16
|
+
* format and returns the response bare, so the wire mapping comes from {@link BaseSystemOneDecision}
|
|
17
|
+
* unchanged; this class supplies the endpoint, the default model, the missing-key check and Perplexity's
|
|
18
|
+
* error messages.
|
|
19
|
+
*
|
|
20
|
+
* **Key.** A Perplexity API key, sent as `Authorization: Bearer <key>` (the API does not read
|
|
21
|
+
* `x-api-key`). A raw key or an `API Key` AI Credential's values in JSON (`{"apiKey":"…"}`).
|
|
22
|
+
*
|
|
23
|
+
* **Endpoint.** The constructor's URL, else the credential's `endpoint` (an `API Key with Endpoint`
|
|
24
|
+
* credential), else {@link PerplexityDecision.DEFAULT_ENDPOINT}. Each is the full request URL, unlike
|
|
25
|
+
* `SystemOneDecision`'s base URL. Trailing slashes are removed: the API answers `404` to a path with one.
|
|
26
|
+
*
|
|
27
|
+
* **Model.** `DecisionParams.Model`, the model-vendor row's `APIName`, defaulting to
|
|
28
|
+
* `pplx-decider-v1-27b`, the only model the API serves.
|
|
29
|
+
*/
|
|
30
|
+
let PerplexityDecision = class PerplexityDecision extends BaseSystemOneDecision {
|
|
31
|
+
static { PerplexityDecision_1 = this; }
|
|
32
|
+
/** The Perplexity Decisions API endpoint. */
|
|
33
|
+
static { this.DEFAULT_ENDPOINT = 'https://api.perplexity.ai/v1/decisions'; }
|
|
34
|
+
/** The model used when DecisionParams.Model is empty. */
|
|
35
|
+
static { this.DEFAULT_MODEL = 'pplx-decider-v1-27b'; }
|
|
36
|
+
/**
|
|
37
|
+
* @param apiKey A Perplexity API key, or an AI Credential's values in JSON.
|
|
38
|
+
* @param endpointURL Overrides the credential's `endpoint` and the default endpoint.
|
|
39
|
+
*/
|
|
40
|
+
constructor(apiKey, endpointURL) {
|
|
41
|
+
super(apiKey);
|
|
42
|
+
const endpoint = NonEmptyString(endpointURL) ?? this.Credential.Endpoint ?? PerplexityDecision_1.DEFAULT_ENDPOINT;
|
|
43
|
+
this._endpointURL = TrimTrailingSlashes(endpoint);
|
|
44
|
+
}
|
|
45
|
+
/** The configured Decisions API endpoint URL. */
|
|
46
|
+
get EndpointURL() {
|
|
47
|
+
return this._endpointURL;
|
|
48
|
+
}
|
|
49
|
+
get ServiceName() {
|
|
50
|
+
return 'Perplexity Decisions API';
|
|
51
|
+
}
|
|
52
|
+
get DefaultModel() {
|
|
53
|
+
return PerplexityDecision_1.DEFAULT_MODEL;
|
|
54
|
+
}
|
|
55
|
+
/** The one endpoint, whatever the model: the model travels in the body. */
|
|
56
|
+
GetEndpointURL(_model) {
|
|
57
|
+
return this.EndpointURL;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Names Perplexity's `error.message` when the body has one. A `404` or `405` has an empty body and a
|
|
61
|
+
* `504` can be an HTML page, so those keep the default: the status and the start of the body.
|
|
62
|
+
*/
|
|
63
|
+
DescribeHTTPError(status, bodyText) {
|
|
64
|
+
const message = perplexityErrorMessage(SafeJSONParse(bodyText));
|
|
65
|
+
return message ? `${this.ServiceName} returned HTTP ${status}: ${message}` : super.DescribeHTTPError(status, bodyText);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* No API key stops the call before any request. It is a `NoCredentials` error that allows failover,
|
|
69
|
+
* as for the other System One drivers: another candidate may be configured.
|
|
70
|
+
*/
|
|
71
|
+
GetConfigurationError() {
|
|
72
|
+
if (this.Credential.APIKey.trim().length > 0) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
ErrorType: 'NoCredentials',
|
|
77
|
+
Message: `${this.ServiceName} has no API key: bind an 'API Key' credential whose apiKey is a Perplexity API key, or set AI_VENDOR_API_KEY__PERPLEXITYDECISION`,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
PerplexityDecision = PerplexityDecision_1 = __decorate([
|
|
82
|
+
RegisterClass(BaseDecision, 'PerplexityDecision'),
|
|
83
|
+
__metadata("design:paramtypes", [String, String])
|
|
84
|
+
], PerplexityDecision);
|
|
85
|
+
export { PerplexityDecision };
|
|
86
|
+
/** The `error.message` of a Perplexity error body (`{ error: { message, type, code } }`), or an empty string. */
|
|
87
|
+
function perplexityErrorMessage(body) {
|
|
88
|
+
if (!IsSystemOneWireObject(body)) {
|
|
89
|
+
return '';
|
|
90
|
+
}
|
|
91
|
+
const error = body['error'];
|
|
92
|
+
return IsSystemOneWireObject(error) && typeof error['message'] === 'string' ? error['message'].trim() : '';
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=perplexityDecision.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"perplexityDecision.js","sourceRoot":"","sources":["../../src/models/perplexityDecision.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EACH,YAAY,EACZ,qBAAqB,EACrB,qBAAqB,EACrB,cAAc,EAEd,mBAAmB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEtE;;;;;;;;;;;;;;;;GAgBG;AAEI,IAAM,kBAAkB,GAAxB,MAAM,kBAAmB,SAAQ,qBAAqB;;IACzD,6CAA6C;aACtB,qBAAgB,GAAG,wCAAwC,AAA3C,CAA4C;IAEnF,yDAAyD;aAClC,kBAAa,GAAG,qBAAqB,AAAxB,CAAyB;IAI7D;;;OAGG;IACH,YAAY,MAAc,EAAE,WAAoB;QAC5C,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,MAAM,QAAQ,GAAG,cAAc,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,QAAQ,IAAI,oBAAkB,CAAC,gBAAgB,CAAC;QAChH,IAAI,CAAC,YAAY,GAAG,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IACtD,CAAC;IAED,iDAAiD;IACjD,IAAW,WAAW;QAClB,OAAO,IAAI,CAAC,YAAY,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EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/G,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { BaseSystemOneDecision, SystemOneConfigurationError } from '@memberjunction/ai';
|
|
2
|
+
/**
|
|
3
|
+
* A `BaseDecision` driver for any server that speaks TypeSafe's System One API, `POST {base}/v1/systemone`:
|
|
4
|
+
* - **Kev's own server**, such as a `modal deploy kev_serve.py` deployment at
|
|
5
|
+
* `https://<workspace>--kev-api.modal.run` (one deployment serves one Kev size);
|
|
6
|
+
* - **llama.cpp's `llama-server`** with a decision model, such as `llama-server -hf ggml-org/Kev-4B-GGUF`,
|
|
7
|
+
* on any machine, VM or Hugging Face Inference Endpoint;
|
|
8
|
+
* - **TypeSafe's own API**.
|
|
9
|
+
*
|
|
10
|
+
* The wire mapping comes from {@link BaseSystemOneDecision}. These servers return the response bare.
|
|
11
|
+
*
|
|
12
|
+
* **Endpoint.** The base URL comes from the constructor, else the credential's `endpoint` (an
|
|
13
|
+
* `API Key with Endpoint` AI Credential), else the `SYSTEMONE_BASE_URL` environment variable.
|
|
14
|
+
* `/v1/systemone` is appended unless the URL already ends with it (a URL ending in `/v1` gets
|
|
15
|
+
* `/systemone`). With no base URL the call fails before any request.
|
|
16
|
+
*
|
|
17
|
+
* **Token.** Optional. With an API key the request carries `Authorization: Bearer <key>`; with none it
|
|
18
|
+
* carries no Authorization header, because a local `llama-server` or Kev server is open by default.
|
|
19
|
+
*
|
|
20
|
+
* **Model.** The body's `model` is `DecisionParams.Model`, the model-vendor row's `APIName`, defaulting
|
|
21
|
+
* to `kev-latest`. Kev's server accepts `kev-latest` and `jev-latest`.
|
|
22
|
+
*
|
|
23
|
+
* **Timeouts.** The driver sets none of its own: a Kev deployment that scaled to zero takes 35 to 55
|
|
24
|
+
* seconds to start. The caller's cancellation and timeout apply.
|
|
25
|
+
*/
|
|
26
|
+
export declare class SystemOneDecision extends BaseSystemOneDecision {
|
|
27
|
+
/** The model used when DecisionParams.Model is empty. */
|
|
28
|
+
static readonly DEFAULT_MODEL = "kev-latest";
|
|
29
|
+
/** The environment variable the base URL is read from when neither the constructor nor the credential gives one. */
|
|
30
|
+
static readonly BASE_URL_ENV_VAR = "SYSTEMONE_BASE_URL";
|
|
31
|
+
private _endpointURL;
|
|
32
|
+
/**
|
|
33
|
+
* @param apiKey The bearer token, empty for an open server, or an AI Credential's values in JSON
|
|
34
|
+
* (`{"apiKey":"…","endpoint":"https://…"}`).
|
|
35
|
+
* @param baseURL Overrides the credential's `endpoint` and `SYSTEMONE_BASE_URL`.
|
|
36
|
+
*/
|
|
37
|
+
constructor(apiKey: string, baseURL?: string);
|
|
38
|
+
/** The URL requests go to, ending in `/v1/systemone`; undefined when no base URL is configured. */
|
|
39
|
+
get EndpointURL(): string | undefined;
|
|
40
|
+
protected get ServiceName(): string;
|
|
41
|
+
protected get DefaultModel(): string;
|
|
42
|
+
protected GetEndpointURL(_model: string): string;
|
|
43
|
+
/** The token as a bearer token when there is one; no Authorization header when there is none. */
|
|
44
|
+
protected GetRequestHeaders(_model: string): Record<string, string>;
|
|
45
|
+
/**
|
|
46
|
+
* Names the server's own message when the body has one: Kev's server (FastAPI) sends `detail`, as a
|
|
47
|
+
* string or a list of `{ msg }`; llama.cpp sends `error.message`. Otherwise the start of the body.
|
|
48
|
+
*/
|
|
49
|
+
protected DescribeHTTPError(status: number, bodyText: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* No base URL stops the call before any request, naming the ways to give one. It is a
|
|
52
|
+
* `NoCredentials` error that allows failover: another candidate may be configured, and the type does
|
|
53
|
+
* not exclude this vendor's other models, each of which may have its own endpoint bound.
|
|
54
|
+
*/
|
|
55
|
+
protected GetConfigurationError(): SystemOneConfigurationError | undefined;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The System One URL for a base URL: trailing slashes removed, then `/v1/systemone` appended unless the
|
|
59
|
+
* URL already ends with it, or `/systemone` when it ends in `/v1`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function ToSystemOneURL(baseURL: string): string;
|
|
62
|
+
//# sourceMappingURL=systemOneDecision.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"systemOneDecision.d.ts","sourceRoot":"","sources":["../../src/models/systemOneDecision.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,qBAAqB,EAGrB,2BAA2B,EAE9B,MAAM,oBAAoB,CAAC;AAM5B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBACa,iBAAkB,SAAQ,qBAAqB;IACxD,yDAAyD;IACzD,gBAAuB,aAAa,gBAAgB;IAEpD,oHAAoH;IACpH,gBAAuB,gBAAgB,wBAAwB;IAE/D,OAAO,CAAC,YAAY,CAAqB;IAEzC;;;;OAIG;gBACS,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM;IAM5C,mGAAmG;IACnG,IAAW,WAAW,IAAI,MAAM,GAAG,SAAS,CAE3C;IAED,SAAS,KAAK,WAAW,IAAI,MAAM,CAElC;IAED,SAAS,KAAK,YAAY,IAAI,MAAM,CAEnC;IAED,SAAS,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM;IAIhD,iGAAiG;IACjG,SAAS,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAOnE;;;OAGG;IACH,SAAS,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAKrE;;;;OAIG;IACH,SAAS,CAAC,qBAAqB,IAAI,2BAA2B,GAAG,SAAS;CAS7E;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAMtD"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var SystemOneDecision_1;
|
|
11
|
+
import { BaseDecision, BaseSystemOneDecision, IsSystemOneWireObject, NonEmptyString, TrimTrailingSlashes, } from '@memberjunction/ai';
|
|
12
|
+
import { RegisterClass, SafeJSONParse } from '@memberjunction/global';
|
|
13
|
+
/** The route every System One server answers on. */
|
|
14
|
+
const SYSTEMONE_PATH = '/v1/systemone';
|
|
15
|
+
/**
|
|
16
|
+
* A `BaseDecision` driver for any server that speaks TypeSafe's System One API, `POST {base}/v1/systemone`:
|
|
17
|
+
* - **Kev's own server**, such as a `modal deploy kev_serve.py` deployment at
|
|
18
|
+
* `https://<workspace>--kev-api.modal.run` (one deployment serves one Kev size);
|
|
19
|
+
* - **llama.cpp's `llama-server`** with a decision model, such as `llama-server -hf ggml-org/Kev-4B-GGUF`,
|
|
20
|
+
* on any machine, VM or Hugging Face Inference Endpoint;
|
|
21
|
+
* - **TypeSafe's own API**.
|
|
22
|
+
*
|
|
23
|
+
* The wire mapping comes from {@link BaseSystemOneDecision}. These servers return the response bare.
|
|
24
|
+
*
|
|
25
|
+
* **Endpoint.** The base URL comes from the constructor, else the credential's `endpoint` (an
|
|
26
|
+
* `API Key with Endpoint` AI Credential), else the `SYSTEMONE_BASE_URL` environment variable.
|
|
27
|
+
* `/v1/systemone` is appended unless the URL already ends with it (a URL ending in `/v1` gets
|
|
28
|
+
* `/systemone`). With no base URL the call fails before any request.
|
|
29
|
+
*
|
|
30
|
+
* **Token.** Optional. With an API key the request carries `Authorization: Bearer <key>`; with none it
|
|
31
|
+
* carries no Authorization header, because a local `llama-server` or Kev server is open by default.
|
|
32
|
+
*
|
|
33
|
+
* **Model.** The body's `model` is `DecisionParams.Model`, the model-vendor row's `APIName`, defaulting
|
|
34
|
+
* to `kev-latest`. Kev's server accepts `kev-latest` and `jev-latest`.
|
|
35
|
+
*
|
|
36
|
+
* **Timeouts.** The driver sets none of its own: a Kev deployment that scaled to zero takes 35 to 55
|
|
37
|
+
* seconds to start. The caller's cancellation and timeout apply.
|
|
38
|
+
*/
|
|
39
|
+
let SystemOneDecision = class SystemOneDecision extends BaseSystemOneDecision {
|
|
40
|
+
static { SystemOneDecision_1 = this; }
|
|
41
|
+
/** The model used when DecisionParams.Model is empty. */
|
|
42
|
+
static { this.DEFAULT_MODEL = 'kev-latest'; }
|
|
43
|
+
/** The environment variable the base URL is read from when neither the constructor nor the credential gives one. */
|
|
44
|
+
static { this.BASE_URL_ENV_VAR = 'SYSTEMONE_BASE_URL'; }
|
|
45
|
+
/**
|
|
46
|
+
* @param apiKey The bearer token, empty for an open server, or an AI Credential's values in JSON
|
|
47
|
+
* (`{"apiKey":"…","endpoint":"https://…"}`).
|
|
48
|
+
* @param baseURL Overrides the credential's `endpoint` and `SYSTEMONE_BASE_URL`.
|
|
49
|
+
*/
|
|
50
|
+
constructor(apiKey, baseURL) {
|
|
51
|
+
super(apiKey);
|
|
52
|
+
const base = NonEmptyString(baseURL) ?? this.Credential.Endpoint ?? NonEmptyString(process.env[SystemOneDecision_1.BASE_URL_ENV_VAR]);
|
|
53
|
+
this._endpointURL = base ? ToSystemOneURL(base) : undefined;
|
|
54
|
+
}
|
|
55
|
+
/** The URL requests go to, ending in `/v1/systemone`; undefined when no base URL is configured. */
|
|
56
|
+
get EndpointURL() {
|
|
57
|
+
return this._endpointURL;
|
|
58
|
+
}
|
|
59
|
+
get ServiceName() {
|
|
60
|
+
return 'System One endpoint';
|
|
61
|
+
}
|
|
62
|
+
get DefaultModel() {
|
|
63
|
+
return SystemOneDecision_1.DEFAULT_MODEL;
|
|
64
|
+
}
|
|
65
|
+
GetEndpointURL(_model) {
|
|
66
|
+
return this._endpointURL ?? '';
|
|
67
|
+
}
|
|
68
|
+
/** The token as a bearer token when there is one; no Authorization header when there is none. */
|
|
69
|
+
GetRequestHeaders(_model) {
|
|
70
|
+
const token = this.Credential.APIKey.trim();
|
|
71
|
+
return token.length > 0
|
|
72
|
+
? { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }
|
|
73
|
+
: { 'Content-Type': 'application/json' };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Names the server's own message when the body has one: Kev's server (FastAPI) sends `detail`, as a
|
|
77
|
+
* string or a list of `{ msg }`; llama.cpp sends `error.message`. Otherwise the start of the body.
|
|
78
|
+
*/
|
|
79
|
+
DescribeHTTPError(status, bodyText) {
|
|
80
|
+
const message = serverMessage(SafeJSONParse(bodyText));
|
|
81
|
+
return message ? `${this.ServiceName} returned HTTP ${status}: ${message}` : super.DescribeHTTPError(status, bodyText);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* No base URL stops the call before any request, naming the ways to give one. It is a
|
|
85
|
+
* `NoCredentials` error that allows failover: another candidate may be configured, and the type does
|
|
86
|
+
* not exclude this vendor's other models, each of which may have its own endpoint bound.
|
|
87
|
+
*/
|
|
88
|
+
GetConfigurationError() {
|
|
89
|
+
if (this._endpointURL) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
ErrorType: 'NoCredentials',
|
|
94
|
+
Message: `${this.ServiceName} has no base URL: bind an 'API Key with Endpoint' credential whose endpoint is the server's URL, or set ${SystemOneDecision_1.BASE_URL_ENV_VAR}`,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
SystemOneDecision = SystemOneDecision_1 = __decorate([
|
|
99
|
+
RegisterClass(BaseDecision, 'SystemOneDecision'),
|
|
100
|
+
__metadata("design:paramtypes", [String, String])
|
|
101
|
+
], SystemOneDecision);
|
|
102
|
+
export { SystemOneDecision };
|
|
103
|
+
/**
|
|
104
|
+
* The System One URL for a base URL: trailing slashes removed, then `/v1/systemone` appended unless the
|
|
105
|
+
* URL already ends with it, or `/systemone` when it ends in `/v1`.
|
|
106
|
+
*/
|
|
107
|
+
export function ToSystemOneURL(baseURL) {
|
|
108
|
+
const base = TrimTrailingSlashes(baseURL.trim());
|
|
109
|
+
if (base.endsWith(SYSTEMONE_PATH)) {
|
|
110
|
+
return base;
|
|
111
|
+
}
|
|
112
|
+
return base.endsWith('/v1') ? `${base}/systemone` : `${base}${SYSTEMONE_PATH}`;
|
|
113
|
+
}
|
|
114
|
+
/** The error message a System One server put in its body, or an empty string. */
|
|
115
|
+
function serverMessage(body) {
|
|
116
|
+
if (!IsSystemOneWireObject(body)) {
|
|
117
|
+
return '';
|
|
118
|
+
}
|
|
119
|
+
const detail = body['detail'];
|
|
120
|
+
if (typeof detail === 'string') {
|
|
121
|
+
return detail.trim();
|
|
122
|
+
}
|
|
123
|
+
if (Array.isArray(detail)) {
|
|
124
|
+
return detail
|
|
125
|
+
.map(d => (IsSystemOneWireObject(d) && typeof d['msg'] === 'string' ? d['msg'].trim() : ''))
|
|
126
|
+
.filter(m => m.length > 0)
|
|
127
|
+
.join('; ');
|
|
128
|
+
}
|
|
129
|
+
const error = body['error'];
|
|
130
|
+
if (IsSystemOneWireObject(error) && typeof error['message'] === 'string') {
|
|
131
|
+
return error['message'].trim();
|
|
132
|
+
}
|
|
133
|
+
return typeof error === 'string' ? error.trim() : '';
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=systemOneDecision.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"systemOneDecision.js","sourceRoot":"","sources":["../../src/models/systemOneDecision.ts"],"names":[],"mappings":";;;;;;;;;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
"type": "module",
|
|
4
|
+
"version": "6.2.0-edge.4",
|
|
5
|
+
"description": "MemberJunction drivers for System One decision APIs: any server with POST /v1/systemone (Kev on Modal, llama.cpp llama-server, TypeSafe), and Perplexity's Decisions API (pplx-decider-v1-27b)",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"/dist"
|
|
10
|
+
],
|
|
11
|
+
"author": "MemberJunction.com",
|
|
12
|
+
"license": "BUSL-1.1",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"ts-node-dev": "^2.0.0",
|
|
15
|
+
"typescript": "^5.9.3"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@memberjunction/ai": "6.2.0-edge.4",
|
|
19
|
+
"@memberjunction/global": "6.2.0-edge.4"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/MemberJunction/MJ"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"start": "ts-node-dev src/index.ts",
|
|
27
|
+
"build": "tsc && tsc-alias -f",
|
|
28
|
+
"test": "vitest run"
|
|
29
|
+
}
|
|
30
|
+
}
|