-
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 11 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 |
---|---|---|
|
@@ -148,3 +148,15 @@ | |
``` | ||
|
||
This full source of this agent is defined in the [system.agent_github](/genaiscript/reference/scripts/system/#systemagent_github) system prompt. | ||
|
||
## Agent Memory | ||
|
||
The `agent_memory` is a special agent that queries the memories created by other agent conversations. It is used to store and retrieve information from the LLM's memory. | ||
|
||
All agent contribute to the conversation memory, and use the `agent_memory`, tool unless it is explicitely disabled using `disableMemory`. | ||
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 word "explicitely" is misspelled. It should be corrected to "explicitly".
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 word "explicitely" is misspelled; it should be "explicitly".
|
||
|
||
```js "disableMemory: true" | ||
defAgent(..., { 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. New section 'Agent Memory' added to provide details on the usage and configuration of the 'agent_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 code snippet provided for disabling memory should be enclosed in a code block for consistency with other code snippets in the documentation.
|
||
|
||
To enable agent memory in the top level script, add the `agent_memory` tool. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,8 +234,7 @@ | |
"github", | ||
"query GitHub to accomplish tasks", | ||
`Your are a helpfull LLM agent that can query GitHub to accomplish tasks. Answer the question in QUERY. | ||
- Prefer diffing job logs rather downloading entire logs which can be very large. | ||
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 seems to be a typo here. "ar" should be "are".
|
||
- Pull Requests ar a specialized type of issues. | ||
`, | ||
{ | ||
model, | ||
|
@@ -290,6 +289,57 @@ | |
````` | ||
|
||
|
||
### `system.agent_memory` | ||
|
||
agent that retrieves memories | ||
|
||
|
||
|
||
|
||
|
||
`````js wrap title="system.agent_memory" | ||
system({ | ||
title: "agent that retrieves 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
system({ | ||
title: "agent that retrieves 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) | ||
Check failure on line 382 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
|
||
trace.detailsFenced( | ||
`π§ memory: ${query}`, | ||
cachedValue.answer, | ||
"markdown" | ||
) | ||
}) | ||
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.