-
Notifications
You must be signed in to change notification settings - Fork 2k
/
openai_functions.ts
65 lines (63 loc) 路 2.12 KB
/
openai_functions.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
57
58
59
60
61
62
63
64
65
import { StructuredToolInterface } from "@langchain/core/tools";
import { ChatOpenAI } from "../../../chat_models/openai.js";
import { ConversationSummaryBufferMemory } from "../../../memory/summary_buffer.js";
import { initializeAgentExecutorWithOptions } from "../../initialize.js";
import { OpenAIAgentTokenBufferMemory } from "./token_buffer_memory.js";
export type ConversationalRetrievalAgentOptions = {
rememberIntermediateSteps?: boolean;
memoryKey?: string;
outputKey?: string;
prefix?: string;
verbose?: boolean;
};
/**
* Asynchronous function that creates a conversational retrieval agent
* using a language model, tools, and options. It initializes the buffer
* memory based on the provided options and initializes the AgentExecutor
* with the tools, language model, and memory.
* @param llm Instance of ChatOpenAI used as the language model for the agent.
* @param tools Array of StructuredTool instances used by the agent.
* @param options Optional ConversationalRetrievalAgentOptions to customize the agent.
* @returns A Promise that resolves to an initialized AgentExecutor.
*/
export async function createConversationalRetrievalAgent(
llm: ChatOpenAI,
tools: StructuredToolInterface[],
options?: ConversationalRetrievalAgentOptions
) {
const {
rememberIntermediateSteps = true,
memoryKey = "chat_history",
outputKey = "output",
prefix,
verbose,
} = options ?? {};
let memory;
if (rememberIntermediateSteps) {
memory = new OpenAIAgentTokenBufferMemory({
memoryKey,
llm,
outputKey,
});
} else {
memory = new ConversationSummaryBufferMemory({
memoryKey,
llm,
maxTokenLimit: 12000,
returnMessages: true,
outputKey,
});
}
const executor = await initializeAgentExecutorWithOptions(tools, llm, {
agentType: "openai-functions",
memory,
verbose,
returnIntermediateSteps: rememberIntermediateSteps,
agentArgs: {
prefix:
prefix ??
`Do your best to answer the questions. Feel free to use any tools available to look up relevant information, only if necessary.`,
},
});
return executor;
}