Repository: tmc/langchaingo
Pull request #33 (2023-04-20 02:16:32)

exp/prompts: move exp/prompts closer to acceptable state
Addresses (partially) #31 

This massages our exp/prompts package towards being mergable.


Patch:
diff --git a/Makefile b/Makefile
index 9f698c65..869c1ba3 100644
--- a/Makefile
+++ b/Makefile
@@ -18,3 +18,6 @@ test-race:
 test-cover:
 	go run test -cover ./...
 
+.PHONY: run-pkgsite
+run-pkgsite:
+	go run golang.org/x/pkgsite/cmd/pkgsite@latest
diff --git a/exp/doc.go b/exp/doc.go
new file mode 100644
index 00000000..f06dd638
--- /dev/null
+++ b/exp/doc.go
@@ -0,0 +1,3 @@
+// Package exp constains experiemntal code that is subject to change or removal.
+// There are no compatibility guarantees for this package and its subpackages.
+package exp
diff --git a/exp/prompts/chatTemplate.go b/exp/prompts/chat_template.go
similarity index 97%
rename from exp/prompts/chatTemplate.go
rename to exp/prompts/chat_template.go
index 00e6df36..595765e2 100644
--- a/exp/prompts/chatTemplate.go
+++ b/exp/prompts/chat_template.go
@@ -4,7 +4,7 @@ import (
 	"fmt"
 	"strings"
 
-	"github.com/tmc/langchaingo/exp/schema"
+	"github.com/tmc/langchaingo/schema"
 )
 
 /*
@@ -20,7 +20,7 @@ type Message struct {
 func (m Message) format(values map[string]any) (schema.ChatMessage, error) {
 	formatted, err := m.prompt.Format(values)
 	if err != nil {
-		return schema.AiChatMessage{}, err
+		return schema.AIChatMessage{}, err
 	}
 
 	return m.toChatMessage(formatted), nil
@@ -45,7 +45,7 @@ func NewHumanMessage(systemPrompt Template) Message {
 func NewAiMessage(systemPrompt Template) Message {
 	return Message{
 		prompt:         systemPrompt,
-		toChatMessage:  func(message string) schema.ChatMessage { return schema.AiChatMessage{Text: message} },
+		toChatMessage:  func(message string) schema.ChatMessage { return schema.AIChatMessage{Text: message} },
 		inputVariables: systemPrompt.GetInputVariables(),
 	}
 }
diff --git a/exp/prompts/doc.go b/exp/prompts/doc.go
new file mode 100644
index 00000000..81c39cb1
--- /dev/null
+++ b/exp/prompts/doc.go
@@ -0,0 +1,3 @@
+// Package prompts contains utilities to manage prompt templates, output parsers, example
+// selectors, and other utilities for working with LLM prompts.
+package prompts
diff --git a/exp/prompts/promptSelector.go b/exp/prompts/prompt_selector.go
similarity index 100%
rename from exp/prompts/promptSelector.go
rename to exp/prompts/prompt_selector.go
diff --git a/exp/prompts/promptTemplate.go b/exp/prompts/prompt_template.go
similarity index 100%
rename from exp/prompts/promptTemplate.go
rename to exp/prompts/prompt_template.go
diff --git a/exp/prompts/prompts.go b/exp/prompts/prompts.go
index 53f36990..354d7bf4 100644
--- a/exp/prompts/prompts.go
+++ b/exp/prompts/prompts.go
@@ -1,7 +1,7 @@
 package prompts
 
 import (
-	"github.com/tmc/langchaingo/exp/schema"
+	"github.com/tmc/langchaingo/schema"
 )
 
 type Template interface {
diff --git a/go.mod b/go.mod
index 8e600490..1b9c09d9 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module github.com/tmc/langchaingo
 
 go 1.20
 
-require github.com/google/uuid v1.3.0
+require (
+	github.com/google/go-cmp v0.5.9
+	github.com/google/uuid v1.3.0
+)
diff --git a/go.sum b/go.sum
index 3dfe1c9f..c1f0f156 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,4 @@
+github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
+github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
 github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=


Test patch:
diff --git a/exp/prompts/chat_test.go b/exp/prompts/chat_test.go
index 041bf3a8..ca397751 100644
--- a/exp/prompts/chat_test.go
+++ b/exp/prompts/chat_test.go
@@ -1,26 +1,26 @@
 package prompts
 
 import (
-	"reflect"
 	"testing"
 
-	"github.com/tmc/langchaingo/exp/schema"
+	"github.com/google/go-cmp/cmp"
+	"github.com/tmc/langchaingo/schema"
 )
 
 func TestChatTemplate(t *testing.T) {
 	systemPrompt, err := NewPromptTemplate("Here's some context: {context}", []string{"context"})
 	if err != nil {
-		t.Errorf("Unexpected error: %s", err.Error())
+		t.Fatal(err)
 	}
 
 	userPrompt, err := NewPromptTemplate("Hello AI. Give me a long response. {question}", []string{"question"})
 	if err != nil {
-		t.Errorf("Unexpected error: %s", err.Error())
+		t.Fatal(err)
 	}
 
 	aiPrompt, err := NewPromptTemplate("Very good question. My answer to {question} is {answer}", []string{"answer", "question"})
 	if err != nil {
-		t.Errorf("Unexpected error: %s", err.Error())
+		t.Fatal(err)
 	}
 
 	messages := []Message{
@@ -41,19 +41,24 @@ func TestChatTemplate(t *testing.T) {
 
 	chatTemplate, err := NewChatTemplate(messages, []string{"answer", "context", "question"})
 	if err != nil {
-		t.Errorf("Unexpected error: %s", err.Error())
+		t.Fatal(err)
 	}
 
 	chatMessages, err := chatTemplate.FormatPromptValue(map[string]any{"context": "foo", "question": "bar", "answer": "foobar"})
+	if err != nil {
+		t.Fatal(err)
+	}
 	expectedChatMessages := []schema.ChatMessage{
 		schema.SystemChatMessage{Text: "Here's some context: foo"},
 		schema.HumanChatMessage{Text: "Hello AI. Give me a long response. bar"},
-		schema.AiChatMessage{Text: "Very good question. My answer to bar is foobar"},
+		schema.AIChatMessage{Text: "Very good question. My answer to bar is foobar"},
 	}
 	expectedString := `[{"text":"Here's some context: foo"},{"text":"Hello AI. Give me a long response. bar"},{"text":"Very good question. My answer to bar is foobar"}]`
 
-	if !reflect.DeepEqual(chatMessages.ToChatMessages(), expectedChatMessages) {
-		t.Errorf("Chat template format prompt value chat messages not equal to expected. Got: %v. Expect: %v", chatMessages.ToChatMessages(), expectedChatMessages)
+	// use cmp to compare:
+	if !cmp.Equal(chatMessages.ToChatMessages(), expectedChatMessages) {
+		t.Errorf("Chat template format prompt value chat messages not equal to expected. Diff: %s", cmp.Diff(chatMessages.ToChatMessages(), expectedChatMessages))
+
 	}
 
 	if !(chatMessages.String() == expectedString) {