@adobe/spacecat-shared-gpt-client 1.1.8 → 1.2.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-gpt-client-v1.2.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-gpt-client-v1.1.9...@adobe/spacecat-shared-gpt-client-v1.2.0) (2024-05-08)
2
+
3
+
4
+ ### Features
5
+
6
+ * use ims-client for auth ([#226](https://github.com/adobe/spacecat-shared/issues/226)) ([2813ecc](https://github.com/adobe/spacecat-shared/commit/2813ecc7b4e7ca5de8b7c7770d6ff3ee772db1b8))
7
+
8
+ # [@adobe/spacecat-shared-gpt-client-v1.1.9](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-gpt-client-v1.1.8...@adobe/spacecat-shared-gpt-client-v1.1.9) (2024-04-17)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **deps:** update dependency @adobe/helix-universal to v4.5.2 ([#211](https://github.com/adobe/spacecat-shared/issues/211)) ([e261e67](https://github.com/adobe/spacecat-shared/commit/e261e677558a6ee165cf0d560625cf665276e144))
14
+
1
15
  # [@adobe/spacecat-shared-gpt-client-v1.1.8](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-gpt-client-v1.1.7...@adobe/spacecat-shared-gpt-client-v1.1.8) (2024-04-16)
2
16
 
3
17
 
package/README.md CHANGED
@@ -7,12 +7,18 @@ The `FirefallClient` library offers a streamlined way to interact with the Firef
7
7
  To use the `FirefallClient`, you need to configure it with the following parameters:
8
8
 
9
9
  - `FIREFALL_API_ENDPOINT`: The endpoint URL for the Firefall API.
10
- - `FIREFALL_IMS_ORG`: Your IMS organization ID for Firefall.
11
10
  - `FIREFALL_API_KEY`: Your API key for accessing the Firefall API.
12
- - `FIREFALL_API_AUTH`: Your Bearer authorization token for the Firefall API.
11
+ - `FIREFALL_API_CAPABILITY_NAME`: The capability name for the Firefall API.
13
12
 
14
13
  These parameters can be set through environment variables or passed directly to the `FirefallClient.createFrom` method.
15
14
 
15
+ Additionally, the configuration for the `@adobe/spacecat-shared-ims-client` library is required to fetch the service access token from the IMS API:
16
+
17
+ - `IMS_HOST`: The hostname of the IMS API.
18
+ - `IMS_CLIENT_ID`: Your IMS client ID.
19
+ - `IMS_CLIENT_CODE`: Your IMS client code, used for authentication.
20
+ - `IMS_CLIENT_SECRET`: Your IMS client secret, used for authentication.
21
+
16
22
  ## Usage Examples
17
23
 
18
24
  ### Instantiating the Firefall Client
@@ -42,9 +48,12 @@ async function fetchInsights(prompt) {
42
48
  const client = FirefallClient.createFrom({
43
49
  env: {
44
50
  FIREFALL_API_ENDPOINT: 'https://api.firefall.example.com',
45
- FIREFALL_IMS_ORG: 'yourImsOrgId',
46
51
  FIREFALL_API_KEY: 'yourApiKey',
47
- FIREFALL_API_AUTH: 'yourApiAuth',
52
+ FIREFALL_API_CAPABILITY_NAME: 'yourCapabilityName',
53
+ IMS_HOST: 'ims.example.com',
54
+ IMS_CLIENT_ID: 'yourClientId',
55
+ IMS_CLIENT_CODE: 'yourClientCode',
56
+ IMS_CLIENT_SECRET: 'yourClientSecret',
48
57
  },
49
58
  log: console,
50
59
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-gpt-client",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "description": "Shared modules of the Spacecat Services - GPT Client",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -31,7 +31,8 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@adobe/fetch": "4.1.2",
34
- "@adobe/helix-universal": "4.5.1",
34
+ "@adobe/helix-universal": "4.5.2",
35
+ "@adobe/spacecat-shared-ims-client": "1.3.4",
35
36
  "@adobe/spacecat-shared-utils": "1.7.2"
36
37
  },
37
38
  "devDependencies": {
@@ -11,6 +11,7 @@
11
11
  */
12
12
 
13
13
  import { createUrl } from '@adobe/fetch';
14
+ import { ImsClient } from '@adobe/spacecat-shared-ims-client';
14
15
  import { hasText, isObject, isValidUrl } from '@adobe/spacecat-shared-utils';
15
16
 
16
17
  import { fetch as httpFetch } from '../utils.js';
@@ -27,11 +28,12 @@ function validateFirefallResponse(response) {
27
28
  export default class FirefallClient {
28
29
  static createFrom(context) {
29
30
  const { log = console } = context;
31
+ const imsClient = ImsClient.createFrom(context);
32
+
30
33
  const {
31
34
  FIREFALL_API_ENDPOINT: apiEndpoint,
32
- FIREFALL_IMS_ORG: imsOrg,
35
+ IMS_CLIENT_ID: imsOrg,
33
36
  FIREFALL_API_KEY: apiKey,
34
- FIREFALL_API_AUTH: apiAuth,
35
37
  FIREFALL_API_POLL_INTERVAL: pollInterval = 2000,
36
38
  FIREFALL_API_CAPABILITY_NAME: capabilityName = 'gpt4_32k_completions_capability',
37
39
  } = context.env;
@@ -40,23 +42,15 @@ export default class FirefallClient {
40
42
  throw new Error('Missing Firefall API endpoint');
41
43
  }
42
44
 
43
- if (!hasText(imsOrg)) {
44
- throw new Error('Missing Firefall IMS Org');
45
- }
46
-
47
45
  if (!hasText(apiKey)) {
48
46
  throw new Error('Missing Firefall API key');
49
47
  }
50
48
 
51
- if (!hasText(apiAuth)) {
52
- throw new Error('Missing Firefall API auth');
53
- }
54
-
55
49
  return new FirefallClient({
56
- apiAuth,
57
50
  apiEndpoint,
58
51
  apiKey,
59
52
  capabilityName,
53
+ imsClient,
60
54
  imsOrg,
61
55
  pollInterval,
62
56
  }, log);
@@ -66,10 +60,10 @@ export default class FirefallClient {
66
60
  * Creates a new Firefall client
67
61
  *
68
62
  * @param {Object} config - The configuration object.
69
- * @param {string} config.apiAuth - The Bearer authorization token for Firefall.
70
63
  * @param {string} config.apiEndpoint - The API endpoint for Firefall.
71
64
  * @param {string} config.apiKey - The API Key for Firefall.
72
65
  * @param {string} config.capabilityName - The capability name for Firefall.
66
+ * @param {ImsClient} config.imsClient - The IMS Client.
73
67
  * @param {string} config.imsOrg - The IMS Org for Firefall.
74
68
  * @param {number} config.pollInterval - The interval to poll for job status.
75
69
  * @param {Object} log - The Logger.
@@ -78,6 +72,15 @@ export default class FirefallClient {
78
72
  constructor(config, log) {
79
73
  this.config = config;
80
74
  this.log = log;
75
+ this.imsClient = config.imsClient;
76
+ this.apiAuth = null;
77
+ }
78
+
79
+ async #getApiAuth() {
80
+ if (!this.apiAuth) {
81
+ this.apiAuth = await this.imsClient.getServiceAccessToken();
82
+ }
83
+ return this.apiAuth;
81
84
  }
82
85
 
83
86
  #logDuration(message, startTime) {
@@ -87,6 +90,8 @@ export default class FirefallClient {
87
90
  }
88
91
 
89
92
  async #submitJob(prompt) {
93
+ const apiAuth = await this.#getApiAuth();
94
+
90
95
  const body = JSON.stringify({
91
96
  input: prompt,
92
97
  capability_name: this.config.capabilityName,
@@ -97,7 +102,7 @@ export default class FirefallClient {
97
102
  method: 'POST',
98
103
  headers: {
99
104
  'Content-Type': 'application/json',
100
- Authorization: `Bearer ${this.config.apiAuth}`,
105
+ Authorization: `Bearer ${apiAuth}`,
101
106
  'x-api-key': this.config.apiKey,
102
107
  'x-gw-ims-org-id': this.config.imsOrg,
103
108
  },
@@ -113,6 +118,8 @@ export default class FirefallClient {
113
118
 
114
119
  /* eslint-disable no-await-in-loop */
115
120
  async #pollJobStatus(jobId) {
121
+ const apiAuth = await this.#getApiAuth();
122
+
116
123
  let jobStatusResponse;
117
124
  do {
118
125
  await new Promise(
@@ -124,7 +131,7 @@ export default class FirefallClient {
124
131
  {
125
132
  method: 'GET',
126
133
  headers: {
127
- Authorization: `Bearer ${this.config.apiAuth}`,
134
+ Authorization: `Bearer ${apiAuth}`,
128
135
  'x-api-key': this.config.apiKey,
129
136
  'x-gw-ims-org-id': this.config.imsOrg,
130
137
  },