llmasaservice-client 0.0.1
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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/README.md +1 -0
- package/dist/index.d.mts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +186 -0
- package/dist/index.mjs +149 -0
- package/index.ts +2 -0
- package/package.json +26 -0
- package/src/LLMAsAService.tsx +35 -0
- package/src/useLLM.ts +165 -0
- package/tsconfig.json +108 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changesets
|
|
2
|
+
|
|
3
|
+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
|
|
4
|
+
with multi-package repos, or single-package repos to help you version and publish your code. You can
|
|
5
|
+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
|
|
6
|
+
|
|
7
|
+
We have a quick list of common questions to get you started engaging with this project in
|
|
8
|
+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://unpkg.com/@changesets/config@3.0.2/schema.json",
|
|
3
|
+
"changelog": "@changesets/cli/changelog",
|
|
4
|
+
"commit": false,
|
|
5
|
+
"fixed": [],
|
|
6
|
+
"linked": [],
|
|
7
|
+
"access": "restricted",
|
|
8
|
+
"baseBranch": "master",
|
|
9
|
+
"updateInternalDependencies": "patch",
|
|
10
|
+
"ignore": []
|
|
11
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# LLMAsAService.io for React Clients
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type LLMAsAServiceCustomer = {
|
|
4
|
+
customer_id: string;
|
|
5
|
+
customer_name?: string;
|
|
6
|
+
customer_user_id?: string;
|
|
7
|
+
customer_user_email?: string;
|
|
8
|
+
};
|
|
9
|
+
interface LLMServiceType {
|
|
10
|
+
project_id: string | undefined;
|
|
11
|
+
customer: LLMAsAServiceCustomer;
|
|
12
|
+
}
|
|
13
|
+
declare const LLMService: React.Context<LLMServiceType | undefined>;
|
|
14
|
+
|
|
15
|
+
declare const useLLM: (options?: LLMServiceType) => {
|
|
16
|
+
response: string;
|
|
17
|
+
send: (prompt: string, messages?: {
|
|
18
|
+
role: string;
|
|
19
|
+
content: string;
|
|
20
|
+
}[], stream?: boolean, abortController?: AbortController, service?: string | null) => Promise<ReadableStreamDefaultReader<any> | string | undefined>;
|
|
21
|
+
stop: (controller: AbortController | null) => void;
|
|
22
|
+
idle: boolean;
|
|
23
|
+
error: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { LLMService as LLMAsAService, useLLM };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type LLMAsAServiceCustomer = {
|
|
4
|
+
customer_id: string;
|
|
5
|
+
customer_name?: string;
|
|
6
|
+
customer_user_id?: string;
|
|
7
|
+
customer_user_email?: string;
|
|
8
|
+
};
|
|
9
|
+
interface LLMServiceType {
|
|
10
|
+
project_id: string | undefined;
|
|
11
|
+
customer: LLMAsAServiceCustomer;
|
|
12
|
+
}
|
|
13
|
+
declare const LLMService: React.Context<LLMServiceType | undefined>;
|
|
14
|
+
|
|
15
|
+
declare const useLLM: (options?: LLMServiceType) => {
|
|
16
|
+
response: string;
|
|
17
|
+
send: (prompt: string, messages?: {
|
|
18
|
+
role: string;
|
|
19
|
+
content: string;
|
|
20
|
+
}[], stream?: boolean, abortController?: AbortController, service?: string | null) => Promise<ReadableStreamDefaultReader<any> | string | undefined>;
|
|
21
|
+
stop: (controller: AbortController | null) => void;
|
|
22
|
+
idle: boolean;
|
|
23
|
+
error: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { LLMService as LLMAsAService, useLLM };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// index.ts
|
|
51
|
+
var useLLM_exports = {};
|
|
52
|
+
__export(useLLM_exports, {
|
|
53
|
+
LLMAsAService: () => LLMAsAService_default,
|
|
54
|
+
useLLM: () => useLLM_default
|
|
55
|
+
});
|
|
56
|
+
module.exports = __toCommonJS(useLLM_exports);
|
|
57
|
+
|
|
58
|
+
// src/useLLM.ts
|
|
59
|
+
var import_react2 = require("react");
|
|
60
|
+
|
|
61
|
+
// src/LLMAsAService.tsx
|
|
62
|
+
var import_react = __toESM(require("react"));
|
|
63
|
+
var LLMService = (0, import_react.createContext)(void 0);
|
|
64
|
+
var LLMAsAService_default = LLMService;
|
|
65
|
+
|
|
66
|
+
// src/useLLM.ts
|
|
67
|
+
var useLLM = (options) => {
|
|
68
|
+
var _a;
|
|
69
|
+
const [response, setResponse] = (0, import_react2.useState)("");
|
|
70
|
+
const [idle, setIdle] = (0, import_react2.useState)(true);
|
|
71
|
+
const [error, setError] = (0, import_react2.useState)("");
|
|
72
|
+
const context = (_a = (0, import_react2.useContext)(LLMService)) != null ? _a : options;
|
|
73
|
+
if (!context) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
"useLLM must be used within a LLMServiceProvider or constructed with options in your useLLM() call."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
const stop = (controller) => {
|
|
79
|
+
if (controller) controller.abort();
|
|
80
|
+
setIdle(true);
|
|
81
|
+
};
|
|
82
|
+
function send(_0) {
|
|
83
|
+
return __async(this, arguments, function* (prompt, messages = [{ role: "system", content: "You are a useful assistant." }], stream = true, abortController = new AbortController(), service = null) {
|
|
84
|
+
var _a2, _b, _c;
|
|
85
|
+
setResponse("");
|
|
86
|
+
setIdle(false);
|
|
87
|
+
let errorInFetch = "";
|
|
88
|
+
const url = "https://chat.llmasaservice.io/";
|
|
89
|
+
const responseBody = JSON.stringify({
|
|
90
|
+
projectId: (_a2 = context == null ? void 0 : context.project_id) != null ? _a2 : "",
|
|
91
|
+
serviceId: service,
|
|
92
|
+
prompt,
|
|
93
|
+
messages,
|
|
94
|
+
customer: (_b = context == null ? void 0 : context.customer) != null ? _b : {}
|
|
95
|
+
});
|
|
96
|
+
const options2 = {
|
|
97
|
+
method: "POST",
|
|
98
|
+
signal: abortController.signal,
|
|
99
|
+
mode: "cors",
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "text/plain"
|
|
102
|
+
//"x-Amz-Content-Sha256": sha256.create().update(responseBody).hex(),
|
|
103
|
+
//"x-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
|
|
104
|
+
},
|
|
105
|
+
body: responseBody
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
const response2 = yield fetch(url, options2);
|
|
109
|
+
if (!response2.ok) {
|
|
110
|
+
errorInFetch = `Error: Network error for service. (${response2.status} ${response2.statusText})`;
|
|
111
|
+
} else {
|
|
112
|
+
const reader = (_c = response2 == null ? void 0 : response2.body) == null ? void 0 : _c.getReader();
|
|
113
|
+
const decoder = new TextDecoder("utf-8");
|
|
114
|
+
setIdle(false);
|
|
115
|
+
if (!stream) {
|
|
116
|
+
setResponse(
|
|
117
|
+
yield readStream(reader, decoder, stream, {
|
|
118
|
+
signal: options2.signal
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
} else {
|
|
122
|
+
readStream(reader, decoder, stream, {
|
|
123
|
+
signal: options2.signal
|
|
124
|
+
});
|
|
125
|
+
return reader;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
} catch (errorObject) {
|
|
129
|
+
errorInFetch = `Error: Having trouble connecting to chat service. (${errorObject.message})`;
|
|
130
|
+
}
|
|
131
|
+
if (errorInFetch !== "") {
|
|
132
|
+
setError(errorInFetch);
|
|
133
|
+
console.error(`Error: Error in fetch. (${errorInFetch})`);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function readStream(_0, _1) {
|
|
138
|
+
return __async(this, arguments, function* (reader, decoder, stream = true, { signal }) {
|
|
139
|
+
let errorInRead = "";
|
|
140
|
+
let result = "";
|
|
141
|
+
while (true) {
|
|
142
|
+
try {
|
|
143
|
+
if (signal.aborted) {
|
|
144
|
+
reader.cancel();
|
|
145
|
+
setIdle(true);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
const { value, done } = yield reader.read();
|
|
149
|
+
if (decoder.decode(value).startsWith("Error:")) {
|
|
150
|
+
errorInRead = decoder.decode(value).substring(6);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
if (done) {
|
|
154
|
+
setIdle(true);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
result += decoder.decode(value);
|
|
158
|
+
if (stream) setResponse((prevState) => result);
|
|
159
|
+
} catch (error2) {
|
|
160
|
+
if (error2.name === "AbortError") {
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
errorInRead = `Reading error ${error2.message}`;
|
|
164
|
+
break;
|
|
165
|
+
} finally {
|
|
166
|
+
if (signal.aborted) {
|
|
167
|
+
reader.releaseLock();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (errorInRead !== "") {
|
|
172
|
+
setError(errorInRead);
|
|
173
|
+
reader.cancel();
|
|
174
|
+
setIdle(true);
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return { response, send, stop, idle, error };
|
|
180
|
+
};
|
|
181
|
+
var useLLM_default = useLLM;
|
|
182
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
183
|
+
0 && (module.exports = {
|
|
184
|
+
LLMAsAService,
|
|
185
|
+
useLLM
|
|
186
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/useLLM.ts
|
|
23
|
+
import { useContext, useState } from "react";
|
|
24
|
+
|
|
25
|
+
// src/LLMAsAService.tsx
|
|
26
|
+
import React, { createContext } from "react";
|
|
27
|
+
var LLMService = createContext(void 0);
|
|
28
|
+
var LLMAsAService_default = LLMService;
|
|
29
|
+
|
|
30
|
+
// src/useLLM.ts
|
|
31
|
+
var useLLM = (options) => {
|
|
32
|
+
var _a;
|
|
33
|
+
const [response, setResponse] = useState("");
|
|
34
|
+
const [idle, setIdle] = useState(true);
|
|
35
|
+
const [error, setError] = useState("");
|
|
36
|
+
const context = (_a = useContext(LLMService)) != null ? _a : options;
|
|
37
|
+
if (!context) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"useLLM must be used within a LLMServiceProvider or constructed with options in your useLLM() call."
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
const stop = (controller) => {
|
|
43
|
+
if (controller) controller.abort();
|
|
44
|
+
setIdle(true);
|
|
45
|
+
};
|
|
46
|
+
function send(_0) {
|
|
47
|
+
return __async(this, arguments, function* (prompt, messages = [{ role: "system", content: "You are a useful assistant." }], stream = true, abortController = new AbortController(), service = null) {
|
|
48
|
+
var _a2, _b, _c;
|
|
49
|
+
setResponse("");
|
|
50
|
+
setIdle(false);
|
|
51
|
+
let errorInFetch = "";
|
|
52
|
+
const url = "https://chat.llmasaservice.io/";
|
|
53
|
+
const responseBody = JSON.stringify({
|
|
54
|
+
projectId: (_a2 = context == null ? void 0 : context.project_id) != null ? _a2 : "",
|
|
55
|
+
serviceId: service,
|
|
56
|
+
prompt,
|
|
57
|
+
messages,
|
|
58
|
+
customer: (_b = context == null ? void 0 : context.customer) != null ? _b : {}
|
|
59
|
+
});
|
|
60
|
+
const options2 = {
|
|
61
|
+
method: "POST",
|
|
62
|
+
signal: abortController.signal,
|
|
63
|
+
mode: "cors",
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "text/plain"
|
|
66
|
+
//"x-Amz-Content-Sha256": sha256.create().update(responseBody).hex(),
|
|
67
|
+
//"x-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
|
|
68
|
+
},
|
|
69
|
+
body: responseBody
|
|
70
|
+
};
|
|
71
|
+
try {
|
|
72
|
+
const response2 = yield fetch(url, options2);
|
|
73
|
+
if (!response2.ok) {
|
|
74
|
+
errorInFetch = `Error: Network error for service. (${response2.status} ${response2.statusText})`;
|
|
75
|
+
} else {
|
|
76
|
+
const reader = (_c = response2 == null ? void 0 : response2.body) == null ? void 0 : _c.getReader();
|
|
77
|
+
const decoder = new TextDecoder("utf-8");
|
|
78
|
+
setIdle(false);
|
|
79
|
+
if (!stream) {
|
|
80
|
+
setResponse(
|
|
81
|
+
yield readStream(reader, decoder, stream, {
|
|
82
|
+
signal: options2.signal
|
|
83
|
+
})
|
|
84
|
+
);
|
|
85
|
+
} else {
|
|
86
|
+
readStream(reader, decoder, stream, {
|
|
87
|
+
signal: options2.signal
|
|
88
|
+
});
|
|
89
|
+
return reader;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} catch (errorObject) {
|
|
93
|
+
errorInFetch = `Error: Having trouble connecting to chat service. (${errorObject.message})`;
|
|
94
|
+
}
|
|
95
|
+
if (errorInFetch !== "") {
|
|
96
|
+
setError(errorInFetch);
|
|
97
|
+
console.error(`Error: Error in fetch. (${errorInFetch})`);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function readStream(_0, _1) {
|
|
102
|
+
return __async(this, arguments, function* (reader, decoder, stream = true, { signal }) {
|
|
103
|
+
let errorInRead = "";
|
|
104
|
+
let result = "";
|
|
105
|
+
while (true) {
|
|
106
|
+
try {
|
|
107
|
+
if (signal.aborted) {
|
|
108
|
+
reader.cancel();
|
|
109
|
+
setIdle(true);
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
const { value, done } = yield reader.read();
|
|
113
|
+
if (decoder.decode(value).startsWith("Error:")) {
|
|
114
|
+
errorInRead = decoder.decode(value).substring(6);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (done) {
|
|
118
|
+
setIdle(true);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
result += decoder.decode(value);
|
|
122
|
+
if (stream) setResponse((prevState) => result);
|
|
123
|
+
} catch (error2) {
|
|
124
|
+
if (error2.name === "AbortError") {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
errorInRead = `Reading error ${error2.message}`;
|
|
128
|
+
break;
|
|
129
|
+
} finally {
|
|
130
|
+
if (signal.aborted) {
|
|
131
|
+
reader.releaseLock();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (errorInRead !== "") {
|
|
136
|
+
setError(errorInRead);
|
|
137
|
+
reader.cancel();
|
|
138
|
+
setIdle(true);
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return { response, send, stop, idle, error };
|
|
144
|
+
};
|
|
145
|
+
var useLLM_default = useLLM;
|
|
146
|
+
export {
|
|
147
|
+
LLMAsAService_default as LLMAsAService,
|
|
148
|
+
useLLM_default as useLLM
|
|
149
|
+
};
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "llmasaservice-client",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup index.ts --format cjs,esm --dts",
|
|
10
|
+
"lint": "tsc"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"react": "^18.3.0",
|
|
14
|
+
"react-dom": "^18.3.1"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@changesets/cli": "^2.27.7",
|
|
18
|
+
"@types/react": "^18.3.3",
|
|
19
|
+
"react": "^18.3.1",
|
|
20
|
+
"react-dom": "^18.3.1",
|
|
21
|
+
"tsup": "^8.1.0",
|
|
22
|
+
"typescript": "^5.5.3"
|
|
23
|
+
},
|
|
24
|
+
"description": "HOC and hook to use the LLMAsAService.io LLM load balancer and firewall",
|
|
25
|
+
"author": "CASEY, Inc. <support@heycasey.io>"
|
|
26
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React, { createContext, ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
export type LLMAsAServiceCustomer = {
|
|
4
|
+
customer_id: string;
|
|
5
|
+
customer_name?: string;
|
|
6
|
+
customer_user_id?: string;
|
|
7
|
+
customer_user_email?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export interface LLMServiceType {
|
|
11
|
+
project_id: string | undefined;
|
|
12
|
+
customer: LLMAsAServiceCustomer;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const LLMService = createContext<LLMServiceType | undefined>(undefined);
|
|
16
|
+
|
|
17
|
+
interface UserProviderProps {
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
project_id: string | undefined;
|
|
20
|
+
customer: LLMAsAServiceCustomer;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const LLMServiceProvider: React.FC<UserProviderProps> = ({
|
|
24
|
+
children,
|
|
25
|
+
project_id,
|
|
26
|
+
customer,
|
|
27
|
+
}) => {
|
|
28
|
+
return (
|
|
29
|
+
<LLMService.Provider value={{ project_id, customer }}>
|
|
30
|
+
{children}
|
|
31
|
+
</LLMService.Provider>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default LLMService;
|
package/src/useLLM.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { useContext, useState } from "react";
|
|
2
|
+
import { LLMService, LLMServiceType } from "./LLMAsAService";
|
|
3
|
+
|
|
4
|
+
const useLLM = (options?: LLMServiceType) => {
|
|
5
|
+
const [response, setResponse] = useState<string>("");
|
|
6
|
+
const [idle, setIdle] = useState<boolean>(true);
|
|
7
|
+
const [error, setError] = useState<string>("");
|
|
8
|
+
|
|
9
|
+
const context = useContext(LLMService) ?? options;
|
|
10
|
+
if (!context) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"useLLM must be used within a LLMServiceProvider or constructed with options in your useLLM() call."
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Stops the fetch request and returns the hook to an idle state. Use this to add abort functionality to your UI.
|
|
18
|
+
*
|
|
19
|
+
* @param controller An AbortController object to stop the fetch request and return this hook to an idle state, the controller should be the same one passed to the send function.
|
|
20
|
+
*/
|
|
21
|
+
const stop = (controller: AbortController | null) => {
|
|
22
|
+
if (controller) controller.abort();
|
|
23
|
+
setIdle(true);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Calls the LLM as a service with the given prompt and messages. The response is returned in the response property of the hook.
|
|
28
|
+
*
|
|
29
|
+
* @param prompt The prompt to send the the LLM service.
|
|
30
|
+
* @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." }]
|
|
31
|
+
* @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.
|
|
32
|
+
* @param abortController The AbortController used to abort this request once its started. This allows you to add a stop button to your UI.
|
|
33
|
+
* @param service The service to use for the request. If null, load balancing will be applied. This is typically only used for testing.
|
|
34
|
+
* @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.
|
|
35
|
+
*/
|
|
36
|
+
async function send(
|
|
37
|
+
prompt: string,
|
|
38
|
+
messages = [{ role: "system", content: "You are a useful assistant." }],
|
|
39
|
+
stream: boolean = true,
|
|
40
|
+
abortController: AbortController = new AbortController(),
|
|
41
|
+
service: string | null = null // null means use the default service and apply services load balancing
|
|
42
|
+
): Promise<ReadableStreamDefaultReader<any> | string | undefined> {
|
|
43
|
+
setResponse("");
|
|
44
|
+
setIdle(false);
|
|
45
|
+
|
|
46
|
+
let errorInFetch = "";
|
|
47
|
+
const url = "https://chat.llmasaservice.io/";
|
|
48
|
+
|
|
49
|
+
const responseBody = JSON.stringify({
|
|
50
|
+
projectId: context?.project_id ?? "",
|
|
51
|
+
serviceId: service,
|
|
52
|
+
prompt: prompt,
|
|
53
|
+
messages: messages,
|
|
54
|
+
customer: context?.customer ?? {},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// trying to get cloudfront oac going. posts need to be signed, but when i add this the call fails...
|
|
58
|
+
const options = {
|
|
59
|
+
method: "POST",
|
|
60
|
+
signal: abortController.signal,
|
|
61
|
+
mode: "cors" as RequestMode,
|
|
62
|
+
headers: {
|
|
63
|
+
"Content-Type": "text/plain",
|
|
64
|
+
//"x-Amz-Content-Sha256": sha256.create().update(responseBody).hex(),
|
|
65
|
+
//"x-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
|
|
66
|
+
},
|
|
67
|
+
body: responseBody,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
const response = await fetch(url, options);
|
|
72
|
+
if (!response.ok) {
|
|
73
|
+
errorInFetch = `Error: Network error for service. (${response.status} ${response.statusText})`;
|
|
74
|
+
} else {
|
|
75
|
+
const reader =
|
|
76
|
+
response?.body?.getReader() as ReadableStreamDefaultReader;
|
|
77
|
+
const decoder = new TextDecoder("utf-8");
|
|
78
|
+
setIdle(false);
|
|
79
|
+
|
|
80
|
+
if (!stream) {
|
|
81
|
+
setResponse(
|
|
82
|
+
await readStream(reader, decoder, stream, {
|
|
83
|
+
signal: options.signal,
|
|
84
|
+
})
|
|
85
|
+
);
|
|
86
|
+
} else {
|
|
87
|
+
readStream(reader, decoder, stream, {
|
|
88
|
+
signal: options.signal,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return reader;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} catch (errorObject: any) {
|
|
95
|
+
errorInFetch = `Error: Having trouble connecting to chat service. (${errorObject.message})`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (errorInFetch !== "") {
|
|
99
|
+
setError(errorInFetch);
|
|
100
|
+
console.error(`Error: Error in fetch. (${errorInFetch})`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function readStream(
|
|
105
|
+
reader: ReadableStreamDefaultReader,
|
|
106
|
+
decoder: TextDecoder,
|
|
107
|
+
stream: Boolean = true,
|
|
108
|
+
{ signal: signal }: { signal: AbortSignal }
|
|
109
|
+
): Promise<string> {
|
|
110
|
+
let errorInRead = "";
|
|
111
|
+
let result = "";
|
|
112
|
+
|
|
113
|
+
while (true) {
|
|
114
|
+
try {
|
|
115
|
+
// Check if the stream has been aborted
|
|
116
|
+
if (signal.aborted) {
|
|
117
|
+
reader.cancel();
|
|
118
|
+
setIdle(true);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Read a chunk of data from the stream
|
|
123
|
+
const { value, done } = await reader.read();
|
|
124
|
+
|
|
125
|
+
if (decoder.decode(value).startsWith("Error:")) {
|
|
126
|
+
errorInRead = decoder.decode(value).substring(6);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// If the stream has been read to the end, exit the loop
|
|
131
|
+
if (done) {
|
|
132
|
+
setIdle(true);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Process the chunk of data
|
|
137
|
+
result += decoder.decode(value);
|
|
138
|
+
if (stream) setResponse((prevState: any) => result);
|
|
139
|
+
} catch (error: any) {
|
|
140
|
+
if (error.name === "AbortError") {
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
errorInRead = `Reading error ${error.message}`;
|
|
145
|
+
break;
|
|
146
|
+
} finally {
|
|
147
|
+
if (signal.aborted) {
|
|
148
|
+
reader.releaseLock();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (errorInRead !== "") {
|
|
154
|
+
setError(errorInRead);
|
|
155
|
+
reader.cancel();
|
|
156
|
+
setIdle(true);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return { response, send, stop, idle, error };
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export default useLLM;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
"jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
39
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
40
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
41
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
42
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
43
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
44
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
45
|
+
|
|
46
|
+
/* JavaScript Support */
|
|
47
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
48
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
49
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
50
|
+
|
|
51
|
+
/* Emit */
|
|
52
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
53
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
54
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
55
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
56
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
57
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
58
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
59
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
60
|
+
"noEmit": true, /* Disable emitting files from a compilation. */
|
|
61
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
62
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
63
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
64
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
65
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
66
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
67
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
68
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
69
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
70
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
71
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
72
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
73
|
+
|
|
74
|
+
/* Interop Constraints */
|
|
75
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
76
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
77
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
78
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
79
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
80
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
81
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
82
|
+
|
|
83
|
+
/* Type Checking */
|
|
84
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
85
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
86
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
87
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
88
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
89
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
90
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
91
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
92
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
93
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
94
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
95
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
96
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
97
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
98
|
+
"noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
99
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
100
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
101
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
102
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
103
|
+
|
|
104
|
+
/* Completeness */
|
|
105
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
106
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
107
|
+
}
|
|
108
|
+
}
|