-
Notifications
You must be signed in to change notification settings - Fork 129
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
Add memory agent and caching functionality π§ β¨ #761
Changes from 7 commits
505163b
5885830
8979905
c3ed96d
dbb1be2
d27cc10
b04fbcf
cb2925d
37b1cb0
2f78c61
897139d
6420070
b2a20ba
File filter
Filter by extension
Conversations
Jump to
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ import { LinkCard } from '@astrojs/starlight/components'; | |
<LinkCard title="agent git" description="query a repository using Git to accomplish tasks. Provide all the context information available to execute git queries." href="/genaiscript/reference/scripts/system#systemagent_git" /> | ||
<LinkCard title="agent github" description="query GitHub to accomplish tasks" href="/genaiscript/reference/scripts/system#systemagent_github" /> | ||
<LinkCard title="agent interpreter" description="run code interpreters for Python, Math. Use this agent to ground computation questions." href="/genaiscript/reference/scripts/system#systemagent_interpreter" /> | ||
<LinkCard title="agent memory" description="queries the memories created by other agent conversations." href="/genaiscript/reference/scripts/system#systemagent_memory" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newly added LinkCard for "agent memory" should maintain consistency with the other LinkCard entries. Consider adding a period at the end of the description for uniformity.
|
||
<LinkCard title="agent user_input" description="ask user for input to confirm, select or answer the question in the query. The message should be very clear and provide all the context." href="/genaiscript/reference/scripts/system#systemagent_user_input" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,7 @@ | |
{ | ||
model, | ||
system: ["system.git_info", "system.github_info", "system.git"], | ||
memory: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 'memory' property has been added to the 'system.git_info', 'system.github_info', 'system.git' agent. Verify that this property addition is intentional and correctly documented.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memory feature enabled in the 'system.agent_git' agent configuration.
|
||
} | ||
) | ||
|
||
|
@@ -248,6 +249,7 @@ | |
"system.github_issues", | ||
"system.github_pulls", | ||
], | ||
memory: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 'memory' property has been added to the 'system.github_issues', 'system.github_pulls' agent. Verify that this property addition is intentional and correctly documented.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memory feature enabled in the 'system.agent_github' agent configuration.
|
||
} | ||
) | ||
|
||
|
@@ -290,6 +292,57 @@ | |
````` | ||
|
||
|
||
### `system.agent_memory` | ||
|
||
agent that retreives memories | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in the word "retreives"; it should be "retrieves".
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in the word "retreives"; it should be "retrieves".
|
||
|
||
|
||
|
||
|
||
|
||
`````js wrap title="system.agent_memory" | ||
system({ | ||
title: "agent that retreives memories", | ||
}) | ||
|
||
const cache = await host.cache("memory") | ||
defAgent( | ||
"memory", | ||
"queries the memories created by other agent conversations.", | ||
async (ctx) => { | ||
const memories = await cache.values() | ||
ctx.$`Your are a helpfull LLM agent that acts as a knowledge base for memories created by other agents. | ||
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Answer the question in QUERY with the memories in MEMORY. | ||
|
||
- Use the information in MEMORY exclusively to answer the question in QUERY. | ||
- If the information in MEMORY is not enough to answer the question in QUERY, respond <NO_MEMORY>. | ||
- The memory | ||
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in the word "helpfull"; it should be "helpful". Additionally, the sentence "The memory" in line 320 is incomplete and should be revised or removed.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The instructions within the code snippet may be unclear due to the incomplete sentence on line 320. This could lead to confusion about the expected behavior of the agent.
|
||
` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sentence "- The memory" is incomplete and seems to be cut off. It should be removed or completed.
|
||
memories.reverse().forEach( | ||
({ agent, query, answer }) => | ||
ctx.def( | ||
"MEMORY", | ||
`${agent}> ${query}? | ||
${answer} | ||
` | ||
), | ||
{ | ||
flex: 1, | ||
} | ||
) | ||
}, | ||
{ | ||
model: "openai:gpt-4o", | ||
flexTokens: 30000, | ||
system: ["system"], | ||
disableMemory: true, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A new section for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The section for
|
||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New section 'system.agent_memory' added to describe the agent that retrieves memories.
|
||
|
||
````` | ||
|
||
|
||
### `system.agent_user_input` | ||
|
||
Agent that can asks questions to the user. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,6 @@ defAgent( | |
{ | ||
model, | ||
system: ["system.git_info", "system.github_info", "system.git"], | ||
memory: true | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,6 @@ defAgent( | |
"system.github_issues", | ||
"system.github_pulls", | ||
], | ||
memory: true, | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
system({ | ||
title: "agent that retreives memories", | ||
}) | ||
|
||
const cache = await host.cache("memory") | ||
defAgent( | ||
"memory", | ||
"queries the memories created by other agent conversations.", | ||
async (ctx) => { | ||
const memories = await cache.values() | ||
ctx.$`Your are a helpfull LLM agent that acts as a knowledge base for memories created by other agents. | ||
|
||
Answer the question in QUERY with the memories in MEMORY. | ||
|
||
- Use the information in MEMORY exclusively to answer the question in QUERY. | ||
- If the information in MEMORY is not enough to answer the question in QUERY, respond <NO_MEMORY>. | ||
- The memory | ||
` | ||
memories.reverse().forEach( | ||
({ agent, query, answer }) => | ||
ctx.def( | ||
"MEMORY", | ||
`${agent}> ${query}? | ||
${answer} | ||
` | ||
), | ||
{ | ||
flex: 1, | ||
} | ||
) | ||
}, | ||
{ | ||
model: "openai:gpt-4o", | ||
flexTokens: 30000, | ||
system: ["system"], | ||
disableMemory: true, | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
import { parseModelIdentifier, resolveModelConnectionInfo } from "./models" | ||
import { | ||
CHAT_REQUEST_PER_MODEL_CONCURRENT_LIMIT, | ||
MEMORY_CACHE_NAME, | ||
MODEL_PROVIDER_AICI, | ||
SYSTEM_FENCE, | ||
} from "./constants" | ||
|
@@ -59,6 +60,7 @@ | |
import { dedent } from "./indent" | ||
import { runtimeHost } from "./host" | ||
import { writeFileEdits } from "./fileedits" | ||
import { MemoryCache } from "./cache" | ||
|
||
export function createChatTurnGenerationContext( | ||
options: GenerationOptions, | ||
|
@@ -313,7 +315,8 @@ | |
) => Promise<void>, | ||
options?: DefAgentOptions | ||
): void => { | ||
const { tools, system, ...rest } = options || {} | ||
const { tools, system, disableMemory, ...rest } = options || {} | ||
Check failure on line 318 in packages/core/src/runpromptcontext.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
const memory = !disableMemory | ||
|
||
name = name.replace(/^agent_/i, "") | ||
const agentName = `agent_${name}` | ||
|
@@ -327,7 +330,9 @@ | |
const agentTools = resolveTools( | ||
runtimeHost.project, | ||
agentSystem, | ||
arrayify(tools) | ||
[...arrayify(tools), memory ? "agent_memory" : undefined].filter( | ||
(t) => !!t | ||
) | ||
Check failure on line 335 in packages/core/src/runpromptcontext.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The filter function is unnecessary here as the 'undefined' values will not affect the functionality of the 'arrayify' function.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The filter function
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The expression used to generate the
|
||
) | ||
const agentDescription = dedent`Agent that uses an LLM to ${description}.\nAvailable tools: | ||
${agentTools.map((t) => `- ${t.description}`).join("\n")}` // DO NOT LEAK TOOL ID HERE | ||
|
@@ -347,17 +352,40 @@ | |
}, | ||
pelikhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async (args) => { | ||
const { context, query } = args | ||
logVerbose(`${agentLabel}: ${query}`) | ||
context.log(`${agentLabel}: ${query}`) | ||
const res = await runPrompt( | ||
async (_) => { | ||
_.def("QUERY", query) | ||
if (typeof fn === "string") _.writeText(dedent(fn)) | ||
else await fn(_, args) | ||
_.$`- Assume that your answer will be analyzed by an LLM, not a human. | ||
${memory ? `- If you are missing information, try querying the memory using 'agent_memory'.` : ""} | ||
- If you are missing information, reply "MISSING_INFO: <what is missing>". | ||
- If you cannot answer the query, return "NO_ANSWER: <reason>". | ||
- Be concise. Minimize output to the most relevant information to save context tokens. | ||
` | ||
if (memory) | ||
_.defOutputProcessor(async ({ text }) => { | ||
const agentMemory = MemoryCache.byName< | ||
{ agent: string; query: string }, | ||
{ | ||
agent: string | ||
query: string | ||
answer: string | ||
} | ||
>(MEMORY_CACHE_NAME) | ||
const cacheKey = { agent: agentName, query } | ||
const cachedValue = { | ||
...cacheKey, | ||
answer: text, | ||
} | ||
await agentMemory.set(cacheKey, cachedValue) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
trace.detailsFenced( | ||
`π§ memory: ${query}`, | ||
cachedValue.answer, | ||
"markdown" | ||
) | ||
}) | ||
Check failure on line 388 in packages/core/src/runpromptcontext.ts GitHub Actions / build
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no error handling for the async function 'agentMemory.set'. If it fails, it could lead to unexpected behavior.
|
||
}, | ||
{ | ||
label: agentLabel, | ||
|
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.
A new agent 'agent memory' has been added to the documentation. Ensure that the description and link are accurate and consistent with the rest of the documentation.