Skip to content

Commit

Permalink
lint: Add separate more relaxed linting for exp tree
Browse files Browse the repository at this point in the history
This is just local config for now.
  • Loading branch information
tmc committed Apr 22, 2023
1 parent ce4891f commit aa409b3
Show file tree
Hide file tree
Showing 25 changed files with 51 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .golangci-exp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
linters:
presets:
- bugs
- comment
7 changes: 0 additions & 7 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,3 @@ linters:
- varnamelen
- nlreturn
- wrapcheck # TODO: we should probably enable this one (at least for new code).
exclude-rules:
- path: ^exp/.*.go
linters:
- errorlint
# issues:
# new: true
# new-from-rev: HEAD
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ test:
lint:
golangci-lint run --color=always --sort-results --skip-dirs=exp ./...

.PHONY: lint-exp
lint-exp:
golangci-lint run --fix --config .golangci-exp.yaml ./...

.PHONY: lint-fix
lint-fix:
golangci-lint run --fix --skip-dirs=exp ./...
Expand Down
2 changes: 0 additions & 2 deletions exp/agent/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ func New(
tools []tools.Tool,
agentType AgentType,
opts ...Options,

) (executor.AgentExecutor, error) {

options := defaultOptions()
for _, opt := range opts {
opt(&options)
Expand Down
3 changes: 1 addition & 2 deletions exp/agent/mrkl/mrkl.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func NewOneShotAgent(llm llms.LLM, tools []tools.Tool, opts map[string]any) (*On
verbose: opts["verbose"].(bool),
maxRetries: opts["maxRetries"].(int),
}, nil

}

// Run is an implementation of the AgentExecutor interface. It takes a query as input
Expand Down Expand Up @@ -113,7 +112,7 @@ func (a *OneShotZeroAgent) nextStep(action schema.AgentAction) (string, error) {

// Use the updated resp in the next iteration
return newResp["text"].(string), nil

}
}
func (a *OneShotZeroAgent) plan(info string) (*schema.AgentAction, *schema.AgentFinish) {
action := getAgentAction(info)
Expand Down
6 changes: 4 additions & 2 deletions exp/agent/mrkl/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/tmc/langchaingo/exp/tools"
)

const Prefix = `Answer the following questions as best you can. You have access to the following Tools:`
const FormatInstructions = `Use the following format:
const (
Prefix = `Answer the following questions as best you can. You have access to the following Tools:`
FormatInstructions = `Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Expand All @@ -19,6 +20,7 @@ Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question`
)
const Suffix = `Begin!
Question: {input}
Expand Down
2 changes: 1 addition & 1 deletion exp/chains/stuff_documents_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var DefaultQAPrompt, _ = prompts.NewPromptTemplate(
[]string{"context", "question"},
)

// TODO: add conditional after chat model is added
// TODO: add conditional after chat model is added.
var QAPromptSelector = prompts.NewConditionalPromptSelector(DefaultQAPrompt, []prompts.Conditional{})

func loadQAStuffChain(llm llms.LLM) StuffDocumentsChain {
Expand Down
2 changes: 1 addition & 1 deletion exp/doc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Package exp constains experiemntal code that is subject to change or removal.
// Package exp constains experimental code that is subject to change or removal.
// There are no compatibility guarantees for this package and its subpackages.
package exp
2 changes: 1 addition & 1 deletion exp/document_loaders/document_loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/tmc/langchaingo/schema"
)

// Document loader is the interface for loading and splitting documents from a source
// Document loader is the interface for loading and splitting documents from a source.
type DocumentLoader interface {
// Loads from source and returns documents
Load() ([]schema.Document, error)
Expand Down
2 changes: 1 addition & 1 deletion exp/document_loaders/text_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type TextLoader struct {
filePath string
//Todo: blob equivalent
// Todo: blob equivalent
}

func NewTextLoaderFromFile(filePath string) TextLoader {
Expand Down
2 changes: 1 addition & 1 deletion exp/embeddings/embeddings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package embeddings

// Embeddings is the interface for creating vector embeddings from texts
// Embeddings is the interface for creating vector embeddings from texts.
type Embeddings interface {
// Returns vector for each text
EmbedDocuments(texts []string) ([][]float64, error)
Expand Down
3 changes: 2 additions & 1 deletion exp/memory/chat_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func (h *ChatMessageHistory) GetMessages() []schema.ChatMessage { return h.messa
func (h *ChatMessageHistory) AddAiMessage(text string) {
h.messages = append(h.messages, schema.AIChatMessage{Text: text})
}

func (h *ChatMessageHistory) AddUserMessage(text string) {
h.messages = append(h.messages, schema.HumanChatMessage{Text: text})
}
Expand All @@ -28,7 +29,7 @@ func NewChatMessageHistory(options ...NewChatMessageOption) *ChatMessageHistory

type NewChatMessageOption func(m *ChatMessageHistory)

// Option for NewChatMessageHistory adding previous messages to the history
// Option for NewChatMessageHistory adding previous messages to the history.
func WithPreviousMessages(previousMessages []schema.ChatMessage) NewChatMessageOption {
return func(m *ChatMessageHistory) {
m.messages = append(m.messages, previousMessages...)
Expand Down
5 changes: 3 additions & 2 deletions exp/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,24 @@ func getBufferString(messages []schema.ChatMessage, humanPrefix, aiPrefix string
role = humanPrefix
default:
role = string(message.GetType())

}

stringMessages = append(stringMessages, fmt.Sprintf("%s: %s", role, messages[i].GetText()))
}

return strings.Join(stringMessages[:], "\n")
return strings.Join(stringMessages, "\n")
}

type EmptyMemory struct{}

func (m EmptyMemory) MemoryVariables() []string {
return []string{}
}

func (m EmptyMemory) LoadMemoryVariables(map[string]any) map[string]any {
return map[string]any{}
}

func (m EmptyMemory) SaveContext(inputs map[string]any, outputs map[string]any) error {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion exp/output_parsers/empty_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package output_parsers

import "github.com/tmc/langchaingo/exp/prompts"

// Output parser that does nothing
// Output parser that does nothing.
type EmptyOutputParser struct{}

func (p EmptyOutputParser) GetFormatInstructions() string { return "" }
Expand Down
5 changes: 2 additions & 3 deletions exp/prompts/chat_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ type ChatPromptValue struct {
messages []schema.ChatMessage
}

// Formats the ChatPromptValue as a JSON string
// Formats the ChatPromptValue as a JSON string.
func (v ChatPromptValue) String() string {

keyValueJSON := make([]string, 0)
for i := 0; i < len(v.messages); i++ {
keyValueJSON = append(keyValueJSON, fmt.Sprintf("{\"text\":\"%s\"}", v.messages[i].GetText()))
}

return fmt.Sprintf("[%s]", strings.Join(keyValueJSON, ",")) //Joins the string with [] around creating an array of objects

This comment has been minimized.

Copy link
@nekomeowww

nekomeowww Apr 22, 2023

Contributor

This change breaks the entire codebase.


}

func (v ChatPromptValue) ToChatMessages() []schema.ChatMessage {
Expand Down
1 change: 0 additions & 1 deletion exp/prompts/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestChatTemplate(t *testing.T) {
// 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) {
Expand Down
10 changes: 5 additions & 5 deletions exp/prompts/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ func renderTemplate(template string, templateFormat string, inputValues map[stri
return formatter(template, inputValues)
}

type parsedFStringNode interface{}
type fStringLiteral struct{ text string }
type fStringVariable struct{ name string }
type (
parsedFStringNode interface{}
fStringLiteral struct{ text string }
fStringVariable struct{ name string }
)

func paresFString(_template string) ([]parsedFStringNode, error) {
template := []rune(_template)
nodes := make([]parsedFStringNode, 0)

for i := 0; i < len(template); {

if template[i] == '{' && i+1 < len(template) && template[i+1] == '{' {
nodes = append(nodes, fStringLiteral{text: "{"})
i += 2
Expand Down Expand Up @@ -115,7 +116,6 @@ func interpolateFString(template string, values map[string]any) (string, error)
output += fmt.Sprintf("%v", variableValue)
case fStringLiteral:
output += n.text

}
}

Expand Down
8 changes: 3 additions & 5 deletions exp/text_splitters/recursive_character.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ func NewRecursiveCharactersSplitter() RecursiveCharactersSplitter {
}

func (s RecursiveCharactersSplitter) SplitText(text string) ([]string, error) {

//Find the appropriate separator
// Find the appropriate separator
separator := s.Separators[len(s.Separators)-1]
for _, s := range s.Separators {
if s == "" {
Expand All @@ -34,15 +33,14 @@ func (s RecursiveCharactersSplitter) SplitText(text string) ([]string, error) {
}
}

//Split the text

splits := strings.Split(text, separator)

//Merge

finalChunks := make([]string, 0)
goodSplits := make([]string, 0)

for _, split := range splits {

if len(split) < s.ChunkSize {
goodSplits = append(goodSplits, split)
continue
Expand Down
3 changes: 0 additions & 3 deletions exp/text_splitters/recursive_character_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type test struct {

var tests = []test{
{

text: "Hi.\nI'm Harrison.\n\nHow?\na\nb",
chunkOverlap: 1,
chunkSize: 20,
Expand All @@ -42,7 +41,6 @@ var tests = []test{
},
},
{

text: "Hi.\nI'm Harrison.\n\nHow?\na\nbHi.\nI'm Harrison.\n\nHow?\na\nb",
chunkOverlap: 1,
chunkSize: 40,
Expand Down Expand Up @@ -85,5 +83,4 @@ func TestRecursiveCharacterSplitter(t *testing.T) {
t.Logf("Result creating documents with recursive character splitter not equal expected. \n Got:\n %v \nWant:\n %v \n", docs, test.expectedDocs)
}
}

}
6 changes: 2 additions & 4 deletions exp/text_splitters/text_spliters.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type LOCMetadata struct {
Lines LineData
}

// Creates documents with a text splitter
// Creates documents with a text splitter.
func CreateDocuments(textSplitter TextSplitter, texts []string, metadatas []map[string]any) ([]schema.Document, error) {
if len(metadatas) == 0 {
metadatas = make([]map[string]any, len(texts))
Expand All @@ -56,12 +56,11 @@ func CreateDocuments(textSplitter TextSplitter, texts []string, metadatas []map[
}

for _, chunk := range chunks {

// Code for adding line number to the metadata of the chunk.
// Does not work because some chunks generated are very small and can be written multiple places in the file
// Makes it impossible to find the correct index

//Find complete number of newlines between last chunk and current chunk
// Find complete number of newlines between last chunk and current chunk
/* numberOfIntermediateNewLines := 0
if !first {
prevIndexChunk := strings.Index(text, prevChunk)
Expand Down Expand Up @@ -142,7 +141,6 @@ func MergeSplits(splits []string, separator string, chunkSize int, chunkOverlap

doc := joinDocs(currentDoc, separator)
if doc != "" {

docs = append(docs, doc)
}

Expand Down
6 changes: 4 additions & 2 deletions exp/tools/serpapi/internal/serpapi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"strings"
)

var ErrMissingToken = errors.New("missing the OpenAI API key, set it in the SERPAPI_API_KEY environment variable")
var NoGoodResult = "No good search result found"
var (
ErrMissingToken = errors.New("missing the OpenAI API key, set it in the SERPAPI_API_KEY environment variable")
NoGoodResult = "No good search result found"
)

type SerpapiClient struct {
apiKey string
Expand Down
3 changes: 1 addition & 2 deletions exp/tools/serpapi/serpapi_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/tmc/langchaingo/exp/tools/serpapi/internal"
)

//Create a new tool for serpapi to search on internet
// Create a new tool for serpapi to search on internet.
func New() (*tools.Tool, error) {
client, err := internal.New()
if err != nil {
Expand All @@ -30,5 +30,4 @@ func New() (*tools.Tool, error) {
return strings.Join(strings.Fields(result), " "), nil
},
}, nil

}
2 changes: 1 addition & 1 deletion exp/tools/tool.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package tools defines the types for tools to be used by the llms agents.
package tools

// Tool is a tool for the llm agent to interact with diferent application.
// Tool is a tool for the llm agent to interact with different application.
type Tool struct {
Name string
Description string
Expand Down
Loading

0 comments on commit aa409b3

Please sign in to comment.