Skip to content

Commit

Permalink
Add memory agent and caching functionality 🧠✨
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 8, 2024
1 parent 35d4773 commit 505163b
Show file tree
Hide file tree
Showing 23 changed files with 144 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/src/components/BuiltinAgents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<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" />
53 changes: 52 additions & 1 deletion docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defAgent(
system: ["system.explanations", "system.github_info"],
tools: [
"md_find_files",
"md_read_frontmatterm",
"md_read_frontmatter",
"fs_find_files",
"fs_read_file",
],
Expand Down Expand Up @@ -290,6 +290,57 @@ defAgent(
`````


### `system.agent_memory`

agent that retreives memories





`````js wrap title="system.agent_memory"
system({
title: "agent that retreives memories",
})

const cache = await workspace.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"],
}
)

`````


### `system.agent_user_input`

Agent that can asks questions to the user.
Expand Down
2 changes: 2 additions & 0 deletions eval/extrism/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/auto/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,5 @@ export const CLI_ENV_VAR_RX = /^genaiscript_var_/i

export const GIT_DIFF_MAX_TOKENS = 8000
export const MAX_TOOL_CONTENT_TOKENS = 4000

export const MEMORY_CACHE_NAME = "memory"
2 changes: 2 additions & 0 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions packages/core/src/genaisrc/system.agent_memory.genai.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
system({
title: "agent that retreives memories",
})

const cache = await workspace.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"],
}
)
16 changes: 15 additions & 1 deletion packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import { parseModelIdentifier, resolveModelConnectionInfo } from "./models"
import {
CHAT_REQUEST_PER_MODEL_CONCURRENT_LIMIT,
MEMORY_CACHE_NAME,
MODEL_PROVIDER_AICI,
SYSTEM_FENCE,
} from "./constants"
Expand All @@ -59,6 +60,8 @@ import { Project } from "./ast"
import { dedent } from "./indent"
import { runtimeHost } from "./host"
import { writeFileEdits } from "./fileedits"
import { JSONLineCache } from "./cache"
import { string } from "mathjs"

export function createChatTurnGenerationContext(
options: GenerationOptions,
Expand Down Expand Up @@ -347,7 +350,7 @@ export function createChatGenerationContext(
},
async (args) => {
const { context, query } = args
logVerbose(`${agentLabel}: ${query}`)
context.log(`${agentLabel}: ${query}`)
const res = await runPrompt(
async (_) => {
_.def("QUERY", query)
Expand All @@ -358,6 +361,17 @@ export function createChatGenerationContext(
- If you cannot answer the query, return "NO_ANSWER: <reason>".
- Be concise. Minimize output to the most relevant information to save context tokens.
`
_.defOutputProcessor(async ({ text }) => {
const agentMemory = JSONLineCache.byName<
{ agent: string; query: string },
{ answer: string }
>(MEMORY_CACHE_NAME)
const cacheKey = { agent: agentName, query }
agentMemory.set(cacheKey, {
...cacheKey,
answer: text,
})
})
},
{
label: agentLabel,
Expand Down
2 changes: 2 additions & 0 deletions packages/sample/genaisrc/blog/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/src/errors/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/src/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/sample/src/makecode/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 505163b

Please sign in to comment.