llmasaservice-client 0.2.5 → 0.4.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,5 +1,17 @@
1
1
  # llmasaservice-client
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Added the ability to disable cache for some calls on send
8
+
9
+ ## 0.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - Final onComplete and onError callbacks added to the send method
14
+
3
15
  ## 0.2.5
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -91,7 +91,7 @@ var useLLM = (options) => {
91
91
  setIdle(true);
92
92
  };
93
93
  function send(_0) {
94
- return __async(this, arguments, function* (prompt, messages = [], stream = true, abortController = new AbortController(), service = null, onCompleteCallback) {
94
+ return __async(this, arguments, function* (prompt, messages = [], stream = true, allowCaching = true, service = null, abortController = new AbortController(), onComplete, onError) {
95
95
  var _a, _b, _c, _d, _e;
96
96
  setResponse("");
97
97
  setIdle(false);
@@ -101,8 +101,9 @@ var useLLM = (options) => {
101
101
  serviceId: service,
102
102
  prompt,
103
103
  messages,
104
- customer: (_b = context == null ? void 0 : context.customer) != null ? _b : {}
104
+ customer: (_b = context == null ? void 0 : context.customer) != null ? _b : {},
105
105
  // if no customer, use the projectId as the customer_id
106
+ allowCaching
106
107
  });
107
108
  const options2 = {
108
109
  method: "POST",
@@ -124,13 +125,27 @@ var useLLM = (options) => {
124
125
  const decoder = new TextDecoder("utf-8");
125
126
  setIdle(false);
126
127
  if (!stream) {
127
- return yield readStream(reader, decoder, stream, {
128
- signal: options2.signal
129
- }, onCompleteCallback);
128
+ return yield readStream(
129
+ reader,
130
+ decoder,
131
+ stream,
132
+ {
133
+ signal: options2.signal
134
+ },
135
+ onComplete,
136
+ onError
137
+ );
130
138
  } else {
131
- readStream(reader, decoder, stream, {
132
- signal: options2.signal
133
- }, onCompleteCallback);
139
+ readStream(
140
+ reader,
141
+ decoder,
142
+ stream,
143
+ {
144
+ signal: options2.signal
145
+ },
146
+ onComplete,
147
+ onError
148
+ );
134
149
  return reader;
135
150
  }
136
151
  }
@@ -139,12 +154,15 @@ var useLLM = (options) => {
139
154
  }
140
155
  if (errorInFetch !== "") {
141
156
  setError(errorInFetch);
157
+ if (onError) {
158
+ onError(errorInFetch);
159
+ }
142
160
  console.error(`Error: Error in fetch. (${errorInFetch})`);
143
161
  }
144
162
  });
145
163
  }
146
164
  function readStream(_0, _1) {
147
- return __async(this, arguments, function* (reader, decoder, stream = true, { signal }, onCompleteCallback) {
165
+ return __async(this, arguments, function* (reader, decoder, stream = true, { signal }, onComplete, onError) {
148
166
  let errorInRead = "";
149
167
  let result = "";
150
168
  while (true) {
@@ -180,12 +198,11 @@ var useLLM = (options) => {
180
198
  if (errorInRead !== "") {
181
199
  setError(errorInRead);
182
200
  reader.cancel();
201
+ if (onError) onError(errorInRead);
183
202
  setIdle(true);
184
203
  }
185
- console.log("about to call callback", onCompleteCallback);
186
- if (onCompleteCallback) {
187
- onCompleteCallback(result);
188
- console.log("called callback");
204
+ if (onComplete) {
205
+ onComplete(result);
189
206
  }
190
207
  return result;
191
208
  });
package/dist/index.mjs CHANGED
@@ -54,7 +54,7 @@ var useLLM = (options) => {
54
54
  setIdle(true);
55
55
  };
56
56
  function send(_0) {
57
- return __async(this, arguments, function* (prompt, messages = [], stream = true, abortController = new AbortController(), service = null, onCompleteCallback) {
57
+ return __async(this, arguments, function* (prompt, messages = [], stream = true, allowCaching = true, service = null, abortController = new AbortController(), onComplete, onError) {
58
58
  var _a, _b, _c, _d, _e;
59
59
  setResponse("");
60
60
  setIdle(false);
@@ -64,8 +64,9 @@ var useLLM = (options) => {
64
64
  serviceId: service,
65
65
  prompt,
66
66
  messages,
67
- customer: (_b = context == null ? void 0 : context.customer) != null ? _b : {}
67
+ customer: (_b = context == null ? void 0 : context.customer) != null ? _b : {},
68
68
  // if no customer, use the projectId as the customer_id
69
+ allowCaching
69
70
  });
70
71
  const options2 = {
71
72
  method: "POST",
@@ -87,13 +88,27 @@ var useLLM = (options) => {
87
88
  const decoder = new TextDecoder("utf-8");
88
89
  setIdle(false);
89
90
  if (!stream) {
90
- return yield readStream(reader, decoder, stream, {
91
- signal: options2.signal
92
- }, onCompleteCallback);
91
+ return yield readStream(
92
+ reader,
93
+ decoder,
94
+ stream,
95
+ {
96
+ signal: options2.signal
97
+ },
98
+ onComplete,
99
+ onError
100
+ );
93
101
  } else {
94
- readStream(reader, decoder, stream, {
95
- signal: options2.signal
96
- }, onCompleteCallback);
102
+ readStream(
103
+ reader,
104
+ decoder,
105
+ stream,
106
+ {
107
+ signal: options2.signal
108
+ },
109
+ onComplete,
110
+ onError
111
+ );
97
112
  return reader;
98
113
  }
99
114
  }
@@ -102,12 +117,15 @@ var useLLM = (options) => {
102
117
  }
103
118
  if (errorInFetch !== "") {
104
119
  setError(errorInFetch);
120
+ if (onError) {
121
+ onError(errorInFetch);
122
+ }
105
123
  console.error(`Error: Error in fetch. (${errorInFetch})`);
106
124
  }
107
125
  });
108
126
  }
109
127
  function readStream(_0, _1) {
110
- return __async(this, arguments, function* (reader, decoder, stream = true, { signal }, onCompleteCallback) {
128
+ return __async(this, arguments, function* (reader, decoder, stream = true, { signal }, onComplete, onError) {
111
129
  let errorInRead = "";
112
130
  let result = "";
113
131
  while (true) {
@@ -143,12 +161,11 @@ var useLLM = (options) => {
143
161
  if (errorInRead !== "") {
144
162
  setError(errorInRead);
145
163
  reader.cancel();
164
+ if (onError) onError(errorInRead);
146
165
  setIdle(true);
147
166
  }
148
- console.log("about to call callback", onCompleteCallback);
149
- if (onCompleteCallback) {
150
- onCompleteCallback(result);
151
- console.log("called callback");
167
+ if (onComplete) {
168
+ onComplete(result);
152
169
  }
153
170
  return result;
154
171
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "llmasaservice-client",
3
3
  "license": "MIT",
4
- "version": "0.2.5",
4
+ "version": "0.4.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
package/src/useLLM.ts CHANGED
@@ -44,18 +44,22 @@ export const useLLM = (options?: LLMServiceType): UseLLMReturnType => {
44
44
  * @param prompt The prompt to send the the LLM service.
45
45
  * @param messages The history and context messages to send to the LLM service. as an array of {role: string, content: string} objects. for example, [{ role: "system", content: "You are a useful assistant." }]
46
46
  * @param stream Determines whether to stream results back in the response property as they return from the service or batch them up and return them all at once in the response property as a string.
47
- * @param abortController The AbortController used to abort this request once its started. This allows you to add a stop button to your UI.
47
+ * @param allowCaching Determines whether the service can use cached results or not.
48
48
  * @param service The service to use for the request. If null, load balancing will be applied. This is typically only used for testing.
49
- * @param onCompleteCallback The callback function to be called once the stream completes, with the final result string.
49
+ * @param abortController The AbortController used to abort this request once its started. This allows you to add a stop button to your UI.
50
+ * @param onComplete The callback function to be called once the stream completes, with the final result string.
51
+ * @param onError The callback function to be called if an error occurs, with the error string.
50
52
  * @returns a StreamReader object if stream is true, otherwise a string of the response. Typically this isn't used when streaming, the stream is exposed in the response property.
51
53
  */
52
54
  async function send(
53
55
  prompt: string,
54
56
  messages = [],
55
57
  stream: boolean = true,
56
- abortController: AbortController = new AbortController(),
58
+ allowCaching: boolean = true,
57
59
  service: string | null = null, // null means use the default service and apply services load balancing
58
- onCompleteCallback?: (result: string) => void
60
+ abortController: AbortController = new AbortController(),
61
+ onComplete?: (result: string) => void,
62
+ onError?: (error: string) => void
59
63
  ): Promise<ReadableStreamDefaultReader<any> | string | undefined> {
60
64
  setResponse("");
61
65
  setIdle(false);
@@ -68,6 +72,7 @@ export const useLLM = (options?: LLMServiceType): UseLLMReturnType => {
68
72
  prompt: prompt,
69
73
  messages: messages,
70
74
  customer: context?.customer ?? {}, // if no customer, use the projectId as the customer_id
75
+ allowCaching: allowCaching,
71
76
  });
72
77
 
73
78
  // trying to get cloudfront oac going. posts need to be signed, but when i add this the call fails...
@@ -94,13 +99,27 @@ export const useLLM = (options?: LLMServiceType): UseLLMReturnType => {
94
99
  setIdle(false);
95
100
 
96
101
  if (!stream) {
97
- return await readStream(reader, decoder, stream, {
98
- signal: options.signal,
99
- }, onCompleteCallback);
102
+ return await readStream(
103
+ reader,
104
+ decoder,
105
+ stream,
106
+ {
107
+ signal: options.signal,
108
+ },
109
+ onComplete,
110
+ onError
111
+ );
100
112
  } else {
101
- readStream(reader, decoder, stream, {
102
- signal: options.signal,
103
- }, onCompleteCallback);
113
+ readStream(
114
+ reader,
115
+ decoder,
116
+ stream,
117
+ {
118
+ signal: options.signal,
119
+ },
120
+ onComplete,
121
+ onError
122
+ );
104
123
 
105
124
  return reader;
106
125
  }
@@ -111,6 +130,9 @@ export const useLLM = (options?: LLMServiceType): UseLLMReturnType => {
111
130
 
112
131
  if (errorInFetch !== "") {
113
132
  setError(errorInFetch);
133
+ if (onError) {
134
+ onError(errorInFetch);
135
+ }
114
136
  console.error(`Error: Error in fetch. (${errorInFetch})`);
115
137
  }
116
138
  }
@@ -120,7 +142,8 @@ export const useLLM = (options?: LLMServiceType): UseLLMReturnType => {
120
142
  decoder: TextDecoder,
121
143
  stream: Boolean = true,
122
144
  { signal: signal }: { signal: AbortSignal },
123
- onCompleteCallback?: (result: string) => void
145
+ onComplete?: (result: string) => void,
146
+ onError?: (error: string) => void
124
147
  ): Promise<string> {
125
148
  let errorInRead = "";
126
149
  let result = "";
@@ -168,13 +191,12 @@ export const useLLM = (options?: LLMServiceType): UseLLMReturnType => {
168
191
  if (errorInRead !== "") {
169
192
  setError(errorInRead);
170
193
  reader.cancel();
194
+ if (onError) onError(errorInRead);
171
195
  setIdle(true);
172
196
  }
173
197
 
174
- console.log("about to call callback", onCompleteCallback);
175
- if (onCompleteCallback) {
176
- onCompleteCallback(result);
177
- console.log("called callback");
198
+ if (onComplete) {
199
+ onComplete(result);
178
200
  }
179
201
 
180
202
  return result;