-
Notifications
You must be signed in to change notification settings - Fork 125
/
13.ts
executable file
·58 lines (56 loc) · 2.08 KB
/
13.ts
1
2
3
4
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
46
47
48
49
50
51
52
53
54
55
56
import { ChatOpenAI } from "langchain/chat_models/openai";
import {BaseMessageChunk, HumanMessage} from "langchain/schema";
const queryEnrichmentSchema = {
"name": "query_enrichment",
"description": "Describe users query with semantic tags and classify with type",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "boolean",
"description": "Set to 'true' when query is direct command for AI. Set to 'false' when queries asks for saying/writing/translating/explaining something and all other."
},
"type": {
"type": "string",
"description": "memory (queries about the user and/or AI), notes|links (queries about user's notes|links). By default pick 'memory'.",
"enum": ["memory", "notes", "links"]
},
"tags": {
"type": "array",
"description": "Multiple semantic tags/keywords that enriches query for search purposes (similar words, meanings). When query refers to the user, add 'overment' tag, and when refers to 'you' add tag 'Alice'",
"items": {
"type": "string"
}
}
},
"required": [
"type", "tags", "command"
]
}
};
const model = new ChatOpenAI({
modelName: "gpt-4-0613",
}).bind({
functions: [queryEnrichmentSchema],
function_call: { name: "query_enrichment" },
});
console.log({
functions: [queryEnrichmentSchema],
function_call: { name: "query_enrichment" },
})
const result = await model.invoke([
new HumanMessage("Hey there!")
]);
const parseFunctionCall = (result: BaseMessageChunk): { name: string, args: any } | null => {
if (result?.additional_kwargs?.function_call === undefined) {
return null;
}
return {
name: result.additional_kwargs.function_call.name,
args: JSON.parse(result.additional_kwargs.function_call.arguments),
}
}
const action = parseFunctionCall(result);
if (action) {
console.log(action.name, action.args);
}