-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add new prompt scope - add prompt user - add CHAT Hisotry in prompt
- Loading branch information
Joao Paulo Nobrega
committed
Feb 20, 2024
1 parent
408f1dd
commit aac1293
Showing
8 changed files
with
238 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,39 @@ | ||
|
||
import { IDatabaseConfig } from '../../interface/agent.interface'; | ||
import { BaseChatMessageHistory } from 'langchain/schema'; | ||
import { BaseChatMessageHistory, BaseMessage } from 'langchain/schema'; | ||
|
||
import RedisChatHistory from './redis-chat-history'; | ||
import { BufferMemory } from 'langchain/memory'; | ||
import MemoryChatHistory from './memory-chat-history'; | ||
|
||
interface IChatHistory { | ||
addUserMessage(message: string): Promise<void>; | ||
addAIChatMessage(message: string): Promise<void>; | ||
getMessages(): Promise<BaseMessage[]>; | ||
getFormatedMessages(): Promise<string>; | ||
clear(): Promise<void>; | ||
getChatHistory(): BaseChatMessageHistory; | ||
getBufferMemory(): BufferMemory; | ||
} | ||
|
||
const Services = { | ||
redis: RedisChatHistory, | ||
memory: MemoryChatHistory, | ||
} as any; | ||
|
||
class ChatHistoryFactory { | ||
public static async create(settings: IDatabaseConfig): Promise<BaseChatMessageHistory> { | ||
return await new Services[settings.type](settings).build(); | ||
public static async create(settings: IDatabaseConfig): Promise<IChatHistory> { | ||
const service = new Services[settings?.type](settings); | ||
|
||
if (!service) { | ||
return await new MemoryChatHistory(settings).build(); | ||
} | ||
|
||
return await service.build(); | ||
} | ||
} | ||
|
||
export default ChatHistoryFactory; | ||
export { | ||
IChatHistory, | ||
ChatHistoryFactory, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { BaseChatMessageHistory, BaseMessage } from 'langchain/schema'; | ||
import { IDatabaseConfig } from '../../interface/agent.interface'; | ||
import { IChatHistory } from '.'; | ||
import { BufferMemory } from 'langchain/memory'; | ||
|
||
class MemoryChatHistory implements IChatHistory { | ||
private _settings: IDatabaseConfig; | ||
private _history: BaseChatMessageHistory; | ||
private _bufferMemory: BufferMemory; | ||
|
||
constructor(settings: IDatabaseConfig) { | ||
this._settings = settings; | ||
} | ||
|
||
addUserMessage(message: string): Promise<void> { | ||
return this._history?.addUserMessage(message); | ||
} | ||
|
||
addAIChatMessage(message: string): Promise<void> { | ||
return this._history?.addAIChatMessage(message); | ||
} | ||
|
||
getMessages(): Promise<BaseMessage[]> { | ||
return this._history?.getMessages(); | ||
} | ||
|
||
async getFormatedMessages(): Promise<string> { | ||
const messages = await this._history?.getMessages(); | ||
|
||
const cut = messages | ||
.slice(-(this._settings?.limit || 5)); | ||
|
||
const formated = cut.map((message) => `${message._getType().toUpperCase()}: ${message.content}`).join('\n'); | ||
|
||
return formated; | ||
} | ||
|
||
getChatHistory(): BaseChatMessageHistory { | ||
return this._history; | ||
} | ||
|
||
getBufferMemory(): BufferMemory { | ||
return this._bufferMemory; | ||
} | ||
|
||
clear(): Promise<void> { | ||
return this._history?.clear(); | ||
} | ||
|
||
async build(): Promise<IChatHistory> { | ||
const { ChatMessageHistory } = (await import('langchain/stores/message/in_memory')); | ||
|
||
this._history = new ChatMessageHistory(); | ||
|
||
this._bufferMemory = new BufferMemory({ | ||
returnMessages: true, | ||
memoryKey: 'chat_history', | ||
chatHistory: this._history, | ||
}); | ||
|
||
return this; | ||
} | ||
} | ||
|
||
export default MemoryChatHistory; |
Oops, something went wrong.