From e7d82321f08064604fd9956cf0c483c4be1a2105 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Thu, 28 Sep 2023 17:36:04 -0600 Subject: [PATCH 01/15] custom summary prompt in env functionality --- config/config.go | 9 +++++---- config/models.go | 3 ++- pkg/extractors/summarizer.go | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index def08e27..e2da7bdf 100644 --- a/config/config.go +++ b/config/config.go @@ -16,10 +16,11 @@ var log = logrus.New() // EnvVars is a set of secrets that should be stored in the environment, not config file var EnvVars = map[string]string{ - "llm.anthropic_api_key": "ZEP_ANTHROPIC_API_KEY", - "llm.openai_api_key": "ZEP_OPENAI_API_KEY", - "auth.secret": "ZEP_AUTH_SECRET", - "development": "ZEP_DEVELOPMENT", + "llm.anthropic_api_key": "ZEP_ANTHROPIC_API_KEY", + "llm.openai_api_key": "ZEP_OPENAI_API_KEY", + "auth.secret": "ZEP_AUTH_SECRET", + "development": "ZEP_DEVELOPMENT", + "extractors.messages.summarizer.custom_prompt": "ZEP_SUMMARIZER_CUSTOM_PROMPT", } // LoadConfig loads the config file and ENV variables into a Config struct diff --git a/config/models.go b/config/models.go index 517f1d32..68c8ab7b 100644 --- a/config/models.go +++ b/config/models.go @@ -87,7 +87,8 @@ type DocumentExtractorsConfig struct { } type SummarizerConfig struct { - Enabled bool `mapstructure:"enabled"` + Enabled bool `mapstructure:"enabled"` + CustomPrompt string `mapstructure:"custom_prompt"` } type EmbeddingsConfig struct { diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index 172c2b6a..e5e86e7b 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -277,15 +277,42 @@ func incrementalSummarizer( } var summaryPromptTemplate string + + customPrompt := appState.Config.Extractors.Messages.Summarizer.CustomPrompt + isCustomPromptSet := strings.TrimSpace(customPrompt) != "" + + prevSummaryIdentifier := "{{.PrevSummary}}" + messagesJoinedIdentifier := "{{.MessagesJoined}}" + + isCustomPromptValid := strings.Contains(customPrompt, prevSummaryIdentifier) + && strings.Contains(customPrompt, messagesJoinedIdentifier) + + if isCustomPromptSet && !isCustomPromptValid { + log.Warn( + "A custom summarizer prompt was set but the format used is wrong. Using default prompt instead." + + "Please make sure the prompt contains the identifiers {{.PrevSummary}} and {{.MessagesJoined}}.", + ) + } + + useCustomPrompt := isCustomPromptSet && isCustomPromptValid + switch appState.Config.LLM.Service { case "openai": - summaryPromptTemplate = summaryPromptTemplateOpenAI + if useCustomPrompt { + summaryPromptTemplate = customPrompt + } else { + summaryPromptTemplate = summaryPromptTemplateOpenAI + } case "anthropic": - summaryPromptTemplate = summaryPromptTemplateAnthropic + if useCustomPrompt { + summaryPromptTemplate = customPrompt + } else { + summaryPromptTemplate = summaryPromptTemplateAnthropic + } default: return "", 0, fmt.Errorf("unknown LLM service: %s", appState.Config.LLM.Service) } - + progressivePrompt, err := internal.ParsePrompt(summaryPromptTemplate, promptData) if err != nil { return "", 0, err From 1e6b50d2773583d92eb29630d9a21130145c8ee4 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Thu, 28 Sep 2023 17:46:19 -0600 Subject: [PATCH 02/15] fix --- pkg/extractors/summarizer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index e5e86e7b..18fcc59d 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -284,8 +284,8 @@ func incrementalSummarizer( prevSummaryIdentifier := "{{.PrevSummary}}" messagesJoinedIdentifier := "{{.MessagesJoined}}" - isCustomPromptValid := strings.Contains(customPrompt, prevSummaryIdentifier) - && strings.Contains(customPrompt, messagesJoinedIdentifier) + isCustomPromptValid := strings.Contains(customPrompt, prevSummaryIdentifier) && + strings.Contains(customPrompt, messagesJoinedIdentifier) if isCustomPromptSet && !isCustomPromptValid { log.Warn( From ce2581f3bf17d9f9ea2f585a221bbd57fc898e6e Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Thu, 28 Sep 2023 17:48:55 -0600 Subject: [PATCH 03/15] missing space --- pkg/extractors/summarizer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index 18fcc59d..22f9e1e5 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -290,7 +290,7 @@ func incrementalSummarizer( if isCustomPromptSet && !isCustomPromptValid { log.Warn( "A custom summarizer prompt was set but the format used is wrong. Using default prompt instead." + - "Please make sure the prompt contains the identifiers {{.PrevSummary}} and {{.MessagesJoined}}.", + " Please make sure the prompt contains the identifiers {{.PrevSummary}} and {{.MessagesJoined}}.", ) } From 4cab3951712a085dd30b16ae0113a5a944edfae6 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Thu, 28 Sep 2023 18:18:40 -0600 Subject: [PATCH 04/15] indent --- pkg/extractors/summarizer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index 22f9e1e5..e11746c9 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -290,7 +290,7 @@ func incrementalSummarizer( if isCustomPromptSet && !isCustomPromptValid { log.Warn( "A custom summarizer prompt was set but the format used is wrong. Using default prompt instead." + - " Please make sure the prompt contains the identifiers {{.PrevSummary}} and {{.MessagesJoined}}.", + " Please make sure the prompt contains the identifiers {{.PrevSummary}} and {{.MessagesJoined}}.", ) } From f326304e54e9b61bf76aa5bac1055e4e98e64364 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:28:50 -0600 Subject: [PATCH 05/15] using config instead of env --- config.yaml | 43 ++++++++++++++++++++++++++++-- config/models.go | 30 ++++++++++++++------- pkg/extractors/prompts.go | 17 ++++++++++-- pkg/extractors/summarizer.go | 51 ++++++++++++++++-------------------- 4 files changed, 99 insertions(+), 42 deletions(-) diff --git a/config.yaml b/config.yaml index d406a0d0..03bae800 100644 --- a/config.yaml +++ b/config.yaml @@ -28,8 +28,8 @@ extractors: enabled: true dimensions: 384 service: "local" -# dimensions: 1536 -# service: "openai" + # dimensions: 1536 + # service: "openai" messages: summarizer: enabled: true @@ -67,3 +67,42 @@ data: purge_every: 60 log: level: "info" +# Custom Prompts Configuration +# Allows customization of extractor prompts. +custom_prompts: + summarizer_prompts: + # Anthropic Guidelines: + # - Use XML-style tags like as element identifiers. + # - Include {{.PrevSummary}} and {{.MessagesJoined}} as templated variables. + # - Clearly explain model instructions, e.g., "Review content inside tags". + # - Provide a clear example within the prompt. + # + # Example format: + # anthropic: | + # + # + # + # + # {{.PrevSummary}} + # {{.MessagesJoined}} + # Response without preamble. + # + # If left empty, the default Anthropic summary prompt from zep/pkg/extractors/prompts.go will be used. + anthropic: | + + # OpenAI summarizer prompt configuration. + # Guidelines: + # - Include {{.PrevSummary}} and {{.MessagesJoined}} as templated variables. + # - Provide a clear example within the prompt. + # + # Example format: + # openai: | + # + # Example: + # + # Current summary: {{.PrevSummary}} + # New lines of conversation: {{.MessagesJoined}} + # New summary:` + # + # If left empty, the default OpenAI summary prompt from zep/pkg/extractors/prompts.go will be used. + openai: | diff --git a/config/models.go b/config/models.go index 68c8ab7b..b2e7bde2 100644 --- a/config/models.go +++ b/config/models.go @@ -3,16 +3,26 @@ package config // Config holds the configuration of the application // Use cmd.NewConfig to create a new instance type Config struct { - LLM LLM `mapstructure:"llm"` - NLP NLP `mapstructure:"nlp"` - Memory MemoryConfig `mapstructure:"memory"` - Extractors ExtractorsConfig `mapstructure:"extractors"` - Store StoreConfig `mapstructure:"store"` - Server ServerConfig `mapstructure:"server"` - Log LogConfig `mapstructure:"log"` - Auth AuthConfig `mapstructure:"auth"` - DataConfig DataConfig `mapstructure:"data"` - Development bool `mapstructure:"development"` + LLM LLM `mapstructure:"llm"` + NLP NLP `mapstructure:"nlp"` + Memory MemoryConfig `mapstructure:"memory"` + Extractors ExtractorsConfig `mapstructure:"extractors"` + Store StoreConfig `mapstructure:"store"` + Server ServerConfig `mapstructure:"server"` + Log LogConfig `mapstructure:"log"` + Auth AuthConfig `mapstructure:"auth"` + DataConfig DataConfig `mapstructure:"data"` + Development bool `mapstructure:"development"` + CustomPrompts CustomPromptsConfig +} + +type CustomPromptConfig struct { + SummarizerPrompts ExtractorPromptsConfig `mapstructure:"summarizer_prompts"` +} + +type ExtractorPromptsConfig struct { + OpenAI string `mapstructure:"openai"` + Anthropic string `mapstructure:"anthropic"` } type StoreConfig struct { diff --git a/pkg/extractors/prompts.go b/pkg/extractors/prompts.go index 4e98592a..45ec6e6c 100644 --- a/pkg/extractors/prompts.go +++ b/pkg/extractors/prompts.go @@ -16,7 +16,7 @@ type IntentPromptTemplateData struct { Input string } -const summaryPromptTemplateAnthropic = ` +const defaultSummaryPromptTemplateAnthropic = ` Review the Current Summary inside XML tags, and the New Lines of the provided conversation inside the XML tags. Create a concise summary of the conversation, adding from the to the . @@ -47,7 +47,7 @@ singer and lists the founding members as Jimmy Page, John Paul Jones, and John B Provide a response immediately without preamble. ` -const summaryPromptTemplateOpenAI = ` +const defaultSummaryPromptTemplateOpenAI = ` Review the Current Content, if there is one, and the New Lines of the provided conversation. Create a concise summary of the conversation, adding from the New Lines to the Current summary. If the New Lines are meaningless, return the Current Content. @@ -72,6 +72,19 @@ New lines of conversation: New summary: ` +summaryPromptTemplateAnthropic := defaultSummaryPromptTemplateAnthropic +summaryPromptTemplateOpenAI := defaultSummaryPromptTemplateOpenAI + +func init() { + if appState.Config.CustomPrompts.SummarizerPrompt.Anthropic != "" { + SummaryPromptTemplateAnthropic = config.CustomSummaryPromptTemplateAnthropic + } + + if appState.Config.CustomPrompts.SummarizerPrompt.OpenAI != "" { + SummaryPromptTemplateOpenAI = config.CustomSummaryPromptTemplateOpenAI + } +} + type SummaryPromptTemplateData struct { PrevSummary string MessagesJoined string diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index e11746c9..5bfe81e2 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -250,6 +250,22 @@ func processOverLimitMessages( }, nil } +prevSummaryIdentifier := "{{.PrevSummary}}" +messagesJoinedIdentifier := "{{.MessagesJoined}}" + +func validateSummarizerPrompt(prompt string) error { + isCustomPromptValid := strings.Contains(prompt, prevSummaryIdentifier) && + strings.Contains(prompt, messagesJoinedIdentifier) + + if !isCustomPromptValid { + return fmt.Errorf( + "Wrong summary prompt format. Please make sure it contains the identifiers %s and %s", + prevSummaryIdentifier, messagesJoinedIdentifier, + ) + } + return nil +} + // incrementalSummarizer takes a slice of messages and a summary, calls the LLM, // and returns a new summary enriched with the messages content. Summary can be // an empty string. Returns a string with the new summary and the number of @@ -278,40 +294,19 @@ func incrementalSummarizer( var summaryPromptTemplate string - customPrompt := appState.Config.Extractors.Messages.Summarizer.CustomPrompt - isCustomPromptSet := strings.TrimSpace(customPrompt) != "" - - prevSummaryIdentifier := "{{.PrevSummary}}" - messagesJoinedIdentifier := "{{.MessagesJoined}}" - - isCustomPromptValid := strings.Contains(customPrompt, prevSummaryIdentifier) && - strings.Contains(customPrompt, messagesJoinedIdentifier) - - if isCustomPromptSet && !isCustomPromptValid { - log.Warn( - "A custom summarizer prompt was set but the format used is wrong. Using default prompt instead." + - " Please make sure the prompt contains the identifiers {{.PrevSummary}} and {{.MessagesJoined}}.", - ) - } - - useCustomPrompt := isCustomPromptSet && isCustomPromptValid - switch appState.Config.LLM.Service { case "openai": - if useCustomPrompt { - summaryPromptTemplate = customPrompt - } else { - summaryPromptTemplate = summaryPromptTemplateOpenAI - } + summaryPromptTemplate = summaryPromptTemplateOpenAI case "anthropic": - if useCustomPrompt { - summaryPromptTemplate = customPrompt - } else { - summaryPromptTemplate = summaryPromptTemplateAnthropic - } + summaryPromptTemplate = summaryPromptTemplateAnthropic default: return "", 0, fmt.Errorf("unknown LLM service: %s", appState.Config.LLM.Service) } + + err := validateSummarizerPrompt(summaryPromptTemplate) + if err != nil { + return "", 0, err + } progressivePrompt, err := internal.ParsePrompt(summaryPromptTemplate, promptData) if err != nil { From cfe0e34efeade13f197c61c866a0e5ffcb9e34e8 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:30:47 -0600 Subject: [PATCH 06/15] indent --- config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.yaml b/config.yaml index 03bae800..363121ef 100644 --- a/config.yaml +++ b/config.yaml @@ -28,8 +28,8 @@ extractors: enabled: true dimensions: 384 service: "local" - # dimensions: 1536 - # service: "openai" +# dimensions: 1536 +# service: "openai" messages: summarizer: enabled: true From dfbaa03058de9b3244fc4ca88ea8164439af1123 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:32:49 -0600 Subject: [PATCH 07/15] indent --- pkg/extractors/summarizer.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index 5bfe81e2..9812d314 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -293,12 +293,11 @@ func incrementalSummarizer( } var summaryPromptTemplate string - switch appState.Config.LLM.Service { case "openai": summaryPromptTemplate = summaryPromptTemplateOpenAI case "anthropic": - summaryPromptTemplate = summaryPromptTemplateAnthropic + summaryPromptTemplate = summaryPromptTemplateAnthropic default: return "", 0, fmt.Errorf("unknown LLM service: %s", appState.Config.LLM.Service) } From 76b3d14b35b9ac568d4afdf2af8ed942bdfc8bdb Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:34:41 -0600 Subject: [PATCH 08/15] fix --- config/models.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/config/models.go b/config/models.go index b2e7bde2..bccf333f 100644 --- a/config/models.go +++ b/config/models.go @@ -3,17 +3,17 @@ package config // Config holds the configuration of the application // Use cmd.NewConfig to create a new instance type Config struct { - LLM LLM `mapstructure:"llm"` - NLP NLP `mapstructure:"nlp"` - Memory MemoryConfig `mapstructure:"memory"` - Extractors ExtractorsConfig `mapstructure:"extractors"` - Store StoreConfig `mapstructure:"store"` - Server ServerConfig `mapstructure:"server"` - Log LogConfig `mapstructure:"log"` - Auth AuthConfig `mapstructure:"auth"` - DataConfig DataConfig `mapstructure:"data"` - Development bool `mapstructure:"development"` - CustomPrompts CustomPromptsConfig + LLM LLM `mapstructure:"llm"` + NLP NLP `mapstructure:"nlp"` + Memory MemoryConfig `mapstructure:"memory"` + Extractors ExtractorsConfig `mapstructure:"extractors"` + Store StoreConfig `mapstructure:"store"` + Server ServerConfig `mapstructure:"server"` + Log LogConfig `mapstructure:"log"` + Auth AuthConfig `mapstructure:"auth"` + DataConfig DataConfig `mapstructure:"data"` + Development bool `mapstructure:"development"` + CustomPrompts CustomPromptsConfig `mapstructure:"custom_prompts"` } type CustomPromptConfig struct { From c0411bddaa4ea7c313ddcf7331b2cd5032fe2f9d Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:45:16 -0600 Subject: [PATCH 09/15] removing env var --- config/config.go | 1 - 1 file changed, 1 deletion(-) diff --git a/config/config.go b/config/config.go index e2da7bdf..637cda7a 100644 --- a/config/config.go +++ b/config/config.go @@ -20,7 +20,6 @@ var EnvVars = map[string]string{ "llm.openai_api_key": "ZEP_OPENAI_API_KEY", "auth.secret": "ZEP_AUTH_SECRET", "development": "ZEP_DEVELOPMENT", - "extractors.messages.summarizer.custom_prompt": "ZEP_SUMMARIZER_CUSTOM_PROMPT", } // LoadConfig loads the config file and ENV variables into a Config struct From 0a169544d1ece73bd57a1590ce9ed72bc1580987 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:45:57 -0600 Subject: [PATCH 10/15] indent --- config/config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 637cda7a..def08e27 100644 --- a/config/config.go +++ b/config/config.go @@ -16,10 +16,10 @@ var log = logrus.New() // EnvVars is a set of secrets that should be stored in the environment, not config file var EnvVars = map[string]string{ - "llm.anthropic_api_key": "ZEP_ANTHROPIC_API_KEY", - "llm.openai_api_key": "ZEP_OPENAI_API_KEY", - "auth.secret": "ZEP_AUTH_SECRET", - "development": "ZEP_DEVELOPMENT", + "llm.anthropic_api_key": "ZEP_ANTHROPIC_API_KEY", + "llm.openai_api_key": "ZEP_OPENAI_API_KEY", + "auth.secret": "ZEP_AUTH_SECRET", + "development": "ZEP_DEVELOPMENT", } // LoadConfig loads the config file and ENV variables into a Config struct From 4b335176014d121a488b0eaa21b4bc6f13a0e7bc Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Fri, 29 Sep 2023 22:47:16 -0600 Subject: [PATCH 11/15] clean --- config/models.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config/models.go b/config/models.go index bccf333f..4672fea3 100644 --- a/config/models.go +++ b/config/models.go @@ -97,8 +97,7 @@ type DocumentExtractorsConfig struct { } type SummarizerConfig struct { - Enabled bool `mapstructure:"enabled"` - CustomPrompt string `mapstructure:"custom_prompt"` + Enabled bool `mapstructure:"enabled"` } type EmbeddingsConfig struct { From 21f9a36b0dbd9d67e226f474399e0840597601ca Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Tue, 3 Oct 2023 11:05:26 -0600 Subject: [PATCH 12/15] fixes --- config/models.go | 2 +- pkg/extractors/prompts.go | 13 ------------- pkg/extractors/summarizer.go | 21 ++++++++++++++++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/config/models.go b/config/models.go index 4672fea3..89ff6997 100644 --- a/config/models.go +++ b/config/models.go @@ -16,7 +16,7 @@ type Config struct { CustomPrompts CustomPromptsConfig `mapstructure:"custom_prompts"` } -type CustomPromptConfig struct { +type CustomPromptsConfig struct { SummarizerPrompts ExtractorPromptsConfig `mapstructure:"summarizer_prompts"` } diff --git a/pkg/extractors/prompts.go b/pkg/extractors/prompts.go index 45ec6e6c..1a82e5ce 100644 --- a/pkg/extractors/prompts.go +++ b/pkg/extractors/prompts.go @@ -72,19 +72,6 @@ New lines of conversation: New summary: ` -summaryPromptTemplateAnthropic := defaultSummaryPromptTemplateAnthropic -summaryPromptTemplateOpenAI := defaultSummaryPromptTemplateOpenAI - -func init() { - if appState.Config.CustomPrompts.SummarizerPrompt.Anthropic != "" { - SummaryPromptTemplateAnthropic = config.CustomSummaryPromptTemplateAnthropic - } - - if appState.Config.CustomPrompts.SummarizerPrompt.OpenAI != "" { - SummaryPromptTemplateOpenAI = config.CustomSummaryPromptTemplateOpenAI - } -} - type SummaryPromptTemplateData struct { PrevSummary string MessagesJoined string diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index 9812d314..f591d96e 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -250,10 +250,10 @@ func processOverLimitMessages( }, nil } -prevSummaryIdentifier := "{{.PrevSummary}}" -messagesJoinedIdentifier := "{{.MessagesJoined}}" - func validateSummarizerPrompt(prompt string) error { + prevSummaryIdentifier := "{{.PrevSummary}}" + messagesJoinedIdentifier := "{{.MessagesJoined}}" + isCustomPromptValid := strings.Contains(prompt, prevSummaryIdentifier) && strings.Contains(prompt, messagesJoinedIdentifier) @@ -292,12 +292,23 @@ func incrementalSummarizer( MessagesJoined: messagesJoined, } + customSummaryPromptTemplateAnthropic := appState.Config.CustomPrompts.SummarizerPrompts.Anthropic + customSummaryPromptTemplateOpenAI := appState.Config.CustomPrompts.SummarizerPrompts.OpenAI + var summaryPromptTemplate string switch appState.Config.LLM.Service { case "openai": - summaryPromptTemplate = summaryPromptTemplateOpenAI + if customSummaryPromptTemplateOpenAI != "" { + summaryPromptTemplate = customSummaryPromptTemplateOpenAI + } else { + summaryPromptTemplate = defaultSummaryPromptTemplateOpenAI + } case "anthropic": - summaryPromptTemplate = summaryPromptTemplateAnthropic + if customSummaryPromptTemplateAnthropic != "" { + summaryPromptTemplate = customSummaryPromptTemplateAnthropic + } else { + summaryPromptTemplate = defaultSummaryPromptTemplateAnthropic + } default: return "", 0, fmt.Errorf("unknown LLM service: %s", appState.Config.LLM.Service) } From 35cbe216b9e8545456443b5aa54a36b29428d2c1 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 3 Oct 2023 11:18:01 -0600 Subject: [PATCH 13/15] ... --- pkg/extractors/summarizer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index f591d96e..ce408b9c 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -252,7 +252,7 @@ func processOverLimitMessages( func validateSummarizerPrompt(prompt string) error { prevSummaryIdentifier := "{{.PrevSummary}}" - messagesJoinedIdentifier := "{{.MessagesJoined}}" + messagesJoinedIdentifier := "{{.MessagesJoined}}" isCustomPromptValid := strings.Contains(prompt, prevSummaryIdentifier) && strings.Contains(prompt, messagesJoinedIdentifier) @@ -293,7 +293,7 @@ func incrementalSummarizer( } customSummaryPromptTemplateAnthropic := appState.Config.CustomPrompts.SummarizerPrompts.Anthropic - customSummaryPromptTemplateOpenAI := appState.Config.CustomPrompts.SummarizerPrompts.OpenAI + customSummaryPromptTemplateOpenAI := appState.Config.CustomPrompts.SummarizerPrompts.OpenAI var summaryPromptTemplate string switch appState.Config.LLM.Service { From 63790185fa2141f2ccd0dd38579af4bc8763efb1 Mon Sep 17 00:00:00 2001 From: Brice Macias Date: Tue, 3 Oct 2023 11:36:21 -0600 Subject: [PATCH 14/15] ... --- pkg/extractors/summarizer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index ce408b9c..bc9bf383 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -252,7 +252,7 @@ func processOverLimitMessages( func validateSummarizerPrompt(prompt string) error { prevSummaryIdentifier := "{{.PrevSummary}}" - messagesJoinedIdentifier := "{{.MessagesJoined}}" + messagesJoinedIdentifier := "{{.MessagesJoined}}" isCustomPromptValid := strings.Contains(prompt, prevSummaryIdentifier) && strings.Contains(prompt, messagesJoinedIdentifier) @@ -293,7 +293,7 @@ func incrementalSummarizer( } customSummaryPromptTemplateAnthropic := appState.Config.CustomPrompts.SummarizerPrompts.Anthropic - customSummaryPromptTemplateOpenAI := appState.Config.CustomPrompts.SummarizerPrompts.OpenAI + customSummaryPromptTemplateOpenAI := appState.Config.CustomPrompts.SummarizerPrompts.OpenAI var summaryPromptTemplate string switch appState.Config.LLM.Service { From a8cd8ea5f26eb7841f59c1c65fbda627df61b68e Mon Sep 17 00:00:00 2001 From: Daniel Chalef <131175+danielchalef@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:14:36 -0700 Subject: [PATCH 15/15] light restructuring; add tests --- config.yaml | 4 +- config/models.go | 18 +++--- pkg/extractors/summarizer.go | 54 +++++++++------- pkg/extractors/summarizer_test.go | 104 ++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 36 deletions(-) diff --git a/config.yaml b/config.yaml index 00282e94..b75305af 100644 --- a/config.yaml +++ b/config.yaml @@ -76,7 +76,7 @@ custom_prompts: summarizer_prompts: # Anthropic Guidelines: # - Use XML-style tags like as element identifiers. - # - Include {{.PrevSummary}} and {{.MessagesJoined}} as templated variables. + # - Include {{.PrevSummary}} and {{.MessagesJoined}} as template variables. # - Clearly explain model instructions, e.g., "Review content inside tags". # - Provide a clear example within the prompt. # @@ -95,7 +95,7 @@ custom_prompts: # OpenAI summarizer prompt configuration. # Guidelines: - # - Include {{.PrevSummary}} and {{.MessagesJoined}} as templated variables. + # - Include {{.PrevSummary}} and {{.MessagesJoined}} as template variables. # - Provide a clear example within the prompt. # # Example format: diff --git a/config/models.go b/config/models.go index 4a54e39b..dc98d9f1 100644 --- a/config/models.go +++ b/config/models.go @@ -16,15 +16,6 @@ type Config struct { CustomPrompts CustomPromptsConfig `mapstructure:"custom_prompts"` } -type CustomPromptsConfig struct { - SummarizerPrompts ExtractorPromptsConfig `mapstructure:"summarizer_prompts"` -} - -type ExtractorPromptsConfig struct { - OpenAI string `mapstructure:"openai"` - Anthropic string `mapstructure:"anthropic"` -} - type StoreConfig struct { Type string `mapstructure:"type"` Postgres PostgresConfig `mapstructure:"postgres"` @@ -106,6 +97,15 @@ type SummarizerConfig struct { Enabled bool `mapstructure:"enabled"` } +type CustomPromptsConfig struct { + SummarizerPrompts ExtractorPromptsConfig `mapstructure:"summarizer_prompts"` +} + +type ExtractorPromptsConfig struct { + OpenAI string `mapstructure:"openai"` + Anthropic string `mapstructure:"anthropic"` +} + type EmbeddingsConfig struct { Enabled bool `mapstructure:"enabled"` Dimensions int `mapstructure:"dimensions"` diff --git a/pkg/extractors/summarizer.go b/pkg/extractors/summarizer.go index bc9bf383..3666e168 100644 --- a/pkg/extractors/summarizer.go +++ b/pkg/extractors/summarizer.go @@ -259,7 +259,7 @@ func validateSummarizerPrompt(prompt string) error { if !isCustomPromptValid { return fmt.Errorf( - "Wrong summary prompt format. Please make sure it contains the identifiers %s and %s", + "wrong summary prompt format. please make sure it contains the identifiers %s and %s", prevSummaryIdentifier, messagesJoinedIdentifier, ) } @@ -292,6 +292,31 @@ func incrementalSummarizer( MessagesJoined: messagesJoined, } + progressivePrompt, err := generateProgressiveSummarizerPrompt(appState, promptData) + if err != nil { + return "", 0, err + } + + summary, err := appState.LLMClient.Call( + ctx, + progressivePrompt, + llms2.WithMaxTokens(summaryMaxTokens), + ) + if err != nil { + return "", 0, err + } + + summary = strings.TrimSpace(summary) + + tokensUsed, err := appState.LLMClient.GetTokenCount(summary) + if err != nil { + return "", 0, err + } + + return summary, tokensUsed, nil +} + +func generateProgressiveSummarizerPrompt(appState *models.AppState, promptData SummaryPromptTemplateData) (string, error) { customSummaryPromptTemplateAnthropic := appState.Config.CustomPrompts.SummarizerPrompts.Anthropic customSummaryPromptTemplateOpenAI := appState.Config.CustomPrompts.SummarizerPrompts.OpenAI @@ -310,34 +335,13 @@ func incrementalSummarizer( summaryPromptTemplate = defaultSummaryPromptTemplateAnthropic } default: - return "", 0, fmt.Errorf("unknown LLM service: %s", appState.Config.LLM.Service) + return "", fmt.Errorf("unknown LLM service: %s", appState.Config.LLM.Service) } err := validateSummarizerPrompt(summaryPromptTemplate) if err != nil { - return "", 0, err - } - - progressivePrompt, err := internal.ParsePrompt(summaryPromptTemplate, promptData) - if err != nil { - return "", 0, err - } - - summary, err := appState.LLMClient.Call( - ctx, - progressivePrompt, - llms2.WithMaxTokens(summaryMaxTokens), - ) - if err != nil { - return "", 0, err + return "", err } - summary = strings.TrimSpace(summary) - - tokensUsed, err := appState.LLMClient.GetTokenCount(summary) - if err != nil { - return "", 0, err - } - - return summary, tokensUsed, nil + return internal.ParsePrompt(summaryPromptTemplate, promptData) } diff --git a/pkg/extractors/summarizer_test.go b/pkg/extractors/summarizer_test.go index 64ec0835..eb652635 100644 --- a/pkg/extractors/summarizer_test.go +++ b/pkg/extractors/summarizer_test.go @@ -3,6 +3,7 @@ package extractors import ( "testing" + "github.com/getzep/zep/config" "github.com/getzep/zep/pkg/llms" "github.com/getzep/zep/pkg/models" "github.com/getzep/zep/pkg/testutils" @@ -80,3 +81,106 @@ func TestSummarize_Anthropic(t *testing.T) { // Reset the config to the default appState.Config = testutils.NewTestConfig() } + +func TestValidateSummarizerPrompt(t *testing.T) { + testCases := []struct { + name string + prompt string + wantErr bool + }{ + { + name: "valid prompt", + prompt: "{{.PrevSummary}} {{.MessagesJoined}}", + wantErr: false, + }, + { + name: "invalid prompt", + prompt: "{{.PrevSummary}}", + wantErr: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := validateSummarizerPrompt(tc.prompt) + if tc.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestGenerateProgressiveSummarizerPrompt(t *testing.T) { + testCases := []struct { + name string + service string + customPromptOpenAI string + customPromptAnthropic string + expectedPrompt string + defaultPrompt bool + }{ + { + name: "OpenAI with custom prompt", + service: "openai", + customPromptOpenAI: "custom openai prompt {{.PrevSummary}} {{.MessagesJoined}}", + customPromptAnthropic: "", + expectedPrompt: "custom openai prompt previous summary joined messages", + }, + { + name: "Anthropic with custom prompt", + service: "anthropic", + customPromptOpenAI: "", + customPromptAnthropic: "custom anthropic prompt {{.PrevSummary}} {{.MessagesJoined}}", + expectedPrompt: "custom anthropic prompt previous summary joined messages", + }, + { + name: "OpenAI without custom prompt", + service: "openai", + customPromptOpenAI: "", + customPromptAnthropic: "", + expectedPrompt: defaultSummaryPromptTemplateOpenAI, + defaultPrompt: true, + }, + { + name: "Anthropic without custom prompt", + service: "anthropic", + customPromptOpenAI: "", + customPromptAnthropic: "", + expectedPrompt: defaultSummaryPromptTemplateAnthropic, + defaultPrompt: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + appState := &models.AppState{ + Config: &config.Config{ + LLM: config.LLM{ + Service: tc.service, + }, + CustomPrompts: config.CustomPromptsConfig{ + SummarizerPrompts: config.ExtractorPromptsConfig{ + OpenAI: tc.customPromptOpenAI, + Anthropic: tc.customPromptAnthropic, + }, + }, + }, + } + promptData := SummaryPromptTemplateData{ + PrevSummary: "previous summary", + MessagesJoined: "joined messages", + } + + prompt, err := generateProgressiveSummarizerPrompt(appState, promptData) + assert.NoError(t, err) + if !tc.defaultPrompt { + assert.Equal(t, tc.expectedPrompt, prompt) + } else { + // Only compare the first 50 characters of the prompt, since the instructions should match + assert.Equal(t, tc.expectedPrompt[:50], prompt[:50]) + } + }) + } +}