@sprucelabs/sprucebot-llm 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/README.md +148 -0
- package/build/.spruce/errors/errors.types.d.ts +13 -0
- package/build/.spruce/errors/errors.types.js +4 -0
- package/build/.spruce/errors/options.types.d.ts +7 -0
- package/build/.spruce/errors/options.types.js +2 -0
- package/build/.spruce/errors/sprucebotLlm/noBotInstanceSet.schema.d.ts +3 -0
- package/build/.spruce/errors/sprucebotLlm/noBotInstanceSet.schema.js +11 -0
- package/build/.spruce/schemas/fields/fieldClassMap.d.ts +2 -0
- package/build/.spruce/schemas/fields/fieldClassMap.js +4 -0
- package/build/.spruce/schemas/fields/fields.types.d.ts +1 -0
- package/build/.spruce/schemas/fields/fields.types.js +2 -0
- package/build/.spruce/settings.json +13 -0
- package/build/bots/PromptGenerator.d.ts +15 -0
- package/build/bots/PromptGenerator.js +84 -0
- package/build/bots/SprucebotLlmBotImpl.d.ts +18 -0
- package/build/bots/SprucebotLlmBotImpl.js +69 -0
- package/build/bots/SprucebotLlmFactory.d.ts +9 -0
- package/build/bots/SprucebotLlmFactory.js +35 -0
- package/build/bots/SprucebotLlmSkillImpl.d.ts +12 -0
- package/build/bots/SprucebotLlmSkillImpl.js +39 -0
- package/build/bots/adapters/OpenAi.d.ts +10 -0
- package/build/bots/adapters/OpenAi.js +32 -0
- package/build/bots/adapters/SpyOpenAiApi.d.ts +12 -0
- package/build/bots/adapters/SpyOpenAiApi.js +34 -0
- package/build/bots/templates.d.ts +4 -0
- package/build/bots/templates.js +74 -0
- package/build/chat.d.ts +1 -0
- package/build/chat.js +60 -0
- package/build/errors/SpruceError.d.ts +6 -0
- package/build/errors/SpruceError.js +25 -0
- package/build/errors/noBotInstanceSet.builder.d.ts +6 -0
- package/build/errors/noBotInstanceSet.builder.js +8 -0
- package/build/esm/.spruce/errors/errors.types.d.ts +13 -0
- package/build/esm/.spruce/errors/errors.types.js +3 -0
- package/build/esm/.spruce/errors/options.types.d.ts +7 -0
- package/build/esm/.spruce/errors/options.types.js +1 -0
- package/build/esm/bots/PromptGenerator.d.ts +15 -0
- package/build/esm/bots/PromptGenerator.js +67 -0
- package/build/esm/bots/SprucebotLlmBotImpl.d.ts +18 -0
- package/build/esm/bots/SprucebotLlmBotImpl.js +76 -0
- package/build/esm/bots/SprucebotLlmFactory.d.ts +9 -0
- package/build/esm/bots/SprucebotLlmFactory.js +29 -0
- package/build/esm/bots/SprucebotLlmSkillImpl.d.ts +12 -0
- package/build/esm/bots/SprucebotLlmSkillImpl.js +47 -0
- package/build/esm/bots/adapters/OpenAi.d.ts +10 -0
- package/build/esm/bots/adapters/OpenAi.js +36 -0
- package/build/esm/bots/adapters/SpyOpenAiApi.d.ts +12 -0
- package/build/esm/bots/adapters/SpyOpenAiApi.js +42 -0
- package/build/esm/bots/templates.d.ts +4 -0
- package/build/esm/bots/templates.js +68 -0
- package/build/esm/chat.d.ts +1 -0
- package/build/esm/chat.js +41 -0
- package/build/esm/errors/SpruceError.d.ts +6 -0
- package/build/esm/errors/SpruceError.js +19 -0
- package/build/esm/errors/noBotInstanceSet.builder.d.ts +6 -0
- package/build/esm/errors/noBotInstanceSet.builder.js +6 -0
- package/build/esm/examples/buildCallbackSkill.d.ts +2 -0
- package/build/esm/examples/buildCallbackSkill.js +28 -0
- package/build/esm/examples/buildJokeSkill.d.ts +2 -0
- package/build/esm/examples/buildJokeSkill.js +12 -0
- package/build/esm/examples/buildProfileSkill.d.ts +2 -0
- package/build/esm/examples/buildProfileSkill.js +29 -0
- package/build/esm/index.d.ts +0 -0
- package/build/esm/index.js +2 -0
- package/build/esm/llm.types.d.ts +61 -0
- package/build/esm/llm.types.js +6 -0
- package/build/esm/parsingResponses/ResponseParser.d.ts +15 -0
- package/build/esm/parsingResponses/ResponseParser.js +60 -0
- package/build/esm/parsingResponses/renderPlaceholder.d.ts +1 -0
- package/build/esm/parsingResponses/renderPlaceholder.js +4 -0
- package/build/examples/buildCallbackSkill.d.ts +2 -0
- package/build/examples/buildCallbackSkill.js +22 -0
- package/build/examples/buildJokeSkill.d.ts +2 -0
- package/build/examples/buildJokeSkill.js +15 -0
- package/build/examples/buildProfileSkill.d.ts +2 -0
- package/build/examples/buildProfileSkill.js +32 -0
- package/build/index.d.ts +0 -0
- package/build/index.js +2 -0
- package/build/llm.types.d.ts +61 -0
- package/build/llm.types.js +9 -0
- package/build/parsingResponses/ResponseParser.d.ts +15 -0
- package/build/parsingResponses/ResponseParser.js +53 -0
- package/build/parsingResponses/renderPlaceholder.d.ts +1 -0
- package/build/parsingResponses/renderPlaceholder.js +7 -0
- package/package.json +101 -0
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# sprucebot-llm (WIP)
|
|
2
|
+
A Typescript library for leveraging Large Langage Models (like GPT-3) to do... anything!
|
|
3
|
+
|
|
4
|
+
* Has memory
|
|
5
|
+
* Remembers past messages to build context
|
|
6
|
+
* Configure how much of the conversation your bot should remember*
|
|
7
|
+
* [Manages state](#adding-state-to-your-conversation)
|
|
8
|
+
* The state builds as the conversation continues
|
|
9
|
+
* Invoke callbacks whenever state changes
|
|
10
|
+
* [Connect to 3rd party API's](pulling-from-3rd-party-apis)
|
|
11
|
+
* Pull in data in real time
|
|
12
|
+
* Have your bot respond generated responses
|
|
13
|
+
* Unlimited use cases
|
|
14
|
+
* Skill architecture for extensibility
|
|
15
|
+
* Leverage Skills to get your bot to complete any task!
|
|
16
|
+
* Adapter Interface to create your own adapters
|
|
17
|
+
* Only support GPT-3 for now (more adapters based on demand)
|
|
18
|
+
* Fully typed
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
*In progress
|
|
22
|
+
## Getting started
|
|
23
|
+
|
|
24
|
+
### Install the library
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
yarn add @sprucelabs/sprucebot-llm
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Building your first joke bot
|
|
31
|
+
|
|
32
|
+
Create a file called `chat.ts` and add the following to get started:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { stdin as input, stdout as output } from 'node:process'
|
|
36
|
+
import * as readline from 'node:readline/promises'
|
|
37
|
+
import dotenv from 'dotenv'
|
|
38
|
+
import { OpenAi } from './bots/adapters/OpenAi'
|
|
39
|
+
import SprucebotLlmFactory from './bots/SprucebotLlmFactory'
|
|
40
|
+
|
|
41
|
+
dotenv.config()
|
|
42
|
+
const rl = readline.createInterface({ input, output })
|
|
43
|
+
|
|
44
|
+
;(async () => {
|
|
45
|
+
console.clear()
|
|
46
|
+
|
|
47
|
+
const adapter = new OpenAi(process.env.OPEN_AI_API_KEY!)
|
|
48
|
+
const bots = SprucebotLlmFactory.Factory()
|
|
49
|
+
|
|
50
|
+
const skill = bots.Skill({
|
|
51
|
+
yourJobIfYouChooseToAcceptItIs: 'to tell knock knock jokes!',
|
|
52
|
+
pleaseKeepInMindThat: [
|
|
53
|
+
'our audience is younger, so keep it PG!',
|
|
54
|
+
'you should never laugh when someone does not get the joke.',
|
|
55
|
+
"after each joke, you should tell me how many jokes you have left to tell before we're done.",
|
|
56
|
+
'you should acknowledge if someone laughs at your joke by saying "Thanks!" or "Glad you thought that was funny"!',
|
|
57
|
+
],
|
|
58
|
+
weAreDoneWhen: 'you have told 3 jokes!',
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const bot = bots.Bot({
|
|
62
|
+
adapter,
|
|
63
|
+
skill,
|
|
64
|
+
youAre:
|
|
65
|
+
"a bot named Sprucebot that is in test mode. At the start of every conversation, you introduce yourself and announce that you are in test mode so I don't get confused! You are young, hip, and adorable. You say things like, 'Jeepers' and 'Golly' because you are so cute!",
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
do {
|
|
69
|
+
const input = await rl.question('Message > ')
|
|
70
|
+
const response = await bot.sendMessage(input)
|
|
71
|
+
console.log('>', response)
|
|
72
|
+
} while (!bot.getIsDone())
|
|
73
|
+
|
|
74
|
+
console.log('Signing off...')
|
|
75
|
+
rl.close()
|
|
76
|
+
})()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Adding state to your conversation
|
|
82
|
+
This library depends on [`@sprucelabs/spruce-schema`](https://github.com/sprucelabsai/spruce-schema) to handle the structure and validation rules around your state.
|
|
83
|
+
```ts
|
|
84
|
+
const skill = bots.Skill({
|
|
85
|
+
yourJobIfYouChooseToAcceptItIs:
|
|
86
|
+
'to collect some information from me! You are a receptionist with 20 years experience and are very focused on getting answers needed to complete my profile',
|
|
87
|
+
stateSchema: buildSchema({
|
|
88
|
+
id: 'profile',
|
|
89
|
+
fields: {
|
|
90
|
+
firstName: {
|
|
91
|
+
type: 'text',
|
|
92
|
+
label: 'First name',
|
|
93
|
+
},
|
|
94
|
+
lastName: {
|
|
95
|
+
type: 'text',
|
|
96
|
+
label: 'Last name',
|
|
97
|
+
},
|
|
98
|
+
favoriteColor: {
|
|
99
|
+
type: 'select',
|
|
100
|
+
options: {
|
|
101
|
+
choices: [
|
|
102
|
+
{ label: 'Red', value: 'red' },
|
|
103
|
+
{ label: 'Blue', value: 'blue' },
|
|
104
|
+
{ label: 'Green', value: 'green' },
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Listening to state changes
|
|
114
|
+
|
|
115
|
+
If you supply a `stateSchema` then your bot will work through it until the state is completely updated. While the conversation is taking place, if the state changes, the skill will emit `did-update-state`
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
await skill.on('did-update-state', () => {
|
|
119
|
+
console.log('we are making progress!')
|
|
120
|
+
console.log(JSON.stringify(this.skill.getState()))
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
```
|
|
124
|
+
### Pulling from 3rd party api's
|
|
125
|
+
|
|
126
|
+
The approach to integrating 3rd party api's (as well as dropping in other dynamic data into responses) is straight forward.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const skill = bots.Skill({
|
|
130
|
+
yourJobIfYouChooseToAcceptItIs:
|
|
131
|
+
"to be be the best appointment taker on the planet. You have a many years of experience. You are going to ask me only 2 questions for this practice run. First, you'll ask me to pick an available time. Then, you'll ask me to pick my favorite color!",
|
|
132
|
+
callbacks: {
|
|
133
|
+
availableTimes: {
|
|
134
|
+
cb: async () => {
|
|
135
|
+
return ['9am', '10am', '11am', '1pm', '4pm', '5pm', '12am.'].join(
|
|
136
|
+
'\n'
|
|
137
|
+
)
|
|
138
|
+
},
|
|
139
|
+
useThisWhenever: 'your are showing what times I can pick from.',
|
|
140
|
+
},
|
|
141
|
+
favoriteColor: {
|
|
142
|
+
cb: async () => {
|
|
143
|
+
return ['red', 'blue', 'green', 'purple'].join('\n')
|
|
144
|
+
},
|
|
145
|
+
useThisWhenever: 'your are showing what colors I can pick from.',
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { default as SchemaEntity } from '@sprucelabs/schema';
|
|
2
|
+
import * as SpruceSchema from '@sprucelabs/schema';
|
|
3
|
+
export declare namespace SpruceErrors.SprucebotLlm {
|
|
4
|
+
interface NoBotInstanceSet {
|
|
5
|
+
}
|
|
6
|
+
interface NoBotInstanceSetSchema extends SpruceSchema.Schema {
|
|
7
|
+
id: 'noBotInstanceSet';
|
|
8
|
+
namespace: 'SprucebotLlm';
|
|
9
|
+
name: 'No bot instance set';
|
|
10
|
+
fields: {};
|
|
11
|
+
}
|
|
12
|
+
type NoBotInstanceSetEntity = SchemaEntity<SpruceErrors.SprucebotLlm.NoBotInstanceSetSchema>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SpruceErrors } from "./errors.types";
|
|
2
|
+
import { ErrorOptions as ISpruceErrorOptions } from "@sprucelabs/error";
|
|
3
|
+
export interface NoBotInstanceSetErrorOptions extends SpruceErrors.SprucebotLlm.NoBotInstanceSet, ISpruceErrorOptions {
|
|
4
|
+
code: 'NO_BOT_INSTANCE_SET';
|
|
5
|
+
}
|
|
6
|
+
type ErrorOptions = NoBotInstanceSetErrorOptions;
|
|
7
|
+
export default ErrorOptions;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
4
|
+
const noBotInstanceSetSchema = {
|
|
5
|
+
id: 'noBotInstanceSet',
|
|
6
|
+
namespace: 'SprucebotLlm',
|
|
7
|
+
name: 'No bot instance set',
|
|
8
|
+
fields: {}
|
|
9
|
+
};
|
|
10
|
+
schema_1.SchemaRegistry.getInstance().trackSchema(noBotInstanceSetSchema);
|
|
11
|
+
exports.default = noBotInstanceSetSchema;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FieldDefinitions, FieldDefinitionMap, FieldValueTypeGeneratorMap, FieldMap } from '@sprucelabs/schema';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { LlmMessage, SprucebotLlmBot } from '../llm.types';
|
|
2
|
+
export default class PromptGenerator {
|
|
3
|
+
private bot;
|
|
4
|
+
private eta;
|
|
5
|
+
constructor(bot: SprucebotLlmBot);
|
|
6
|
+
generate(): Promise<string>;
|
|
7
|
+
private stringifyState;
|
|
8
|
+
}
|
|
9
|
+
export declare function setUndefinedToNull(obj: Record<string, any>): void;
|
|
10
|
+
export interface TemplateContext {
|
|
11
|
+
youAre: string;
|
|
12
|
+
messages: LlmMessage[];
|
|
13
|
+
stateSchemaJson?: string;
|
|
14
|
+
stateJson?: string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.setUndefinedToNull = void 0;
|
|
38
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
39
|
+
const Eta = __importStar(require("eta"));
|
|
40
|
+
const templates_1 = require("./templates");
|
|
41
|
+
class PromptGenerator {
|
|
42
|
+
constructor(bot) {
|
|
43
|
+
this.eta = Eta;
|
|
44
|
+
(0, schema_1.assertOptions)({ bot }, ['bot']);
|
|
45
|
+
this.bot = bot;
|
|
46
|
+
}
|
|
47
|
+
async generate() {
|
|
48
|
+
const _a = this.bot.serialize(), { stateSchema, state } = _a, rest = __rest(_a, ["stateSchema", "state"]);
|
|
49
|
+
const { stateSchemaJson, stateJson } = this.stringifyState(stateSchema, state);
|
|
50
|
+
return await this.eta.render(templates_1.PROMPT_TEMPLATE, Object.assign({ stateSchemaJson,
|
|
51
|
+
stateJson }, rest), {
|
|
52
|
+
async: true,
|
|
53
|
+
autoEscape: false,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
stringifyState(stateSchema, state) {
|
|
57
|
+
if (!stateSchema) {
|
|
58
|
+
return {
|
|
59
|
+
stateSchemaJson: null,
|
|
60
|
+
stateJson: null,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const normalizedState = (0, schema_1.normalizeSchemaValues)(stateSchema, state !== null && state !== void 0 ? state : {}, {});
|
|
64
|
+
setUndefinedToNull(normalizedState);
|
|
65
|
+
const stateSchemaJson = JSON.stringify(stateSchema);
|
|
66
|
+
const stateJson = JSON.stringify(normalizedState);
|
|
67
|
+
return { stateSchemaJson, stateJson };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.default = PromptGenerator;
|
|
71
|
+
function setUndefinedToNull(obj) {
|
|
72
|
+
for (const key in obj) {
|
|
73
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
74
|
+
if (obj.hasOwnProperty(key)) {
|
|
75
|
+
if (obj[key] === undefined) {
|
|
76
|
+
obj[key] = null;
|
|
77
|
+
}
|
|
78
|
+
else if (typeof obj[key] === 'object') {
|
|
79
|
+
setUndefinedToNull(obj[key]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.setUndefinedToNull = setUndefinedToNull;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
|
|
2
|
+
import { Schema, SchemaValues } from '@sprucelabs/schema';
|
|
3
|
+
import { BotOptions, LlmEventContract, LlmMessage, SerializedBot, SprucebotLlmBot } from '../llm.types';
|
|
4
|
+
export default class SprucebotLlmBotImpl<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends AbstractEventEmitter<LlmEventContract> implements SprucebotLlmBot<StateSchema, State> {
|
|
5
|
+
private adapter;
|
|
6
|
+
private youAre;
|
|
7
|
+
private stateSchema?;
|
|
8
|
+
protected state?: Partial<State>;
|
|
9
|
+
private isDone;
|
|
10
|
+
protected messages: LlmMessage[];
|
|
11
|
+
private skill?;
|
|
12
|
+
constructor(options: BotOptions<StateSchema, State>);
|
|
13
|
+
markAsDone(): void;
|
|
14
|
+
getIsDone(): boolean;
|
|
15
|
+
serialize(): SerializedBot<StateSchema, State>;
|
|
16
|
+
sendMessage(message: string): Promise<string>;
|
|
17
|
+
updateState(newState: Partial<State>): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const mercury_event_emitter_1 = require("@sprucelabs/mercury-event-emitter");
|
|
7
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
8
|
+
const llm_types_1 = require("../llm.types");
|
|
9
|
+
const ResponseParser_1 = __importDefault(require("../parsingResponses/ResponseParser"));
|
|
10
|
+
class SprucebotLlmBotImpl extends mercury_event_emitter_1.AbstractEventEmitter {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
const { adapter, youAre, stateSchema, state, skill } = options;
|
|
13
|
+
super(llm_types_1.llmEventContract);
|
|
14
|
+
this.isDone = false;
|
|
15
|
+
this.messages = [];
|
|
16
|
+
this.adapter = adapter;
|
|
17
|
+
this.youAre = youAre;
|
|
18
|
+
this.stateSchema = stateSchema;
|
|
19
|
+
this.skill = skill;
|
|
20
|
+
this.state = stateSchema
|
|
21
|
+
? Object.assign(Object.assign({}, (0, schema_1.defaultSchemaValues)(stateSchema)), state)
|
|
22
|
+
: undefined;
|
|
23
|
+
}
|
|
24
|
+
markAsDone() {
|
|
25
|
+
this.isDone = true;
|
|
26
|
+
}
|
|
27
|
+
getIsDone() {
|
|
28
|
+
return this.isDone;
|
|
29
|
+
}
|
|
30
|
+
serialize() {
|
|
31
|
+
var _a, _b, _c;
|
|
32
|
+
const skill = (_a = this.skill) === null || _a === void 0 ? void 0 : _a.serialize();
|
|
33
|
+
return {
|
|
34
|
+
youAre: this.youAre,
|
|
35
|
+
stateSchema: (_b = this.stateSchema) !== null && _b !== void 0 ? _b : skill === null || skill === void 0 ? void 0 : skill.stateSchema,
|
|
36
|
+
state: (_c = this.state) !== null && _c !== void 0 ? _c : skill === null || skill === void 0 ? void 0 : skill.state,
|
|
37
|
+
messages: this.messages,
|
|
38
|
+
skill,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async sendMessage(message) {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
(0, schema_1.assertOptions)({ message }, ['message']);
|
|
44
|
+
this.messages.push({
|
|
45
|
+
from: 'Me',
|
|
46
|
+
message,
|
|
47
|
+
});
|
|
48
|
+
const response = await this.adapter.sendMessage(this);
|
|
49
|
+
const parser = ResponseParser_1.default.getInstance();
|
|
50
|
+
const { isDone, message: parsedResponse, state, } = await parser.parse(response, (_a = this.skill) === null || _a === void 0 ? void 0 : _a.serialize().callbacks);
|
|
51
|
+
this.isDone = isDone;
|
|
52
|
+
if (this.stateSchema && state) {
|
|
53
|
+
await this.updateState(state);
|
|
54
|
+
}
|
|
55
|
+
else if (state) {
|
|
56
|
+
await ((_b = this.skill) === null || _b === void 0 ? void 0 : _b.updateState(state));
|
|
57
|
+
}
|
|
58
|
+
this.messages.push({
|
|
59
|
+
from: 'You',
|
|
60
|
+
message: parsedResponse,
|
|
61
|
+
});
|
|
62
|
+
return parsedResponse;
|
|
63
|
+
}
|
|
64
|
+
async updateState(newState) {
|
|
65
|
+
this.state = Object.assign(Object.assign({}, this.state), newState);
|
|
66
|
+
await this.emit('did-update-state');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.default = SprucebotLlmBotImpl;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BotOptions, SkillOptions, SprucebotLlmBot, SprucebotLLmSkill } from '../llm.types';
|
|
2
|
+
export default class SprucebotLlmFactory {
|
|
3
|
+
private instance?;
|
|
4
|
+
Bot(options: BotOptions): SprucebotLlmBot;
|
|
5
|
+
Skill(options: SkillOptions): SprucebotLLmSkill;
|
|
6
|
+
getBotInstance(): SprucebotLlmBot<import("@sprucelabs/schema").Schema, import("@sprucelabs/schema").SchemaStaticValues<import("@sprucelabs/schema").Schema, false, never, import("@sprucelabs/schema").StaticSchemaAllValues<import("@sprucelabs/schema").Schema, false>>>;
|
|
7
|
+
setBotInstance(bot: SprucebotLlmBot): void;
|
|
8
|
+
static Factory(): SprucebotLlmFactory;
|
|
9
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
7
|
+
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
8
|
+
const SprucebotLlmBotImpl_1 = __importDefault(require("./SprucebotLlmBotImpl"));
|
|
9
|
+
const SprucebotLlmSkillImpl_1 = __importDefault(require("./SprucebotLlmSkillImpl"));
|
|
10
|
+
class SprucebotLlmFactory {
|
|
11
|
+
Bot(options) {
|
|
12
|
+
(0, schema_1.assertOptions)(options, ['youAre', 'adapter']);
|
|
13
|
+
const { Class } = options;
|
|
14
|
+
return Class ? new Class(options) : new SprucebotLlmBotImpl_1.default(options);
|
|
15
|
+
}
|
|
16
|
+
Skill(options) {
|
|
17
|
+
(0, schema_1.assertOptions)(options, ['yourJobIfYouChooseToAcceptItIs']);
|
|
18
|
+
return new SprucebotLlmSkillImpl_1.default(options);
|
|
19
|
+
}
|
|
20
|
+
getBotInstance() {
|
|
21
|
+
if (!this.instance) {
|
|
22
|
+
throw new SpruceError_1.default({
|
|
23
|
+
code: 'NO_BOT_INSTANCE_SET',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return this.instance;
|
|
27
|
+
}
|
|
28
|
+
setBotInstance(bot) {
|
|
29
|
+
this.instance = bot;
|
|
30
|
+
}
|
|
31
|
+
static Factory() {
|
|
32
|
+
return new this();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.default = SprucebotLlmFactory;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractEventEmitter } from '@sprucelabs/mercury-event-emitter';
|
|
2
|
+
import { Schema, SchemaValues } from '@sprucelabs/schema';
|
|
3
|
+
import { LlmEventContract, SerializedSkill, SkillOptions, SprucebotLLmSkill } from '../llm.types';
|
|
4
|
+
export default class SprucebotLlmSkillImpl<StateSchema extends Schema = Schema, State extends SchemaValues<StateSchema> = SchemaValues<StateSchema>> extends AbstractEventEmitter<LlmEventContract> implements SprucebotLLmSkill<StateSchema, State> {
|
|
5
|
+
private options;
|
|
6
|
+
private state?;
|
|
7
|
+
private stateSchema?;
|
|
8
|
+
constructor(options: SkillOptions<StateSchema, State>);
|
|
9
|
+
updateState(updates: Partial<State>): Promise<void>;
|
|
10
|
+
getState(): Partial<State> | undefined;
|
|
11
|
+
serialize(): SerializedSkill<StateSchema, State>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
const mercury_event_emitter_1 = require("@sprucelabs/mercury-event-emitter");
|
|
15
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
16
|
+
const llm_types_1 = require("../llm.types");
|
|
17
|
+
class SprucebotLlmSkillImpl extends mercury_event_emitter_1.AbstractEventEmitter {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
super(llm_types_1.llmEventContract);
|
|
20
|
+
this.state = {};
|
|
21
|
+
const { state, stateSchema } = options, rest = __rest(options, ["state", "stateSchema"]);
|
|
22
|
+
this.options = Object.assign(Object.assign({}, rest), { stateSchema });
|
|
23
|
+
this.stateSchema = stateSchema;
|
|
24
|
+
this.state = stateSchema
|
|
25
|
+
? Object.assign(Object.assign({}, (0, schema_1.defaultSchemaValues)(stateSchema)), state)
|
|
26
|
+
: undefined;
|
|
27
|
+
}
|
|
28
|
+
async updateState(updates) {
|
|
29
|
+
await this.emit('did-update-state');
|
|
30
|
+
this.state = Object.assign(Object.assign({}, this.state), updates);
|
|
31
|
+
}
|
|
32
|
+
getState() {
|
|
33
|
+
return this.state;
|
|
34
|
+
}
|
|
35
|
+
serialize() {
|
|
36
|
+
return Object.assign(Object.assign({}, this.options), { stateSchema: this.stateSchema, state: this.state });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.default = SprucebotLlmSkillImpl;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Configuration, OpenAIApi } from 'openai';
|
|
2
|
+
import { LlmAdapter, SprucebotLlmBot } from '../../llm.types';
|
|
3
|
+
export declare class OpenAi implements LlmAdapter {
|
|
4
|
+
static Configuration: typeof Configuration;
|
|
5
|
+
static OpenAIApi: typeof OpenAIApi;
|
|
6
|
+
private api;
|
|
7
|
+
constructor(apiKey: string);
|
|
8
|
+
sendMessage(bot: SprucebotLlmBot): Promise<string>;
|
|
9
|
+
}
|
|
10
|
+
export declare const MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MESSAGE_RESPONSE_ERROR_MESSAGE = exports.OpenAi = void 0;
|
|
7
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
8
|
+
const openai_1 = require("openai");
|
|
9
|
+
const PromptGenerator_1 = __importDefault(require("../PromptGenerator"));
|
|
10
|
+
class OpenAi {
|
|
11
|
+
constructor(apiKey) {
|
|
12
|
+
(0, schema_1.assertOptions)({ apiKey }, ['apiKey']);
|
|
13
|
+
const config = new OpenAi.Configuration({ apiKey });
|
|
14
|
+
this.api = new OpenAi.OpenAIApi(config);
|
|
15
|
+
}
|
|
16
|
+
async sendMessage(bot) {
|
|
17
|
+
var _a, _b, _c;
|
|
18
|
+
const generator = new PromptGenerator_1.default(bot);
|
|
19
|
+
const prompt = await generator.generate();
|
|
20
|
+
const response = await this.api.createCompletion({
|
|
21
|
+
prompt,
|
|
22
|
+
model: 'text-davinci-003',
|
|
23
|
+
max_tokens: 100,
|
|
24
|
+
stop: ['__Me__:'],
|
|
25
|
+
});
|
|
26
|
+
return ((_c = (_b = (_a = response.data.choices[0]) === null || _a === void 0 ? void 0 : _a.text) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : exports.MESSAGE_RESPONSE_ERROR_MESSAGE);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.OpenAi = OpenAi;
|
|
30
|
+
OpenAi.Configuration = openai_1.Configuration;
|
|
31
|
+
OpenAi.OpenAIApi = openai_1.OpenAIApi;
|
|
32
|
+
exports.MESSAGE_RESPONSE_ERROR_MESSAGE = "Oh no! Something went wrong and I can't talk right now!";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios';
|
|
2
|
+
import { Configuration, CreateCompletionRequest, CreateCompletionRequestPrompt, CreateCompletionResponse, OpenAIApi } from 'openai';
|
|
3
|
+
export default class SpyOpenAiApi extends OpenAIApi {
|
|
4
|
+
static config?: Configuration;
|
|
5
|
+
static lastMessage?: CreateCompletionRequestPrompt | null;
|
|
6
|
+
static lastModel?: string;
|
|
7
|
+
static responseMessage: string | false;
|
|
8
|
+
constructor(config: Configuration);
|
|
9
|
+
createCompletion(createCompletionRequest: CreateCompletionRequest): Promise<Response>;
|
|
10
|
+
}
|
|
11
|
+
type Response = AxiosResponse<CreateCompletionResponse, any>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const openai_1 = require("openai");
|
|
4
|
+
class SpyOpenAiApi extends openai_1.OpenAIApi {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
super(config);
|
|
7
|
+
SpyOpenAiApi.config = config;
|
|
8
|
+
}
|
|
9
|
+
async createCompletion(createCompletionRequest) {
|
|
10
|
+
SpyOpenAiApi.lastMessage = createCompletionRequest.prompt;
|
|
11
|
+
SpyOpenAiApi.lastModel = createCompletionRequest.model;
|
|
12
|
+
const choices = [];
|
|
13
|
+
if (SpyOpenAiApi.responseMessage) {
|
|
14
|
+
choices.push({
|
|
15
|
+
text: SpyOpenAiApi.responseMessage,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
config: {},
|
|
20
|
+
headers: {},
|
|
21
|
+
status: 200,
|
|
22
|
+
statusText: 'OK',
|
|
23
|
+
data: {
|
|
24
|
+
id: 'cmpl-1',
|
|
25
|
+
model: 'text-davinci-003',
|
|
26
|
+
created: 0,
|
|
27
|
+
object: 'text_completion',
|
|
28
|
+
choices,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.default = SpyOpenAiApi;
|
|
34
|
+
SpyOpenAiApi.responseMessage = 'hello!';
|