Skip to content

enhance: filter existing memories on create #637

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion memory/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/obot-platform/tools/memory
go 1.24.1

require (
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250424204937-af453989e88f
github.com/obot-platform/obot v0.7.1
github.com/obot-platform/obot/apiclient v0.0.0-20250422181343-7b44bf761ab4
)
Expand All @@ -11,7 +12,6 @@ require (
github.com/getkin/kin-openapi v0.129.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250331192455-415de950d72d // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
Expand All @@ -23,3 +23,5 @@ require (
golang.org/x/sys v0.29.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/obot-platform/obot/apiclient => github.com/njhale/obot/apiclient v0.0.0-20250430070646-13d08e96a3d4
8 changes: 4 additions & 4 deletions memory/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+Gr
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250331192455-415de950d72d h1:S9gfdnk0VK3dSFOHn/rhPWBbbOoC64uy38rRKIPLLcQ=
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250331192455-415de950d72d/go.mod h1:QvGPZoRuAiA8P5EzPI05kTrs+LZ0ipHywUGsKruSknw=
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250424204937-af453989e88f h1:JG/uszEGS99hQeajV+h87M2vfQtIONhbdVCC8dpL50E=
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250424204937-af453989e88f/go.mod h1:QvGPZoRuAiA8P5EzPI05kTrs+LZ0ipHywUGsKruSknw=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand All @@ -22,14 +22,14 @@ github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/njhale/obot/apiclient v0.0.0-20250430070646-13d08e96a3d4 h1:7QjOlKSG8MssqAcbQhqNmrTfODteTLJOwDzrYY4VvpM=
github.com/njhale/obot/apiclient v0.0.0-20250430070646-13d08e96a3d4/go.mod h1:6xuT2XK3SdzTxEk8PSpE9KI6Kf/RDf/jw6wRaHNs6m0=
github.com/oasdiff/yaml v0.0.0-20241210131133-6b86fb107d80 h1:nZspmSkneBbtxU9TopEAE0CY+SBJLxO8LPUlw2vG4pU=
github.com/oasdiff/yaml v0.0.0-20241210131133-6b86fb107d80/go.mod h1:7tFDb+Y51LcDpn26GccuUgQXUk6t0CXZsivKjyimYX8=
github.com/oasdiff/yaml3 v0.0.0-20241210130736-a94c01f36349 h1:t05Ww3DxZutOqbMN+7OIuqDwXbhl32HiZGpLy26BAPc=
github.com/oasdiff/yaml3 v0.0.0-20241210130736-a94c01f36349/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
github.com/obot-platform/obot v0.7.1 h1:sxE0Se8Vw2+IQW418yumFGCN7ayAyRPeXZbMSIamDz8=
github.com/obot-platform/obot v0.7.1/go.mod h1:qr3POCZBYQ6Xm8ZuRdzkIQuuO+FHqHfJkHTKP2C0VCI=
github.com/obot-platform/obot/apiclient v0.0.0-20250422181343-7b44bf761ab4 h1:WScX8iJZAX9yQ7oFZbISjgt1x/q/HI1frpnKVBIYkzc=
github.com/obot-platform/obot/apiclient v0.0.0-20250422181343-7b44bf761ab4/go.mod h1:6xuT2XK3SdzTxEk8PSpE9KI6Kf/RDf/jw6wRaHNs6m0=
github.com/obot-platform/obot/logger v0.0.0-20241217130503-4004a5c69f32 h1:PooNaWBm+SVJ5nif7t9eAy/bBgN5r6x2tWW9Za51gKk=
github.com/obot-platform/obot/logger v0.0.0-20241217130503-4004a5c69f32/go.mod h1:mstAp0dK+o0h8AGcfFjyMOwTc0DgIFnKldBARfYYQzI=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
Expand Down
71 changes: 61 additions & 10 deletions memory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/gptscript-ai/go-gptscript"
"github.com/obot-platform/obot/apiclient"
"github.com/obot-platform/obot/pkg/system"
)
Expand All @@ -29,17 +30,69 @@ func main() {
}
}

const filterMemoryPrompt = `
Without providing any additional dialog, respond with a single line of text containing a refined version of the information in NEW_MEMORY that is not already provided by MEMORIES. The refined text should be as concise as possible. If all of the information in NEW_MEMORY is already provided by MEMORIES, do not respond.
<NEW_MEMORY>
%s
</NEW_MEMORY>
<MEMORIES>
%s
</MEMORIES>
`

func create(ctx context.Context, c *apiclient.Client, projectID, content string) error {
if content == "" {
return fmt.Errorf("missing content to remember")
}
content = strings.ReplaceAll(content, "\n", " ")

gc, err := gptscript.NewGPTScript()
if err != nil {
return fmt.Errorf("failed to create gptscript: %v", err)
}

result, err := c.ListMemories(ctx, assistantID, projectID)
if err != nil {
return fmt.Errorf("failed to list current memories: %v", err)
}

// Add each memory as a CSV row
var sb strings.Builder
if result != nil && len(result.Items) > 0 {
for _, memory := range result.Items {
sb.WriteString(fmt.Sprintf("%q\n", strings.ReplaceAll(memory.Content, "\n", " ")))
}
}

resp, err := gc.Evaluate(ctx, gptscript.Options{
GlobalOptions: gptscript.GlobalOptions{
OpenAIAPIKey: os.Getenv("OPENAI_API_KEY"),
OpenAIBaseURL: os.Getenv("OPENAI_BASE_URL"),
},
}, gptscript.ToolDef{
Name: "filter",
ModelName: os.Getenv("OBOT_DEFAULT_LLM_MINI_MODEL"),
Instructions: fmt.Sprintf(filterMemoryPrompt, content, sb.String()),
})
if err != nil {
return fmt.Errorf("failed to filter memory: %v", err)
}
defer resp.Close()

memory, err := c.CreateMemory(ctx, assistantID, projectID, content)
filtered, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to get text from response: %v", err)
}
if filtered == "" {
fmt.Printf("memory already exists")
return nil
}

if _, err := c.CreateMemory(ctx, assistantID, projectID, filtered); err != nil {
return fmt.Errorf("failed to create memory: %v", err)
}

fmt.Printf("memory %q created", memory.ID)
fmt.Printf("memory created")

return nil
}
Expand All @@ -53,12 +106,11 @@ func update(ctx context.Context, c *apiclient.Client, projectID, content string)
return fmt.Errorf("missing content to remember")
}

memory, err := c.UpdateMemory(ctx, assistantID, projectID, memoryID, content)
if err != nil {
if _, err := c.UpdateMemory(ctx, assistantID, projectID, memoryID, content); err != nil {
return fmt.Errorf("failed to update memory: %v", err)
}

fmt.Printf("memory %q updated", memory.ID)
fmt.Printf("memory updated")

return nil
}
Expand All @@ -68,11 +120,11 @@ func delete(ctx context.Context, c *apiclient.Client, projectID string) error {
return fmt.Errorf("missing memory_id")
}

if err := c.DeleteMemory(ctx, assistantID, projectID, memoryID); err != nil {
if _, err := c.DeleteMemory(ctx, assistantID, projectID, memoryID); err != nil {
return fmt.Errorf("failed to delete memory: %v", err)
}

fmt.Printf("memory %q deleted", memoryID)
fmt.Printf("memory deleted")

return nil
}
Expand All @@ -87,11 +139,10 @@ func list(ctx context.Context, c *apiclient.Client, projectID string) error {
sb.WriteString("Below are memories for you to reference when crafting responses to the user:\n")
sb.WriteString("<MEMORIES>\n")

// Add header row
sb.WriteString("memory_id, content\n")

// Add each memory as a CSV row
if result != nil && len(result.Items) > 0 {
// Add header row
sb.WriteString("memory_id, content\n")
for _, memory := range result.Items {
sb.WriteString(fmt.Sprintf("%s, %s\n", memory.ID, memory.Content))
}
Expand Down
2 changes: 2 additions & 0 deletions memory/tool.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Metadata: category: Capability
Name: Create Memory
Description: Store information in agent memory
Param: content: The content to remember
Metadata: noUserAuth: sys.model.provider.credential
Credentials: github.com/gptscript-ai/credentials/model-provider

#!${GPTSCRIPT_TOOL_DIR}/bin/gptscript-go-tool create

Expand Down
Loading