-
Notifications
You must be signed in to change notification settings - Fork 12
Change agent architecture #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rishabhjaiz
wants to merge
1
commit into
main
Choose a base branch
from
rishabh/change-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,168 @@ | ||
| import { | ||
| Memory, | ||
| MemoryCount, | ||
| MemoryType, | ||
| Observation, | ||
| Reflection, | ||
| } from "../interface"; | ||
| import { Engine } from "../Engine/engine"; | ||
| import { dateToString } from "../utils/dateToString"; | ||
| import { updateLocalStorageWithMemory } from "../utils/updateLocalStorageWithMemory"; | ||
| import { nanoid } from "nanoid"; | ||
|
|
||
| export class Agent { | ||
| // Id to uniquely identify the agent. This is something the user should not care about and we would generate this whenever a new agent is initalised. | ||
| id: string; | ||
|
|
||
| // Model responsible for handling agent responses. | ||
| engine: Engine; | ||
|
|
||
| // Description about the agent. Keeping it as string for now will see how it's going. | ||
| description: string; | ||
|
|
||
| // Core of the agent which stores it's memories this will evolve to do a lot of things later. | ||
| memoryStream: Memory[]; | ||
|
|
||
| // TODO: Move this to state later on. | ||
| importanceScoreSumSinceLastPurge: number; | ||
|
|
||
| constructor(id: string, engine: Engine, description: string) { | ||
| this.id = id; | ||
| this.engine = engine; | ||
|
|
||
| // Check everytime if the memory with the associated agent id exists in the database or localstorage for now. | ||
| this.memoryStream = localStorage.getItem(id) | ||
| ? JSON.parse(localStorage.getItem(id) as string) | ||
| : []; | ||
| this.importanceScoreSumSinceLastPurge = 0; | ||
| this.description = description; | ||
| } | ||
|
|
||
| // Add a observation to the agents memory stream. | ||
| observe = async (description: string) => { | ||
| const importance = await this.engine.getImportanceScore(description); | ||
| const embedding = await this.engine.createEmbedding(description); | ||
|
|
||
| const memory: Observation = { | ||
| id: nanoid(), | ||
| createdAt: dateToString(new Date()), | ||
| description, | ||
| importance, | ||
| latestAccess: dateToString(new Date()), | ||
| embedding, | ||
| type: MemoryType.OBSERVATION, | ||
| }; | ||
|
|
||
| // Add memory stream to local storage for now later replace the logic with data base code later. | ||
| this.memoryStream = updateLocalStorageWithMemory({ id: this.id, memory }); | ||
| this.importanceScoreSumSinceLastPurge += importance; | ||
| }; | ||
|
|
||
| // Reflect on the memory periodically. | ||
| reflect = async () => { | ||
| // Get the latest 100 Memories. | ||
| const memories = this.memoryStream.slice(-100); | ||
|
|
||
| // convert to a description string | ||
| const description = memories.map((memory) => memory.description).join("\n"); | ||
|
|
||
| // get 5 top-level questions | ||
| const questions = await this.engine.getSalientQuestions(description, 5); | ||
|
|
||
| // for each relevant question, find the top closest memories using the retrieval score | ||
| const currentTime = new Date(); | ||
|
|
||
| for (const question of questions) { | ||
| const queryEmbedding = await this.engine.createEmbedding(question); | ||
| const retrievalScores = await Promise.all( | ||
| memories.map(async (memory) => ({ | ||
| memory, | ||
| score: await this.engine.getRetrievalScore( | ||
| memory.importance, | ||
| queryEmbedding, | ||
| memory.embedding, | ||
| new Date(memory.latestAccess), | ||
| currentTime | ||
| ), | ||
| })) | ||
| ); | ||
|
|
||
| // Sort memories by retrieval score and take the top 3 | ||
| const topMemories = retrievalScores | ||
| .sort((a, b) => b.score - a.score) | ||
| .slice(0, 3) | ||
| .map((item) => item.memory); | ||
|
|
||
| // consider them retrieved, so update their latestAccess in the actual memory stream | ||
| topMemories.forEach((memory) => { | ||
| const index = this.memoryStream.findIndex( | ||
| (item) => item.id === memory.id | ||
| ); | ||
| this.memoryStream[index].latestAccess = dateToString(currentTime); | ||
| }); | ||
|
|
||
| const importance = await this.engine.getImportanceScore(question); | ||
|
|
||
| // Create a reflection for each relevant question and add evidence accordingly | ||
| const reflection: Reflection = { | ||
| id: nanoid(), | ||
| createdAt: dateToString(currentTime), | ||
| description: question, | ||
| importance, | ||
| latestAccess: dateToString(currentTime), | ||
| embedding: queryEmbedding, | ||
| type: MemoryType.REFLECTION, | ||
| evidence: topMemories.map((memory) => memory.id), | ||
| }; | ||
|
|
||
| // Update other states | ||
| this.memoryStream = updateLocalStorageWithMemory({ | ||
| memory: reflection, | ||
| id: this.id, | ||
| }); | ||
| } | ||
|
|
||
| // purge importance score sum | ||
| this.importanceScoreSumSinceLastPurge = 0; | ||
| }; | ||
|
|
||
| // Reply based on the current message and the memory stream accquired. | ||
| conversation = async ( | ||
| message: string, | ||
| /** Think: LLM's are always designed to have an output but in case of agent there might be some questions it would not want to answer how to think about that have some importance threshold for a question as well? */ | ||
| /** One of the participants would always be the agent and in case of conversational bots only would be the user */ | ||
| participants: string[] = ["user", "agent"] | ||
| // Should the participants list be associated with an id? How do we uniquely identify the agent and the user? | ||
| ): Promise<string> => { | ||
| const memories = this.memoryStream.slice(-100); | ||
|
|
||
| // Convert memories to a context string | ||
| const context = memories.map((memory) => memory.description).join("\n"); | ||
|
|
||
| // Call the engine's reply method with the given message and context | ||
| const reply = await this.engine.reply(message, this, context); | ||
|
|
||
| const memoryDescription = `A converstion between ${participants.join(", ")}. | ||
| They said: ${message} | ||
| You replied: ${reply} | ||
| `; | ||
|
|
||
| // add reply to memory stream | ||
| this.memoryStream = updateLocalStorageWithMemory({ | ||
| id: this.id, | ||
| memory: { | ||
| id: nanoid(), | ||
| createdAt: dateToString(new Date()), | ||
| description: memoryDescription, | ||
| type: MemoryType.CONVERSATION, | ||
| embedding: await this.engine.createEmbedding(reply), | ||
| importance: await this.engine.getImportanceScore(reply), | ||
| latestAccess: dateToString(new Date()), | ||
| participants: participants, | ||
| }, | ||
| }); | ||
|
|
||
| // Return the generated reply | ||
| return reply; | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the item key should be more indicative when stored in local storage and not just id. Something like -
agent_234524for DB it makes sense since it will be in a labelled column (agent_id) but this is not true for localdb,