@aj-archipelago/cortex 0.0.9 → 0.0.11
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/config.js +17 -11
- package/graphql/chunker.js +97 -107
- package/graphql/graphql.js +19 -22
- package/graphql/parser.js +1 -1
- package/graphql/pathwayPrompter.js +8 -9
- package/graphql/pathwayResolver.js +12 -14
- package/graphql/pathwayResponseParser.js +2 -2
- package/graphql/plugins/azureTranslatePlugin.js +2 -2
- package/graphql/plugins/modelPlugin.js +67 -25
- package/graphql/plugins/openAiChatPlugin.js +3 -3
- package/graphql/plugins/openAiCompletionPlugin.js +5 -4
- package/graphql/plugins/openAiWhisperPlugin.js +7 -6
- package/graphql/prompt.js +1 -1
- package/graphql/pubsub.js +2 -2
- package/graphql/requestState.js +1 -1
- package/graphql/resolver.js +4 -4
- package/graphql/subscriptions.js +5 -4
- package/graphql/typeDef.js +53 -53
- package/index.js +5 -5
- package/lib/fileChunker.js +15 -11
- package/lib/keyValueStorageClient.js +5 -5
- package/lib/promiser.js +2 -2
- package/lib/request.js +11 -9
- package/lib/requestMonitor.js +2 -2
- package/package.json +15 -5
- package/pathways/basePathway.js +5 -4
- package/pathways/bias.js +2 -2
- package/pathways/chat.js +3 -2
- package/pathways/complete.js +4 -2
- package/pathways/edit.js +3 -2
- package/pathways/entities.js +3 -2
- package/pathways/index.js +25 -12
- package/pathways/lc_test.mjs +99 -0
- package/pathways/paraphrase.js +3 -2
- package/pathways/sentiment.js +3 -2
- package/pathways/summary.js +27 -10
- package/pathways/transcribe.js +4 -2
- package/pathways/translate.js +3 -2
- package/start.js +5 -2
- package/tests/chunkfunction.test.js +125 -0
- package/tests/chunking.test.js +25 -19
- package/tests/main.test.js +52 -38
- package/tests/translate.test.js +13 -10
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// OpenAICompletionPlugin.js
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import ModelPlugin from './modelPlugin.js';
|
|
3
|
+
|
|
4
|
+
import { encode } from 'gpt-3-encoder';
|
|
4
5
|
|
|
5
6
|
class OpenAICompletionPlugin extends ModelPlugin {
|
|
6
7
|
constructor(config, pathway) {
|
|
@@ -16,7 +17,7 @@ class OpenAICompletionPlugin extends ModelPlugin {
|
|
|
16
17
|
let requestParameters = {};
|
|
17
18
|
|
|
18
19
|
if (modelPromptMessages) {
|
|
19
|
-
const requestMessages = this.
|
|
20
|
+
const requestMessages = this.truncateMessagesToTargetLength(modelPromptMessages, modelMaxTokenLength - 1);
|
|
20
21
|
modelPromptMessagesML = this.messagesToChatML(requestMessages);
|
|
21
22
|
tokenLength = encode(modelPromptMessagesML).length;
|
|
22
23
|
|
|
@@ -66,5 +67,5 @@ class OpenAICompletionPlugin extends ModelPlugin {
|
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
|
|
70
|
+
export default OpenAICompletionPlugin;
|
|
70
71
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// OpenAICompletionPlugin.js
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import ModelPlugin from './modelPlugin.js';
|
|
3
|
+
|
|
4
|
+
import FormData from 'form-data';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import { splitMediaFile, isValidYoutubeUrl, processYoutubeUrl, deleteTempPath } from '../../lib/fileChunker.js';
|
|
7
|
+
import pubsub from '../pubsub.js';
|
|
7
8
|
|
|
8
9
|
class OpenAIWhisperPlugin extends ModelPlugin {
|
|
9
10
|
constructor(config, pathway) {
|
|
@@ -87,5 +88,5 @@ class OpenAIWhisperPlugin extends ModelPlugin {
|
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
export default OpenAIWhisperPlugin;
|
|
91
92
|
|
package/graphql/prompt.js
CHANGED
package/graphql/pubsub.js
CHANGED
package/graphql/requestState.js
CHANGED
package/graphql/resolver.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { fulfillWithTimeout } from '../lib/promiser.js';
|
|
2
|
+
import { PathwayResolver } from './pathwayResolver.js';
|
|
3
3
|
|
|
4
4
|
// This resolver uses standard parameters required by Apollo server:
|
|
5
5
|
// (parent, args, contextValue, info)
|
|
@@ -38,6 +38,6 @@ const cancelRequestResolver = (parent, args, contextValue, info) => {
|
|
|
38
38
|
return true
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
export {
|
|
42
42
|
resolver, rootResolver, cancelRequestResolver
|
|
43
|
-
}
|
|
43
|
+
};
|
package/graphql/subscriptions.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// multi-server instance
|
|
3
3
|
// See https://www.apollographql.com/docs/apollo-server/v3/data/subscriptions/#resolving-a-subscription
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import pubsub from './pubsub.js';
|
|
6
|
+
|
|
7
|
+
import { withFilter } from 'graphql-subscriptions';
|
|
8
|
+
import { requestState } from './requestState.js';
|
|
8
9
|
|
|
9
10
|
const subscriptions = {
|
|
10
11
|
requestProgress: {
|
|
@@ -31,4 +32,4 @@ const subscriptions = {
|
|
|
31
32
|
},
|
|
32
33
|
};
|
|
33
34
|
|
|
34
|
-
|
|
35
|
+
export default subscriptions;
|
package/graphql/typeDef.js
CHANGED
|
@@ -3,59 +3,59 @@ const GRAPHQL_TYPE_MAP = {
|
|
|
3
3
|
string: 'String',
|
|
4
4
|
number: 'Int',
|
|
5
5
|
};
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
name: key,
|
|
47
|
-
type: `${GRAPHQL_TYPE_MAP[typeof value]}${typeof value === 'object' && Array.isArray(value) ? '[]' : ''}`,
|
|
48
|
-
};
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const gqlDefinition = `${messageType}\n\n${type}\n\n${responseType}\n\nextend type Query {${name}(${paramsStr}): ${objName}}`;
|
|
52
|
-
|
|
6
|
+
|
|
7
|
+
const typeDef = (pathway) => {
|
|
8
|
+
const { name, objName, defaultInputParameters, inputParameters, format } = pathway;
|
|
9
|
+
|
|
10
|
+
const fields = format ? format.match(/\b(\w+)\b/g) : null;
|
|
11
|
+
const fieldsStr = !fields ? `` : fields.map((f) => `${f}: String`).join('\n ');
|
|
12
|
+
|
|
13
|
+
const typeName = fields ? `${objName}Result` : `String`;
|
|
14
|
+
const messageType = `input Message { role: String, content: String }`;
|
|
15
|
+
|
|
16
|
+
const type = fields ? `type ${typeName} {
|
|
17
|
+
${fieldsStr}
|
|
18
|
+
}` : ``;
|
|
19
|
+
|
|
20
|
+
const resultStr = pathway.list ? `[${typeName}]` : typeName;
|
|
21
|
+
|
|
22
|
+
const responseType = `type ${objName} {
|
|
23
|
+
debug: String
|
|
24
|
+
result: ${resultStr}
|
|
25
|
+
previousResult: String
|
|
26
|
+
warnings: [String]
|
|
27
|
+
contextId: String
|
|
28
|
+
}`;
|
|
29
|
+
|
|
30
|
+
const params = { ...defaultInputParameters, ...inputParameters };
|
|
31
|
+
|
|
32
|
+
const paramsStr = Object.entries(params)
|
|
33
|
+
.map(([key, value]) => {
|
|
34
|
+
if (typeof value === 'object' && Array.isArray(value)) {
|
|
35
|
+
return `${key}: [Message] = []`;
|
|
36
|
+
} else {
|
|
37
|
+
return `${key}: ${GRAPHQL_TYPE_MAP[typeof value]} = ${
|
|
38
|
+
typeof value === 'string' ? `"${value}"` : value
|
|
39
|
+
}`;
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
.join('\n');
|
|
43
|
+
|
|
44
|
+
const restDefinition = Object.entries(params).map(([key, value]) => {
|
|
53
45
|
return {
|
|
54
|
-
|
|
55
|
-
|
|
46
|
+
name: key,
|
|
47
|
+
type: `${GRAPHQL_TYPE_MAP[typeof value]}${typeof value === 'object' && Array.isArray(value) ? '[]' : ''}`,
|
|
56
48
|
};
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const gqlDefinition = `${messageType}\n\n${type}\n\n${responseType}\n\nextend type Query {${name}(${paramsStr}): ${objName}}`;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
gqlDefinition,
|
|
55
|
+
restDefinition,
|
|
57
56
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export {
|
|
60
|
+
typeDef,
|
|
61
|
+
};
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { config } from './config.js';
|
|
2
|
+
import { build } from './graphql/graphql.js';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export default async (configParams) => {
|
|
5
5
|
configParams && config.load(configParams);
|
|
6
|
-
return build(config);
|
|
7
|
-
}
|
|
6
|
+
return await build(config);
|
|
7
|
+
};
|
package/lib/fileChunker.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { path as ffmpegPath } from '@ffmpeg-installer/ffmpeg';
|
|
3
|
+
import ffmpeg from 'fluent-ffmpeg';
|
|
4
4
|
ffmpeg.setFfmpegPath(ffmpegPath);
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
7
|
+
import os from 'os';
|
|
8
|
+
import util from 'util';
|
|
9
|
+
import { pipeline } from 'stream';
|
|
10
|
+
|
|
9
11
|
const ffmpegProbe = util.promisify(ffmpeg.ffprobe);
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
+
|
|
13
|
+
const cPipeline = util.promisify(pipeline);
|
|
14
|
+
|
|
15
|
+
import ytdl from 'ytdl-core';
|
|
12
16
|
|
|
13
17
|
|
|
14
18
|
async function processChunk(inputPath, outputFileName, start, duration) {
|
|
@@ -117,7 +121,7 @@ function convertYoutubeToMp3Stream(video) {
|
|
|
117
121
|
|
|
118
122
|
async function pipeStreamToFile(stream, filePath) {
|
|
119
123
|
try {
|
|
120
|
-
await
|
|
124
|
+
await cPipeline(stream, fs.createWriteStream(filePath));
|
|
121
125
|
console.log('Stream piped to file successfully.');
|
|
122
126
|
} catch (error) {
|
|
123
127
|
console.error(`Error piping stream to file: ${error.message}`);
|
|
@@ -151,6 +155,6 @@ function deleteFile(filePath) {
|
|
|
151
155
|
}
|
|
152
156
|
}
|
|
153
157
|
|
|
154
|
-
|
|
158
|
+
export {
|
|
155
159
|
splitMediaFile, deleteTempPath, processYoutubeUrl, isValidYoutubeUrl
|
|
156
160
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import Keyv from 'keyv';
|
|
2
|
+
import { config } from '../config.js';
|
|
3
3
|
|
|
4
4
|
const storageConnectionString = config.get('storageConnectionString');
|
|
5
5
|
|
|
@@ -18,15 +18,15 @@ const keyValueStorageClient = new Keyv(storageConnectionString, {
|
|
|
18
18
|
|
|
19
19
|
// Set values to keyv
|
|
20
20
|
async function setv(key, value) {
|
|
21
|
-
return
|
|
21
|
+
return keyValueStorageClient && (await keyValueStorageClient.set(key, value));
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Get values from keyv
|
|
25
25
|
async function getv(key) {
|
|
26
|
-
return
|
|
26
|
+
return keyValueStorageClient && (await keyValueStorageClient.get(key));
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
export {
|
|
30
30
|
keyValueStorageClient,
|
|
31
31
|
setv,
|
|
32
32
|
getv
|
package/lib/promiser.js
CHANGED
package/lib/request.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import Bottleneck from 'bottleneck/es5.js';
|
|
2
|
+
import RequestMonitor from './requestMonitor.js';
|
|
3
|
+
import { config } from '../config.js';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import { setupCache } from 'axios-cache-interceptor';
|
|
6
|
+
|
|
7
|
+
let cortexAxios = axios;
|
|
5
8
|
|
|
6
9
|
if (config.get('enableCache')) {
|
|
7
10
|
// Setup cache
|
|
8
|
-
|
|
9
|
-
axios = setupCache(axios, {
|
|
11
|
+
cortexAxios = setupCache(axios, {
|
|
10
12
|
// enable cache for all requests by default
|
|
11
13
|
methods: ['get', 'post', 'put', 'delete', 'patch'],
|
|
12
14
|
interpretHeader: false,
|
|
@@ -57,7 +59,7 @@ setInterval(() => {
|
|
|
57
59
|
const postWithMonitor = async (model, url, data, axiosConfigObj) => {
|
|
58
60
|
const monitor = monitors[model];
|
|
59
61
|
monitor.incrementCallCount();
|
|
60
|
-
return
|
|
62
|
+
return cortexAxios.post(url, data, axiosConfigObj);
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
const MAX_RETRY = 10;
|
|
@@ -103,6 +105,6 @@ const request = async (params, model) => {
|
|
|
103
105
|
return data;
|
|
104
106
|
}
|
|
105
107
|
|
|
106
|
-
|
|
108
|
+
export {
|
|
107
109
|
request, postRequest, buildLimiters
|
|
108
|
-
}
|
|
110
|
+
};
|
package/lib/requestMonitor.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aj-archipelago/cortex",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "Cortex is a GraphQL API for AI. It provides a simple, extensible interface for using AI services from OpenAI, Azure and others.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,10 +21,11 @@
|
|
|
21
21
|
"main": "index.js",
|
|
22
22
|
"scripts": {
|
|
23
23
|
"start": "node start.js",
|
|
24
|
-
"test": "
|
|
24
|
+
"test": "ava"
|
|
25
25
|
},
|
|
26
26
|
"author": "",
|
|
27
27
|
"license": "MIT",
|
|
28
|
+
"type": "module",
|
|
28
29
|
"homepage": "https://github.com/aj-archipelago/cortex#readme",
|
|
29
30
|
"dependencies": {
|
|
30
31
|
"@apollo/utils.keyvadapter": "^1.1.2",
|
|
@@ -49,14 +50,23 @@
|
|
|
49
50
|
"graphql-ws": "^5.11.2",
|
|
50
51
|
"handlebars": "^4.7.7",
|
|
51
52
|
"keyv": "^4.5.2",
|
|
53
|
+
"langchain": "^0.0.47",
|
|
52
54
|
"ws": "^8.12.0",
|
|
53
55
|
"ytdl-core": "^4.11.2"
|
|
54
56
|
},
|
|
55
57
|
"devDependencies": {
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
+
"ava": "^5.2.0",
|
|
59
|
+
"dotenv": "^16.0.3"
|
|
58
60
|
},
|
|
59
61
|
"publishConfig": {
|
|
60
|
-
"access": "
|
|
62
|
+
"access": "restricted"
|
|
63
|
+
},
|
|
64
|
+
"ava": {
|
|
65
|
+
"files": [
|
|
66
|
+
"tests/**/*.test.js"
|
|
67
|
+
],
|
|
68
|
+
"require": [
|
|
69
|
+
"dotenv/config"
|
|
70
|
+
]
|
|
61
71
|
}
|
|
62
72
|
}
|
package/pathways/basePathway.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { rootResolver, resolver } from '../graphql/resolver.js';
|
|
2
|
+
import { typeDef } from '../graphql/typeDef.js';
|
|
3
3
|
|
|
4
4
|
// all default definitions of a single pathway
|
|
5
|
-
|
|
5
|
+
export default {
|
|
6
6
|
prompt: `{{text}}`,
|
|
7
7
|
defaultInputParameters: {
|
|
8
8
|
text: ``,
|
|
@@ -19,4 +19,5 @@ module.exports = {
|
|
|
19
19
|
useInputSummarization: false,
|
|
20
20
|
truncateFromFront: false,
|
|
21
21
|
timeout: 120, // in seconds
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
|
+
|
package/pathways/bias.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// Objectivity analysis of text
|
|
3
3
|
// This module exports a prompt that analyzes the given text and determines if it's written objectively. It also provides a detailed explanation of the decision.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export default {
|
|
6
6
|
// Uncomment the following line to enable caching for this prompt, if desired.
|
|
7
7
|
// enableCache: true,
|
|
8
8
|
|
|
9
9
|
prompt: `{{text}}\n\nIs the above text written objectively? Why or why not, explain with details:\n`
|
|
10
|
-
}
|
|
10
|
+
};
|
package/pathways/chat.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Simple context-aware chat bot
|
|
3
3
|
// This is a two prompt implementation of a context aware chat bot. The first prompt generates content that will be stored in the previousResult variable and will be returned to the client. In the optimum implementation, the client will then update their chatContext variable for the next call. The second prompt actually responds to the user. The second prompt *could* use previousResult instead of chatContext, but in this situation previousResult will also include the current turn of the conversation to which it is responding. That can get a little confusing as it tends to overemphasize the current turn in the response.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export default {
|
|
6
6
|
prompt:
|
|
7
7
|
[
|
|
8
8
|
`{{{chatContext}}}\n\n{{{text}}}\n\nGiven the information above, create a short summary of the conversation to date making sure to include all of the personal details about the user that you encounter:\n\n`,
|
|
@@ -12,4 +12,5 @@ module.exports = {
|
|
|
12
12
|
chatContext: `User: Starting conversation.`,
|
|
13
13
|
},
|
|
14
14
|
useInputChunking: false,
|
|
15
|
-
}
|
|
15
|
+
};
|
|
16
|
+
|
package/pathways/complete.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// Text completion module
|
|
3
3
|
// This module exports a prompt that takes an input text and completes it by generating a continuation of the given text.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export default {
|
|
6
6
|
prompt: `Continue and complete the following:\n\n{{text}}`
|
|
7
|
-
}
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
|
package/pathways/edit.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Grammar and spelling correction module
|
|
3
3
|
// This module exports a prompt that takes an input text and corrects all spelling and grammar errors found within the text.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export default {
|
|
6
6
|
// Set the temperature to 0 to favor more deterministic output when generating corrections.
|
|
7
7
|
temperature: 0,
|
|
8
8
|
|
|
9
9
|
prompt: `Correct all spelling and grammar errors in the input text.\n\nInput:\n{{text}}\n\nOutput:\n`
|
|
10
|
-
}
|
|
10
|
+
};
|
|
11
|
+
|
package/pathways/entities.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Entity extraction module
|
|
3
3
|
// This module exports a prompt that takes an input text and extracts the top entities and their definitions as specified by the count parameter.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
export default {
|
|
6
6
|
// Set the temperature to 0 to favor more deterministic output when generating entity extraction.
|
|
7
7
|
temperature: 0,
|
|
8
8
|
|
|
@@ -18,4 +18,5 @@ module.exports = {
|
|
|
18
18
|
|
|
19
19
|
// Set the list option to true as the prompt is expected to return a list of entities.
|
|
20
20
|
list: true,
|
|
21
|
-
}
|
|
21
|
+
};
|
|
22
|
+
|
package/pathways/index.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import edit from './edit.js';
|
|
2
|
+
import chat from './chat.js';
|
|
3
|
+
import bias from './bias.js';
|
|
4
|
+
import complete from './complete.js';
|
|
5
|
+
import entities from './entities.js';
|
|
6
|
+
import lc_test from './lc_test.mjs';
|
|
7
|
+
import paraphrase from './paraphrase.js';
|
|
8
|
+
import sentiment from './sentiment.js';
|
|
9
|
+
import summary from './summary.js';
|
|
10
|
+
import transcribe from './transcribe.js';
|
|
11
|
+
import translate from './translate.js';
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
edit,
|
|
15
|
+
chat,
|
|
16
|
+
bias,
|
|
17
|
+
complete,
|
|
18
|
+
entities,
|
|
19
|
+
lc_test,
|
|
20
|
+
paraphrase,
|
|
21
|
+
sentiment,
|
|
22
|
+
summary,
|
|
23
|
+
transcribe,
|
|
24
|
+
translate
|
|
25
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// lc_test.js
|
|
2
|
+
// LangChain Cortex integration test
|
|
3
|
+
|
|
4
|
+
// Import required modules
|
|
5
|
+
import { OpenAI } from "langchain/llms";
|
|
6
|
+
import { PromptTemplate } from "langchain/prompts";
|
|
7
|
+
import { LLMChain, ConversationChain } from "langchain/chains";
|
|
8
|
+
import { initializeAgentExecutor } from "langchain/agents";
|
|
9
|
+
import { SerpAPI, Calculator } from "langchain/tools";
|
|
10
|
+
import { BufferMemory } from "langchain/memory";
|
|
11
|
+
|
|
12
|
+
export default {
|
|
13
|
+
|
|
14
|
+
// Agent test case
|
|
15
|
+
resolver: async (parent, args, contextValue, info) => {
|
|
16
|
+
|
|
17
|
+
const { config } = contextValue;
|
|
18
|
+
const openAIApiKey = config.get('openaiApiKey');
|
|
19
|
+
const serpApiKey = config.get('serpApiKey');
|
|
20
|
+
|
|
21
|
+
const model = new OpenAI({ openAIApiKey: openAIApiKey, temperature: 0 });
|
|
22
|
+
const tools = [new SerpAPI( serpApiKey ), new Calculator()];
|
|
23
|
+
|
|
24
|
+
const executor = await initializeAgentExecutor(
|
|
25
|
+
tools,
|
|
26
|
+
model,
|
|
27
|
+
"zero-shot-react-description"
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
console.log(`====================`);
|
|
31
|
+
console.log("Loaded langchain agent.");
|
|
32
|
+
const input = args.text;
|
|
33
|
+
console.log(`Executing with input "${input}"...`);
|
|
34
|
+
const result = await executor.call({ input });
|
|
35
|
+
console.log(`Got output ${result.output}`);
|
|
36
|
+
console.log(`====================`);
|
|
37
|
+
|
|
38
|
+
return result?.output;
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
// Agent test case
|
|
43
|
+
resolver: async (parent, args, contextValue, info) => {
|
|
44
|
+
|
|
45
|
+
const { config } = contextValue;
|
|
46
|
+
const openAIApiKey = config.get('openaiApiKey');
|
|
47
|
+
const serpApiKey = config.get('serpApiKey');
|
|
48
|
+
|
|
49
|
+
const model = new OpenAI({ openAIApiKey: openAIApiKey, temperature: 0 });
|
|
50
|
+
const tools = [new SerpAPI( serpApiKey ), new Calculator()];
|
|
51
|
+
|
|
52
|
+
const executor = await initializeAgentExecutor(
|
|
53
|
+
tools,
|
|
54
|
+
model,
|
|
55
|
+
"zero-shot-react-description"
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
console.log(`====================`);
|
|
59
|
+
console.log("Loaded langchain agent.");
|
|
60
|
+
const input = args.text;
|
|
61
|
+
console.log(`Executing with input "${input}"...`);
|
|
62
|
+
const result = await executor.call({ input });
|
|
63
|
+
console.log(`Got output ${result.output}`);
|
|
64
|
+
console.log(`====================`);
|
|
65
|
+
|
|
66
|
+
return result?.output;
|
|
67
|
+
},
|
|
68
|
+
*/
|
|
69
|
+
// Simplest test case
|
|
70
|
+
/*
|
|
71
|
+
resolver: async (parent, args, contextValue, info) => {
|
|
72
|
+
|
|
73
|
+
const { config } = contextValue;
|
|
74
|
+
const openAIApiKey = config.get('openaiApiKey');
|
|
75
|
+
|
|
76
|
+
const model = new OpenAI({ openAIApiKey: openAIApiKey, temperature: 0.9 });
|
|
77
|
+
|
|
78
|
+
const template = "What is a good name for a company that makes {product}?";
|
|
79
|
+
|
|
80
|
+
const prompt = new PromptTemplate({
|
|
81
|
+
template: template,
|
|
82
|
+
inputVariables: ["product"],
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const chain = new LLMChain({ llm: model, prompt: prompt });
|
|
86
|
+
|
|
87
|
+
console.log(`====================`);
|
|
88
|
+
console.log(`Calling langchain with prompt: ${prompt?.template}`);
|
|
89
|
+
console.log(`Input text: ${args.text}`);
|
|
90
|
+
const res = await chain.call({ product: args.text });
|
|
91
|
+
console.log(`Result: ${res?.text}`);
|
|
92
|
+
console.log(`====================`);
|
|
93
|
+
|
|
94
|
+
return res?.text?.trim();
|
|
95
|
+
},
|
|
96
|
+
*/
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
|