Skip to content

Commit

Permalink
refactor: Get rid of poorly named util package
Browse files Browse the repository at this point in the history
  • Loading branch information
tmc committed Sep 13, 2024
1 parent 0929c6d commit 76cfb63
Show file tree
Hide file tree
Showing 15 changed files with 474 additions and 100 deletions.
8 changes: 8 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ linters:
- style
- test
- unused
enable:
- forbidigo
disable:
- gci # We don't use gci.
- godox # We allow TODO lines.
Expand Down Expand Up @@ -55,6 +57,12 @@ linters-settings:
ignore-file-rules:
- "**/*_test.go"
- "**/mock/**/*.go"
forbidigo:
forbid:
- 'import "[^"]+/(util|common|helpers)"'
gosec:
excludes:
- G115 # https://github.com/securego/gosec/issues/1212
run:
exclude-dirs:
- 'exp'
13 changes: 7 additions & 6 deletions chains/sequential.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"fmt"
"strings"

"github.com/tmc/langchaingo/internal/util"
"github.com/tmc/langchaingo/internal/maputil"
"github.com/tmc/langchaingo/internal/setutil"
"github.com/tmc/langchaingo/memory"
"github.com/tmc/langchaingo/schema"
)
Expand Down Expand Up @@ -42,11 +43,11 @@ func NewSequentialChain(chains []Chain, inputKeys []string, outputKeys []string,
}

func (c *SequentialChain) validateSeqChain() error {
knownKeys := util.ToSet(c.inputKeys)
knownKeys := setutil.ToSet(c.inputKeys)

// Make sure memory keys don't collide with input keys
memoryKeys := c.memory.MemoryVariables(context.Background())
overlappingKeys := util.Intersection(memoryKeys, knownKeys)
overlappingKeys := setutil.Intersection(memoryKeys, knownKeys)
if len(overlappingKeys) > 0 {
return fmt.Errorf(
"%w: input keys [%v] also exist in the memory keys: [%v] - please use input keys and memory keys that don't overlap",
Expand All @@ -61,16 +62,16 @@ func (c *SequentialChain) validateSeqChain() error {

for i, c := range c.chains {
// Check that chain has input keys that are in knownKeys
missingKeys := util.Difference(c.GetInputKeys(), knownKeys)
missingKeys := setutil.Difference(c.GetInputKeys(), knownKeys)
if len(missingKeys) > 0 {
return fmt.Errorf(
"%w: missing required input keys: [%v], only had: [%v]",
ErrChainInitialization, strings.Join(missingKeys, delimiter), strings.Join(util.ListKeys(knownKeys), delimiter),
ErrChainInitialization, strings.Join(missingKeys, delimiter), strings.Join(maputil.ListKeys(knownKeys), delimiter),
)
}

// Check that chain does not have output keys that are already in knownKeys
overlappingKeys := util.Intersection(c.GetOutputKeys(), knownKeys)
overlappingKeys := setutil.Intersection(c.GetOutputKeys(), knownKeys)
if len(overlappingKeys) > 0 {
return fmt.Errorf(
"%w: chain at index %d has output keys that already exist: %v",
Expand Down
4 changes: 2 additions & 2 deletions embeddings/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/tmc/langchaingo/internal/util"
"github.com/tmc/langchaingo/internal/sliceutil"
)

// NewEmbedder creates a new Embedder from the given EmbedderClient, with
Expand Down Expand Up @@ -89,7 +89,7 @@ func BatchTexts(texts []string, batchSize int) [][]string {
batchedTexts := make([][]string, 0, len(texts)/batchSize+1)

for i := 0; i < len(texts); i += batchSize {
batchedTexts = append(batchedTexts, texts[i:util.MinInt([]int{i + batchSize, len(texts)})])
batchedTexts = append(batchedTexts, texts[i:sliceutil.MinInt([]int{i + batchSize, len(texts)})])
}

return batchedTexts
Expand Down
Loading

0 comments on commit 76cfb63

Please sign in to comment.