|
1 |
| -import { AgentExecutor, ChatAgent } from 'langchain/agents'; |
2 |
| -import { ConversationChain, LLMChain } from 'langchain/chains'; |
| 1 | +import { ConversationChain } from 'langchain/chains'; |
3 | 2 | import { ChatOpenAI } from 'langchain/chat_models/openai';
|
4 | 3 | import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate } from 'langchain/prompts';
|
5 |
| -import { BufferMemory } from 'langchain/memory'; |
6 |
| -import { HumanChatMessage, SystemChatMessage } from 'langchain/schema'; |
7 |
| -import { SerpAPI } from 'langchain/tools'; |
| 4 | +import { ConversationSummaryMemory } from 'langchain/memory'; |
8 | 5 |
|
9 | 6 | class ChatService {
|
10 |
| - static async startChat(data) { |
11 |
| - const chat = new ChatOpenAI({ openAIApiKey: process.env.OPENAI_API_KEY, temperature: 0, verbose: true }); |
12 |
| - const { body: { userInput } } = data; |
13 |
| - |
14 |
| - const chatPrompt = ChatPromptTemplate.fromPromptMessages([ |
| 7 | + constructor () { |
| 8 | + this.chat = new ChatOpenAI({ temperature: 0, verbose: true }); |
| 9 | + this.chatPrompt = ChatPromptTemplate.fromPromptMessages([ |
15 | 10 | SystemMessagePromptTemplate.fromTemplate('The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.'),
|
16 | 11 | new MessagesPlaceholder('history'),
|
17 | 12 | HumanMessagePromptTemplate.fromTemplate('{input}'),
|
18 | 13 | ]);
|
19 | 14 |
|
| 15 | + this.memory = new ConversationSummaryMemory({ llm: this.chat, returnMessages: true }); |
| 16 | + } |
| 17 | + |
| 18 | + async startChat(data) { |
| 19 | + const { body: { userInput } } = data; |
| 20 | + |
20 | 21 | const chain = new ConversationChain({
|
21 |
| - memory: new BufferMemory({ returnMessages: true }), |
22 |
| - prompt: chatPrompt, |
23 |
| - llm: chat, |
| 22 | + memory: this.memory, |
| 23 | + prompt: this.chatPrompt, |
| 24 | + llm: this.chat, |
24 | 25 | });
|
25 | 26 |
|
26 | 27 | const response = await chain.call({
|
|
0 commit comments