@fonoster/autopilot 0.7.4 → 0.7.5
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 +156 -2
- package/dist/Autopilot.d.ts +3 -7
- package/dist/Autopilot.js +19 -36
- package/dist/assistants/AssistantSchema.d.ts +575 -30
- package/dist/assistants/AssistantSchema.js +41 -13
- package/dist/assistants/index.d.ts +1 -3
- package/dist/assistants/index.js +1 -6
- package/dist/assistants/loadAndValidateAssistant.d.ts +2 -2
- package/dist/assistants/loadAndValidateAssistant.js +3 -3
- package/dist/assistants/types.d.ts +6 -24
- package/dist/assistants/types.js +0 -25
- package/dist/createLanguageModel.d.ts +3 -0
- package/dist/createLanguageModel.js +40 -0
- package/dist/envs.d.ts +1 -0
- package/dist/envs.js +2 -1
- package/dist/handleVoiceRequest.d.ts +3 -0
- package/dist/handleVoiceRequest.js +66 -0
- package/dist/index.d.ts +9 -1
- package/dist/index.js +23 -3
- package/dist/knowledge/AbstractKnowledgeBase.d.ts +13 -0
- package/dist/knowledge/AbstractKnowledgeBase.js +18 -0
- package/dist/knowledge/FilesKnowledgeBase.d.ts +12 -0
- package/dist/knowledge/FilesKnowledgeBase.js +50 -0
- package/dist/knowledge/index.d.ts +3 -0
- package/dist/knowledge/index.js +37 -0
- package/dist/knowledge/types.d.ts +4 -0
- package/dist/knowledge/types.js +2 -0
- package/dist/loadAssistantConfig.d.ts +3 -0
- package/dist/loadAssistantConfig.js +26 -0
- package/dist/loadKnowledgeBase.d.ts +3 -0
- package/dist/loadKnowledgeBase.js +29 -0
- package/dist/machine/machine.d.ts +27 -28
- package/dist/machine/machine.js +44 -30
- package/dist/models/AbstractLanguageModel.d.ts +11 -0
- package/dist/models/AbstractLanguageModel.js +78 -0
- package/dist/models/LanguageModelFactory.d.ts +16 -0
- package/dist/models/LanguageModelFactory.js +44 -0
- package/dist/models/chatHistory.d.ts +6 -0
- package/dist/models/chatHistory.js +32 -0
- package/dist/models/createChain.d.ts +7 -0
- package/dist/models/createChain.js +15 -0
- package/dist/models/createPromptTemplate.d.ts +2 -0
- package/dist/{assistants/assistants.js → models/createPromptTemplate.js} +7 -16
- package/dist/models/groq/Groq.d.ts +7 -0
- package/dist/models/groq/Groq.js +22 -0
- package/dist/models/groq/index.d.ts +2 -0
- package/dist/models/groq/index.js +36 -0
- package/dist/models/groq/types.d.ts +13 -0
- package/dist/models/groq/types.js +9 -0
- package/dist/models/index.d.ts +2 -0
- package/dist/models/index.js +36 -0
- package/dist/models/openai/OpenAI.d.ts +7 -0
- package/dist/models/openai/OpenAI.js +22 -0
- package/dist/models/openai/index.d.ts +2 -0
- package/dist/models/openai/index.js +36 -0
- package/dist/models/openai/types.d.ts +12 -0
- package/dist/models/openai/types.js +8 -0
- package/dist/models/types.d.ts +19 -0
- package/dist/models/types.js +2 -0
- package/dist/server.js +23 -0
- package/dist/tools/ToolSchema.d.ts +107 -0
- package/dist/tools/ToolSchema.js +64 -0
- package/dist/tools/ToolsCatalog.d.ts +11 -0
- package/dist/tools/ToolsCatalog.js +52 -0
- package/dist/tools/builtin/hangupToolDefinition.d.ts +3 -0
- package/dist/tools/builtin/hangupToolDefinition.js +35 -0
- package/dist/tools/builtin/transferToolDefinition.d.ts +3 -0
- package/dist/tools/builtin/transferToolDefinition.js +35 -0
- package/dist/tools/convertToolToOpenAITool.d.ts +3 -0
- package/dist/tools/convertToolToOpenAITool.js +11 -0
- package/dist/tools/index.d.ts +4 -0
- package/dist/tools/index.js +38 -0
- package/dist/tools/sendRequest.d.ts +11 -0
- package/dist/tools/sendRequest.js +43 -0
- package/dist/tools/type.d.ts +8 -0
- package/dist/tools/type.js +2 -0
- package/dist/types.d.ts +14 -6
- package/dist/types.js +6 -0
- package/dist/vad/SileroVad.d.ts +8 -0
- package/dist/vad/SileroVad.js +39 -0
- package/dist/vad/index.d.ts +1 -2
- package/dist/vad/index.js +1 -2
- package/dist/vad/makeVad.d.ts +1 -1
- package/dist/vad/makeVad.js +2 -2
- package/dist/vad/types.d.ts +4 -2
- package/dist/voice/Voice.d.ts +25 -0
- package/dist/voice/Voice.js +81 -0
- package/dist/voice/index.d.ts +2 -0
- package/dist/voice/index.js +36 -0
- package/dist/voice/types.d.ts +23 -0
- package/dist/voice/types.js +2 -0
- package/dist/voiceServerSetup.d.ts +2 -0
- package/dist/{runner.js → voiceServerSetup.js} +5 -22
- package/package.json +15 -7
- package/dist/assistants/assistants.d.ts +0 -4
- package/dist/machine/machinev0.d.ts +0 -163
- package/dist/machine/machinev0.js +0 -142
- package/dist/machine/types.d.ts +0 -29
- package/dist/machine/types.js +0 -9
- package/dist/runner.d.ts +0 -2
- package/dist/vad/micVadTest.js +0 -48
- /package/dist/{vad/micVadTest.d.ts → server.d.ts} +0 -0
package/dist/machine/machine.js
CHANGED
|
@@ -19,11 +19,10 @@ exports.machine = void 0;
|
|
|
19
19
|
* See the License for the specific language governing permissions and
|
|
20
20
|
* limitations under the License.
|
|
21
21
|
*/
|
|
22
|
-
const common_1 = require("@fonoster/common");
|
|
23
22
|
const logger_1 = require("@fonoster/logger");
|
|
24
|
-
const uuid_1 = require("uuid");
|
|
25
23
|
const xstate_1 = require("xstate");
|
|
26
24
|
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
25
|
+
// eslint-disable-next-line mocha/no-top-level-hooks
|
|
27
26
|
const machine = (0, xstate_1.setup)({
|
|
28
27
|
types: {
|
|
29
28
|
context: {},
|
|
@@ -36,55 +35,66 @@ const machine = (0, xstate_1.setup)({
|
|
|
36
35
|
firstMessage: context.firstMessage
|
|
37
36
|
});
|
|
38
37
|
await context.voice.answer();
|
|
39
|
-
await context.voice.say(context.firstMessage
|
|
40
|
-
playbackRef: context.playbackRef
|
|
41
|
-
});
|
|
38
|
+
await context.voice.say(context.firstMessage);
|
|
42
39
|
},
|
|
43
40
|
goodbye: async ({ context }) => {
|
|
44
41
|
logger.verbose("called goodbye action", {
|
|
45
42
|
goodbyeMessage: context.goodbyeMessage
|
|
46
43
|
});
|
|
47
|
-
await context.voice.say(context.goodbyeMessage
|
|
48
|
-
playbackRef: context.playbackRef
|
|
49
|
-
});
|
|
44
|
+
await context.voice.say(context.goodbyeMessage);
|
|
50
45
|
await context.voice.hangup();
|
|
51
46
|
},
|
|
52
47
|
announceSystemError: async ({ context }) => {
|
|
53
48
|
logger.verbose("called announceSystemError action", {
|
|
54
49
|
systemErrorMessage: context.systemErrorMessage
|
|
55
50
|
});
|
|
56
|
-
await context.voice.say(context.systemErrorMessage
|
|
57
|
-
playbackRef: context.playbackRef
|
|
58
|
-
});
|
|
51
|
+
await context.voice.say(context.systemErrorMessage);
|
|
59
52
|
},
|
|
60
53
|
interruptPlayback: async ({ context }) => {
|
|
61
54
|
logger.verbose("called interruptPlayback action", {
|
|
62
|
-
|
|
55
|
+
sessionRef: context.sessionRef
|
|
63
56
|
});
|
|
64
|
-
await context.voice.
|
|
57
|
+
await context.voice.stopSpeech();
|
|
65
58
|
},
|
|
66
59
|
processUserRequest: async ({ context }) => {
|
|
67
60
|
logger.verbose("called processUserRequest action", {
|
|
68
61
|
speechBuffer: context.speechBuffer
|
|
69
62
|
});
|
|
70
63
|
const speech = context.speechBuffer.trim();
|
|
71
|
-
const
|
|
64
|
+
const languageModel = context.languageModel;
|
|
65
|
+
const response = await languageModel.invoke(speech);
|
|
72
66
|
const speechResponseTime = Date.now() - context.speechResponseStartTime;
|
|
73
67
|
context.speechResponseTime = speechResponseTime;
|
|
74
|
-
context.speechBuffer = "";
|
|
75
68
|
context.speechResponseStartTime = 0;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
if (!response) {
|
|
70
|
+
logger.verbose("call might already be hung up");
|
|
71
|
+
(0, xstate_1.raise)({ type: "USER_REQUEST_PROCESSED" });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (response.type === "hangup") {
|
|
75
|
+
const message = context.goodbyeMessage;
|
|
76
|
+
await context.voice.say(message);
|
|
77
|
+
await context.voice.hangup();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
else if (response.type === "transfer") {
|
|
81
|
+
const message = context.transferMessage;
|
|
82
|
+
await context.voice.say(message);
|
|
83
|
+
// TODO: The record and timeout options should be configurable
|
|
84
|
+
await context.voice.transfer(context.transferPhoneNumber, {
|
|
85
|
+
record: true,
|
|
86
|
+
timeout: 30
|
|
87
|
+
});
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
await context.voice.say(response.content);
|
|
79
91
|
(0, xstate_1.raise)({ type: "USER_REQUEST_PROCESSED" });
|
|
80
92
|
},
|
|
81
93
|
announceIdleTimeout: async ({ context }) => {
|
|
82
94
|
logger.verbose("called announceIdleTimeout action", {
|
|
83
95
|
idleMessage: context.idleMessage
|
|
84
96
|
});
|
|
85
|
-
await context.voice.say(context.idleMessage
|
|
86
|
-
playbackRef: context.playbackRef
|
|
87
|
-
});
|
|
97
|
+
await context.voice.say(context.idleMessage);
|
|
88
98
|
},
|
|
89
99
|
increaseIdleTimeoutCount: (0, xstate_1.assign)(({ context }) => {
|
|
90
100
|
logger.verbose("called increaseIdleTimeoutCount action", {
|
|
@@ -99,7 +109,9 @@ const machine = (0, xstate_1.setup)({
|
|
|
99
109
|
speech: event.speech
|
|
100
110
|
});
|
|
101
111
|
const speech = event.speech;
|
|
102
|
-
context.speechBuffer = (context.speechBuffer || "") +
|
|
112
|
+
context.speechBuffer = ((context.speechBuffer || "") +
|
|
113
|
+
" " +
|
|
114
|
+
speech).trimStart();
|
|
103
115
|
context.speechResponseStartTime = Date.now();
|
|
104
116
|
return context;
|
|
105
117
|
}),
|
|
@@ -150,16 +162,18 @@ const machine = (0, xstate_1.setup)({
|
|
|
150
162
|
}
|
|
151
163
|
}).createMachine({
|
|
152
164
|
context: ({ input }) => ({
|
|
165
|
+
sessionRef: input.voice.sessionRef,
|
|
153
166
|
voice: input.voice,
|
|
154
|
-
|
|
155
|
-
playbackRef: (0, uuid_1.v4)(),
|
|
167
|
+
languageModel: input.languageModel,
|
|
156
168
|
speechBuffer: "",
|
|
157
|
-
firstMessage: input.firstMessage,
|
|
158
|
-
goodbyeMessage: input.goodbyeMessage,
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
169
|
+
firstMessage: input.conversationSettings.firstMessage,
|
|
170
|
+
goodbyeMessage: input.conversationSettings.goodbyeMessage,
|
|
171
|
+
transferMessage: input.conversationSettings.transferOptions?.message,
|
|
172
|
+
transferPhoneNumber: input.conversationSettings.transferOptions?.phoneNumber,
|
|
173
|
+
systemErrorMessage: input.conversationSettings.systemErrorMessage,
|
|
174
|
+
idleMessage: input.conversationSettings.idleOptions?.message || "",
|
|
175
|
+
idleTimeout: input.conversationSettings.idleOptions?.timeout || 10000,
|
|
176
|
+
maxIdleTimeoutCount: input.conversationSettings.idleOptions?.maxTimeoutCount || 3,
|
|
163
177
|
idleTimeoutCount: 0,
|
|
164
178
|
speechResponseStartTime: 0,
|
|
165
179
|
speechResponseTime: 0,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InvocationResult, LanguageModel, LanguageModelParams } from "./types";
|
|
2
|
+
import { ToolsCatalog } from "../tools";
|
|
3
|
+
declare abstract class AbstractLanguageModel implements LanguageModel {
|
|
4
|
+
private params;
|
|
5
|
+
private chain;
|
|
6
|
+
private chatHistory;
|
|
7
|
+
toolsCatalog: ToolsCatalog;
|
|
8
|
+
constructor(params: LanguageModelParams);
|
|
9
|
+
invoke(text: string): Promise<InvocationResult>;
|
|
10
|
+
}
|
|
11
|
+
export { AbstractLanguageModel };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractLanguageModel = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
6
|
+
* http://github.com/fonoster/fonoster
|
|
7
|
+
*
|
|
8
|
+
* This file is part of Fonoster
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the MIT License (the "License");
|
|
11
|
+
* you may not use this file except in compliance with
|
|
12
|
+
* the License. You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* https://opensource.org/licenses/MIT
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
const logger_1 = require("@fonoster/logger");
|
|
23
|
+
const chatHistory_1 = require("./chatHistory");
|
|
24
|
+
const createChain_1 = require("./createChain");
|
|
25
|
+
const createPromptTemplate_1 = require("./createPromptTemplate");
|
|
26
|
+
const tools_1 = require("../tools");
|
|
27
|
+
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
28
|
+
class AbstractLanguageModel {
|
|
29
|
+
constructor(params) {
|
|
30
|
+
this.params = params;
|
|
31
|
+
const { model, systemTemplate, knowledgeBase, tools } = this.params;
|
|
32
|
+
const promptTemplate = (0, createPromptTemplate_1.createPromptTemplate)(systemTemplate);
|
|
33
|
+
this.chatHistory = (0, chatHistory_1.createChatHistory)();
|
|
34
|
+
this.toolsCatalog = new tools_1.ToolsCatalog(tools);
|
|
35
|
+
this.chain = (0, createChain_1.createChain)(model, knowledgeBase, promptTemplate, this.chatHistory);
|
|
36
|
+
}
|
|
37
|
+
async invoke(text) {
|
|
38
|
+
const { chain, chatHistory, toolsCatalog } = this;
|
|
39
|
+
const response = (await chain.invoke({ text }));
|
|
40
|
+
if (response.additional_kwargs?.tool_calls) {
|
|
41
|
+
// eslint-disable-next-line no-loops/no-loops
|
|
42
|
+
for (const toolCall of response.additional_kwargs.tool_calls) {
|
|
43
|
+
const { arguments: args, name } = toolCall.function;
|
|
44
|
+
logger.verbose(`invoking tool: ${name} with args: ${args}`);
|
|
45
|
+
switch (name) {
|
|
46
|
+
case "hangup":
|
|
47
|
+
await chatHistory.addAIMessage("tool result: call hangup initiated");
|
|
48
|
+
return { type: "hangup" };
|
|
49
|
+
case "transfer":
|
|
50
|
+
await chatHistory.addAIMessage("tool result: call transfer initiated");
|
|
51
|
+
return { type: "transfer" };
|
|
52
|
+
default:
|
|
53
|
+
try {
|
|
54
|
+
const toolResult = await toolsCatalog.invokeTool(name, JSON.parse(args));
|
|
55
|
+
logger.verbose("tool result: ", toolResult);
|
|
56
|
+
await chatHistory.addAIMessage(`tool result: ${toolResult.result}`);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
logger.error(`tool error: ${error.message}`);
|
|
60
|
+
await chatHistory.addAIMessage(`tool error: ${error.message}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const finalResponse = (await chain.invoke({
|
|
65
|
+
text: "Please provide a final response based on the tool results."
|
|
66
|
+
}));
|
|
67
|
+
response.content = finalResponse.content ?? "";
|
|
68
|
+
}
|
|
69
|
+
await chatHistory.addUserMessage(text);
|
|
70
|
+
await chatHistory.addAIMessage(response.content?.toString() ?? "");
|
|
71
|
+
logger.verbose("system will say", { content: response.content });
|
|
72
|
+
return {
|
|
73
|
+
type: "say",
|
|
74
|
+
content: response.content.toString()
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.AbstractLanguageModel = AbstractLanguageModel;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AbstractLanguageModel } from "./AbstractLanguageModel";
|
|
2
|
+
import { GroqParams } from "./groq";
|
|
3
|
+
import { OpenAIParams } from "./openai";
|
|
4
|
+
import { BaseModelParams } from "./types";
|
|
5
|
+
import { LANGUAGE_MODEL_PROVIDER } from "../types";
|
|
6
|
+
type LanguageModelConstructor<T extends BaseModelParams = BaseModelParams> = new (options: T) => AbstractLanguageModel;
|
|
7
|
+
type LanguageModelConfigMap = {
|
|
8
|
+
[LANGUAGE_MODEL_PROVIDER.OPENAI]: OpenAIParams;
|
|
9
|
+
[LANGUAGE_MODEL_PROVIDER.GROQ]: GroqParams;
|
|
10
|
+
};
|
|
11
|
+
declare class LanguageModelFactory {
|
|
12
|
+
private static languageModels;
|
|
13
|
+
static registerLanguageModel<T extends BaseModelParams>(name: string, ctor: LanguageModelConstructor<T>): void;
|
|
14
|
+
static getLanguageModel<T extends keyof LanguageModelConfigMap>(languageModel: T, config: LanguageModelConfigMap[T]): AbstractLanguageModel;
|
|
15
|
+
}
|
|
16
|
+
export { LanguageModelFactory };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LanguageModelFactory = void 0;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
6
|
+
* http://github.com/fonoster/fonoster
|
|
7
|
+
*
|
|
8
|
+
* This file is part of Fonoster
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the MIT License (the "License");
|
|
11
|
+
* you may not use this file except in compliance with
|
|
12
|
+
* the License. You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* https://opensource.org/licenses/MIT
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
const logger_1 = require("@fonoster/logger");
|
|
23
|
+
const groq_1 = require("./groq");
|
|
24
|
+
const openai_1 = require("./openai");
|
|
25
|
+
const types_1 = require("../types");
|
|
26
|
+
const logger = (0, logger_1.getLogger)({ service: "autopilot", filePath: __filename });
|
|
27
|
+
class LanguageModelFactory {
|
|
28
|
+
static registerLanguageModel(name, ctor) {
|
|
29
|
+
logger.verbose("registering llm provider", { name });
|
|
30
|
+
this.languageModels.set(name, ctor);
|
|
31
|
+
}
|
|
32
|
+
static getLanguageModel(languageModel, config) {
|
|
33
|
+
const LanguageModelConstructor = this.languageModels.get(`llm.${languageModel}`);
|
|
34
|
+
if (!LanguageModelConstructor) {
|
|
35
|
+
throw new Error(`Language model ${languageModel} not found`);
|
|
36
|
+
}
|
|
37
|
+
return new LanguageModelConstructor(config);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.LanguageModelFactory = LanguageModelFactory;
|
|
41
|
+
LanguageModelFactory.languageModels = new Map();
|
|
42
|
+
// Register language models
|
|
43
|
+
LanguageModelFactory.registerLanguageModel(openai_1.LANGUAGE_MODEL_NAME, openai_1.OpenAI);
|
|
44
|
+
LanguageModelFactory.registerLanguageModel(groq_1.LANGUAGE_MODEL_NAME, groq_1.Groq);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createChatHistory = createChatHistory;
|
|
4
|
+
/*
|
|
5
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
6
|
+
* http://github.com/fonoster/fonoster
|
|
7
|
+
*
|
|
8
|
+
* This file is part of Fonoster
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the MIT License (the "License");
|
|
11
|
+
* you may not use this file except in compliance with
|
|
12
|
+
* the License. You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* https://opensource.org/licenses/MIT
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
const messages_1 = require("@langchain/core/messages");
|
|
23
|
+
const in_memory_1 = require("langchain/stores/message/in_memory");
|
|
24
|
+
function createChatHistory() {
|
|
25
|
+
const chatHistory = new in_memory_1.ChatMessageHistory();
|
|
26
|
+
return {
|
|
27
|
+
getMessages: () => chatHistory.getMessages(),
|
|
28
|
+
addUserMessage: (text) => chatHistory.addMessage(new messages_1.HumanMessage(text)),
|
|
29
|
+
addAIMessage: (text) => chatHistory.addMessage(new messages_1.AIMessage(text)),
|
|
30
|
+
clear: () => chatHistory.clear()
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
2
|
+
import { RunnableSequence } from "@langchain/core/runnables";
|
|
3
|
+
import { createChatHistory } from "./chatHistory";
|
|
4
|
+
import { createPromptTemplate } from "./createPromptTemplate";
|
|
5
|
+
import { KnowledgeBase } from "../knowledge";
|
|
6
|
+
declare function createChain(model: BaseChatModel, knowledgeBase: KnowledgeBase, promptTemplate: ReturnType<typeof createPromptTemplate>, chatHistory: ReturnType<typeof createChatHistory>): RunnableSequence<any, import("@langchain/core/messages").BaseMessageChunk>;
|
|
7
|
+
export { createChain };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createChain = createChain;
|
|
4
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
5
|
+
function createChain(model, knowledgeBase, promptTemplate, chatHistory) {
|
|
6
|
+
return runnables_1.RunnableSequence.from([
|
|
7
|
+
{
|
|
8
|
+
input: (input) => input.text,
|
|
9
|
+
context: async (input) => knowledgeBase.queryKnowledgeBase(input.text),
|
|
10
|
+
history: async () => chatHistory.getMessages()
|
|
11
|
+
},
|
|
12
|
+
promptTemplate,
|
|
13
|
+
model
|
|
14
|
+
]);
|
|
15
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createPromptTemplate = createPromptTemplate;
|
|
4
4
|
/*
|
|
5
5
|
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
6
6
|
* http://github.com/fonoster/fonoster
|
|
@@ -19,21 +19,12 @@ exports.makeAssistant = makeAssistant;
|
|
|
19
19
|
* See the License for the specific language governing permissions and
|
|
20
20
|
* limitations under the License.
|
|
21
21
|
*/
|
|
22
|
-
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
23
22
|
const prompts_1 = require("@langchain/core/prompts");
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
temperature: config.temperature
|
|
31
|
-
});
|
|
32
|
-
const { systemTemplate } = config;
|
|
33
|
-
const parser = new output_parsers_1.StringOutputParser();
|
|
34
|
-
const promptTemplate = prompts_1.ChatPromptTemplate.fromMessages([
|
|
35
|
-
["system", systemTemplate],
|
|
36
|
-
["user", "{text}"]
|
|
23
|
+
function createPromptTemplate(systemTemplate) {
|
|
24
|
+
return prompts_1.ChatPromptTemplate.fromMessages([
|
|
25
|
+
new prompts_1.MessagesPlaceholder("history"),
|
|
26
|
+
prompts_1.SystemMessagePromptTemplate.fromTemplate(systemTemplate),
|
|
27
|
+
prompts_1.SystemMessagePromptTemplate.fromTemplate("{context}"),
|
|
28
|
+
prompts_1.HumanMessagePromptTemplate.fromTemplate("{input}")
|
|
37
29
|
]);
|
|
38
|
-
return promptTemplate.pipe(model).pipe(parser);
|
|
39
30
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { GroqParams } from "./types";
|
|
2
|
+
import { AbstractLanguageModel } from "../AbstractLanguageModel";
|
|
3
|
+
declare const LANGUAGE_MODEL_NAME = "llm.groq";
|
|
4
|
+
declare class Groq extends AbstractLanguageModel {
|
|
5
|
+
constructor(params: GroqParams);
|
|
6
|
+
}
|
|
7
|
+
export { Groq, LANGUAGE_MODEL_NAME };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LANGUAGE_MODEL_NAME = exports.Groq = void 0;
|
|
4
|
+
const groq_1 = require("@langchain/groq");
|
|
5
|
+
const tools_1 = require("../../tools");
|
|
6
|
+
const AbstractLanguageModel_1 = require("../AbstractLanguageModel");
|
|
7
|
+
const LANGUAGE_MODEL_NAME = "llm.groq";
|
|
8
|
+
exports.LANGUAGE_MODEL_NAME = LANGUAGE_MODEL_NAME;
|
|
9
|
+
class Groq extends AbstractLanguageModel_1.AbstractLanguageModel {
|
|
10
|
+
constructor(params) {
|
|
11
|
+
const model = new groq_1.ChatGroq({
|
|
12
|
+
...params
|
|
13
|
+
}).bind({
|
|
14
|
+
tools: params.tools.map(tools_1.convertToolToOpenAITool)
|
|
15
|
+
});
|
|
16
|
+
super({
|
|
17
|
+
...params,
|
|
18
|
+
model
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Groq = Groq;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Groq"), exports);
|
|
18
|
+
/*
|
|
19
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
20
|
+
* http://github.com/fonoster/fonoster
|
|
21
|
+
*
|
|
22
|
+
* This file is part of Fonoster
|
|
23
|
+
*
|
|
24
|
+
* Licensed under the MIT License (the "License");
|
|
25
|
+
* you may not use this file except in compliance with
|
|
26
|
+
* the License. You may obtain a copy of the License at
|
|
27
|
+
*
|
|
28
|
+
* https://opensource.org/licenses/MIT
|
|
29
|
+
*
|
|
30
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
31
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
32
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
33
|
+
* See the License for the specific language governing permissions and
|
|
34
|
+
* limitations under the License.
|
|
35
|
+
*/
|
|
36
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseModelParams } from "../types";
|
|
2
|
+
declare enum GroqModel {
|
|
3
|
+
GEMMA7B = "gemma-7b-it",
|
|
4
|
+
LLAMA3_GROQ_70B_8192_TOOL_USE_PREVIEW = "llama3-groq-70b-8192-tool-use-preview",
|
|
5
|
+
LLAMA3_1_8B_INSTANT = "llama-3.1-8b-instant"
|
|
6
|
+
}
|
|
7
|
+
type GroqParams = BaseModelParams & {
|
|
8
|
+
model: GroqModel;
|
|
9
|
+
apiKey: string;
|
|
10
|
+
maxTokens: number;
|
|
11
|
+
temperature: number;
|
|
12
|
+
};
|
|
13
|
+
export { GroqModel, GroqParams };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GroqModel = void 0;
|
|
4
|
+
var GroqModel;
|
|
5
|
+
(function (GroqModel) {
|
|
6
|
+
GroqModel["GEMMA7B"] = "gemma-7b-it";
|
|
7
|
+
GroqModel["LLAMA3_GROQ_70B_8192_TOOL_USE_PREVIEW"] = "llama3-groq-70b-8192-tool-use-preview";
|
|
8
|
+
GroqModel["LLAMA3_1_8B_INSTANT"] = "llama-3.1-8b-instant";
|
|
9
|
+
})(GroqModel || (exports.GroqModel = GroqModel = {}));
|
|
@@ -0,0 +1,36 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./LanguageModelFactory"), exports);
|
|
18
|
+
/*
|
|
19
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
20
|
+
* http://github.com/fonoster/fonoster
|
|
21
|
+
*
|
|
22
|
+
* This file is part of Fonoster
|
|
23
|
+
*
|
|
24
|
+
* Licensed under the MIT License (the "License");
|
|
25
|
+
* you may not use this file except in compliance with
|
|
26
|
+
* the License. You may obtain a copy of the License at
|
|
27
|
+
*
|
|
28
|
+
* https://opensource.org/licenses/MIT
|
|
29
|
+
*
|
|
30
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
31
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
32
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
33
|
+
* See the License for the specific language governing permissions and
|
|
34
|
+
* limitations under the License.
|
|
35
|
+
*/
|
|
36
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { OpenAIParams } from "./types";
|
|
2
|
+
import { AbstractLanguageModel } from "../AbstractLanguageModel";
|
|
3
|
+
declare const LANGUAGE_MODEL_NAME = "llm.openai";
|
|
4
|
+
declare class OpenAI extends AbstractLanguageModel {
|
|
5
|
+
constructor(params: OpenAIParams);
|
|
6
|
+
}
|
|
7
|
+
export { LANGUAGE_MODEL_NAME, OpenAI };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAI = exports.LANGUAGE_MODEL_NAME = void 0;
|
|
4
|
+
const openai_1 = require("@langchain/openai");
|
|
5
|
+
const tools_1 = require("../../tools");
|
|
6
|
+
const AbstractLanguageModel_1 = require("../AbstractLanguageModel");
|
|
7
|
+
const LANGUAGE_MODEL_NAME = "llm.openai";
|
|
8
|
+
exports.LANGUAGE_MODEL_NAME = LANGUAGE_MODEL_NAME;
|
|
9
|
+
class OpenAI extends AbstractLanguageModel_1.AbstractLanguageModel {
|
|
10
|
+
constructor(params) {
|
|
11
|
+
const model = new openai_1.ChatOpenAI({
|
|
12
|
+
...params
|
|
13
|
+
}).bind({
|
|
14
|
+
tools: params.tools.map(tools_1.convertToolToOpenAITool)
|
|
15
|
+
});
|
|
16
|
+
super({
|
|
17
|
+
...params,
|
|
18
|
+
model
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.OpenAI = OpenAI;
|
|
@@ -0,0 +1,36 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./OpenAI"), exports);
|
|
18
|
+
/*
|
|
19
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
20
|
+
* http://github.com/fonoster/fonoster
|
|
21
|
+
*
|
|
22
|
+
* This file is part of Fonoster
|
|
23
|
+
*
|
|
24
|
+
* Licensed under the MIT License (the "License");
|
|
25
|
+
* you may not use this file except in compliance with
|
|
26
|
+
* the License. You may obtain a copy of the License at
|
|
27
|
+
*
|
|
28
|
+
* https://opensource.org/licenses/MIT
|
|
29
|
+
*
|
|
30
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
31
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
32
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
33
|
+
* See the License for the specific language governing permissions and
|
|
34
|
+
* limitations under the License.
|
|
35
|
+
*/
|
|
36
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseModelParams } from "../types";
|
|
2
|
+
declare enum OpenAIModel {
|
|
3
|
+
GPT_4O = "gpt-4o",
|
|
4
|
+
GPT_4O_MINI = "gpt-4o-mini"
|
|
5
|
+
}
|
|
6
|
+
type OpenAIParams = BaseModelParams & {
|
|
7
|
+
model: OpenAIModel;
|
|
8
|
+
apiKey: string;
|
|
9
|
+
maxTokens: number;
|
|
10
|
+
temperature: number;
|
|
11
|
+
};
|
|
12
|
+
export { OpenAIModel, OpenAIParams };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIModel = void 0;
|
|
4
|
+
var OpenAIModel;
|
|
5
|
+
(function (OpenAIModel) {
|
|
6
|
+
OpenAIModel["GPT_4O"] = "gpt-4o";
|
|
7
|
+
OpenAIModel["GPT_4O_MINI"] = "gpt-4o-mini";
|
|
8
|
+
})(OpenAIModel || (exports.OpenAIModel = OpenAIModel = {}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
2
|
+
import { KnowledgeBase } from "../knowledge";
|
|
3
|
+
import { Tool } from "../tools/type";
|
|
4
|
+
type LanguageModel = {
|
|
5
|
+
invoke: (text: string) => Promise<InvocationResult>;
|
|
6
|
+
};
|
|
7
|
+
type BaseModelParams = {
|
|
8
|
+
systemTemplate: string;
|
|
9
|
+
knowledgeBase: KnowledgeBase;
|
|
10
|
+
tools: Tool[];
|
|
11
|
+
};
|
|
12
|
+
type LanguageModelParams = BaseModelParams & {
|
|
13
|
+
model: BaseChatModel;
|
|
14
|
+
};
|
|
15
|
+
type InvocationResult = {
|
|
16
|
+
type: "say" | "hangup" | "transfer";
|
|
17
|
+
content?: string;
|
|
18
|
+
};
|
|
19
|
+
export { BaseModelParams, InvocationResult, LanguageModel, LanguageModelParams };
|