Repository: tmc/langchaingo
Pull request #255 (2023-08-18 13:59:02)

schema/chat-history: Chat history clear method context
### PR Checklist

- [x] Read the [Contributing documentation](https://github.com/tmc/langchaingo/blob/main/CONTRIBUTING.md).
- [x] Read the [Code of conduct documentation](https://github.com/tmc/langchaingo/blob/main/CODE_OF_CONDUCT.md).
- [x] Name your Pull Request title clearly, concisely, and prefixed with the name of the primarily affected package you changed according to [Good commit messages](https://go.dev/doc/contribute#commit_messages) (such as `memory: add interfaces for X, Y` or `util: add whizzbang helpers`).
- [x] Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
- [x] Provide a description in this PR that addresses **what** the PR is solving, or reference the issue that it solves (e.g. `Fixes #123`).
- [x] Describes the source of new concepts.
- [x] References existing implementations as appropriate.
- [x] Contains test coverage for new functions.
- [x] Passes all [`golangci-lint`](https://golangci-lint.run/) checks.

## Description
- when adding context the `Clear` method was missed https://github.com/tmc/langchaingo/pull/249, this PR aligns with everything in the chat history schema


Patch:
diff --git a/llms/openai/openaillm_option.go b/llms/openai/openaillm_option.go
index 0b033c3b..607b5451 100644
--- a/llms/openai/openaillm_option.go
+++ b/llms/openai/openaillm_option.go
@@ -27,12 +27,11 @@ type options struct {
 	baseURL      string
 	organization string
 	apiType      APIType
+	httpClient   openaiclient.Doer
 
 	// required when APIType is APITypeAzure or APITypeAzureAD
 	apiVersion     string
 	embeddingModel string
-
-	httpClient openaiclient.Doer
 }
 
 type Option func(*options)
diff --git a/memory/buffer.go b/memory/buffer.go
index e4873298..af374e40 100644
--- a/memory/buffer.go
+++ b/memory/buffer.go
@@ -38,7 +38,7 @@ func (m *ConversationBuffer) MemoryVariables(context.Context) []string {
 
 // LoadMemoryVariables returns the previous chat messages stored in memory. Previous chat messages
 // are returned in a map with the key specified in the MemoryKey field. This key defaults to
-// "history". If ReturnMessages is set to true the output is a slice of schema.ChatMessage. Otherwise
+// "history". If ReturnMessages is set to true the output is a slice of schema.ChatMessage. Otherwise,
 // the output is a buffer string of the chat messages.
 func (m *ConversationBuffer) LoadMemoryVariables(
 	ctx context.Context, _ map[string]any,
@@ -98,8 +98,8 @@ func (m *ConversationBuffer) SaveContext(
 }
 
 // Clear sets the chat messages to a new and empty chat message history.
-func (m *ConversationBuffer) Clear() error {
-	return m.ChatHistory.Clear()
+func (m *ConversationBuffer) Clear(ctx context.Context) error {
+	return m.ChatHistory.Clear(ctx)
 }
 
 func (m *ConversationBuffer) GetMemoryKey(context.Context) string {
diff --git a/memory/chat.go b/memory/chat.go
index 299ce7ae..f582be26 100644
--- a/memory/chat.go
+++ b/memory/chat.go
@@ -36,7 +36,7 @@ func (h *ChatMessageHistory) AddUserMessage(_ context.Context, text string) erro
 	return nil
 }
 
-func (h *ChatMessageHistory) Clear() error {
+func (h *ChatMessageHistory) Clear(_ context.Context) error {
 	h.messages = make([]schema.ChatMessage, 0)
 	return nil
 }
diff --git a/memory/simple.go b/memory/simple.go
index 2cbca2cb..f4a5a2de 100644
--- a/memory/simple.go
+++ b/memory/simple.go
@@ -29,7 +29,7 @@ func (m Simple) SaveContext(context.Context, map[string]any, map[string]any) err
 	return nil
 }
 
-func (m Simple) Clear() error {
+func (m Simple) Clear(context.Context) error {
 	return nil
 }
 
diff --git a/memory/token_buffer.go b/memory/token_buffer.go
index f8e4b875..56918a46 100644
--- a/memory/token_buffer.go
+++ b/memory/token_buffer.go
@@ -86,8 +86,8 @@ func (tb *ConversationTokenBuffer) SaveContext(
 }
 
 // Clear uses ConversationBuffer method for clearing buffer memory.
-func (tb *ConversationTokenBuffer) Clear() error {
-	return tb.ConversationBuffer.Clear()
+func (tb *ConversationTokenBuffer) Clear(ctx context.Context) error {
+	return tb.ConversationBuffer.Clear(ctx)
 }
 
 func (tb *ConversationTokenBuffer) getNumTokensFromMessages(ctx context.Context) (int, error) {
diff --git a/schema/chat_message_history.go b/schema/chat_message_history.go
index 1a30fb00..a78bacb4 100644
--- a/schema/chat_message_history.go
+++ b/schema/chat_message_history.go
@@ -14,7 +14,7 @@ type ChatMessageHistory interface {
 	AddMessage(ctx context.Context, message ChatMessage) error
 
 	// Clear Remove all messages from the store.
-	Clear() error
+	Clear(ctx context.Context) error
 
 	// Messages get all messages from the store
 	Messages(ctx context.Context) ([]ChatMessage, error)
diff --git a/schema/memory.go b/schema/memory.go
index 8579d2f0..90b03fd9 100644
--- a/schema/memory.go
+++ b/schema/memory.go
@@ -14,5 +14,5 @@ type Memory interface {
 	// SaveContext Save the context of this model run to memory.
 	SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error
 	// Clear memory contents.
-	Clear() error
+	Clear(ctx context.Context) error
 }


Test patch:
diff --git a/memory/buffer_test.go b/memory/buffer_test.go
index 509055bc..2c2b4328 100644
--- a/memory/buffer_test.go
+++ b/memory/buffer_test.go
@@ -89,7 +89,7 @@ func (t testChatMessageHistory) AddMessage(context.Context, schema.ChatMessage)
 	return nil
 }
 
-func (t testChatMessageHistory) Clear() error {
+func (t testChatMessageHistory) Clear(context.Context) error {
 	return nil
 }