Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor tube signatures #50

Merged
merged 1 commit into from May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/embeddings/knowledge_base/db.json

This file was deleted.

6 changes: 2 additions & 4 deletions examples/embeddings/knowledge_base/main.go
Expand Up @@ -28,7 +28,7 @@ func main() {
indexIsEmpty, _ := docsVectorIndex.IsEmpty()

if indexIsEmpty {
err := ingestData(openaiEmbedder)
err := ingestData(docsVectorIndex)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -101,12 +101,10 @@ func main() {

}

func ingestData(openaiEmbedder index.Embedder) error {
func ingestData(docsVectorIndex *index.SimpleVectorIndex) error {

fmt.Printf("Learning Knowledge Base...")

docsVectorIndex := index.NewSimpleVectorIndex("db", ".", openaiEmbedder)

loader := loader.NewPDFToTextLoader("/usr/bin/pdftotext", "./kb")

documents, err := loader.Load()
Expand Down
14 changes: 2 additions & 12 deletions examples/embeddings/pinecone/main.go
Expand Up @@ -55,7 +55,7 @@ func main() {
}

if indexIsEmpty {
err = ingestData(projectID, openaiEmbedder)
err = ingestData(projectID, pineconeIndex)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -119,17 +119,7 @@ func getProjectID(pineconeEnvironment, pineconeApiKey string) (string, error) {
return whoamiResp.ProjectID, nil
}

func ingestData(projectID string, openaiEmbedder index.Embedder) error {

pineconeIndex := index.NewPinecone(
index.PineconeOptions{
IndexName: "test",
ProjectID: projectID,
Namespace: "test-namespace",
IncludeContent: true,
},
openaiEmbedder,
)
func ingestData(projectID string, pineconeIndex *index.Pinecone) error {

documents, err := loader.NewDirectoryLoader(".", ".txt").Load()
if err != nil {
Expand Down
16 changes: 3 additions & 13 deletions examples/pipeline/chat/main.go
Expand Up @@ -18,7 +18,7 @@ func main() {

cache := ram.New()

llmChatOpenAI := openai.NewChat()
llmChatOpenAI := openai.NewChat().WithVerbose(true)
llmOpenAI := openai.NewCompletion()

prompt1, _ := prompt.NewPromptTemplate(
Expand Down Expand Up @@ -49,12 +49,7 @@ func main() {
LlmMode: pipeline.LlmModeChat,
Chat: chat,
}
tube1 := pipeline.NewTube(
"step1",
llm1,
nil,
cache,
)
tube1 := pipeline.NewTube(llm1).WithMemory("step1", cache)

prompt3, _ := prompt.NewPromptTemplate(
"Considering the following joke.\n\njoke:\n{{.output}}\n\n{{.command}}",
Expand All @@ -69,12 +64,7 @@ func main() {
Prompt: prompt3,
}

tube2 := pipeline.NewTube(
"step2",
llm2,
decoder.NewJSONDecoder(),
cache,
)
tube2 := pipeline.NewTube(llm2).WithDecoder(decoder.NewJSONDecoder()).WithMemory("step2", cache)

pipe := pipeline.New(tube1, tube2)

Expand Down
9 changes: 4 additions & 5 deletions examples/pipeline/memory/main.go
Expand Up @@ -22,7 +22,7 @@ func main() {
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt1,
}
tube1 := pipeline.NewTube("step1", llm1, nil, cache)
tube1 := pipeline.NewTube(llm1).WithMemory("step1", cache)

prompt2, _ := prompt.NewPromptTemplate(
"It seems you are a random word generator. Your message '{{.output}}' is nonsense. "+
Expand All @@ -36,9 +36,8 @@ func main() {
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt2,
}
tube2 := pipeline.NewTube("step2", llm2, decoder.NewJSONDecoder(), cache)
tube2 := pipeline.NewTube(llm2).WithDecoder(decoder.NewJSONDecoder()).WithMemory("step2", cache)

regexDecoder := decoder.NewRegExDecoder(`(\w+)\s(\w+)\s(.*)`)
prompt3, _ := prompt.NewPromptTemplate(
"Oh! It seems you are a random JSON word generator. You generated two strings, "+
"first:'{{.step2.output.first}}' and second:'{{.step2.output.second}}'. {{.value}}\n\nHowever your first "+
Expand All @@ -48,12 +47,12 @@ func main() {
},
)
llm1.Prompt = prompt3
tube3 := pipeline.NewTube("step3", llm1, regexDecoder, cache)
tube3 := pipeline.NewTube(llm1).WithDecoder(decoder.NewRegExDecoder(`(\w+)\s(\w+)\s(.*)`)).WithMemory("step3", cache)

prompt4, _ := prompt.NewPromptTemplate("Well here is your answer: "+
"{{ range $value := .step3.output }}[{{$value}}] {{end}}", nil)
llm1.Prompt = prompt4
tube4 := pipeline.NewTube("step4", llm1, nil, cache)
tube4 := pipeline.NewTube(llm1).WithMemory("step4", cache)

pipelineTubes := pipeline.New(
tube1,
Expand Down
25 changes: 5 additions & 20 deletions examples/pipeline/openai/main.go
Expand Up @@ -18,21 +18,16 @@ func main() {

llmOpenAI := openai.NewCompletion()

llmOpenAI.SetCallback(func(response types.Meta) {
llmOpenAI.WithCallback(func(response types.Meta) {
fmt.Printf("USAGE: %#v\n", response)
})
}).WithVerbose(true)

llm := pipeline.Llm{
LlmEngine: llmOpenAI,
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt.New("Hello how are you?"),
}
tube1 := pipeline.NewTube(
"step1",
llm,
nil,
cache,
)
tube1 := pipeline.NewTube(llm).WithMemory("step1", cache)

prompt2, _ := prompt.NewPromptTemplate(
"Consider the following sentence.\n\nSentence:\n{{.output}}\n\n"+
Expand All @@ -42,12 +37,7 @@ func main() {
},
)
llm.Prompt = prompt2
tube2 := pipeline.NewTube(
"step2",
llm,
nil,
nil,
)
tube2 := pipeline.NewTube(llm)

prompt3, _ := prompt.NewPromptTemplate(
"Consider the following sentence.\n\nSentence:\n{{.step1.output}}"+
Expand All @@ -57,12 +47,7 @@ func main() {
},
)
llm.Prompt = prompt3
step3 := pipeline.NewTube(
"step3",
llm,
nil,
cache,
)
step3 := pipeline.NewTube(llm).WithMemory("step3", cache)

pipeLine := pipeline.New(
tube1,
Expand Down
6 changes: 3 additions & 3 deletions examples/pipeline/simple/main.go
Expand Up @@ -17,7 +17,7 @@ func main() {
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt.New("Hello how are you?"),
}
tube1 := pipeline.NewTube("step1", llm1, nil, nil)
tube1 := pipeline.NewTube(llm1)

prompt2, _ := prompt.NewPromptTemplate(
"It seems you are a random word generator. Your message '{{.output}}' is nonsense. Anyway I'm fine {{.value}}!",
Expand All @@ -30,7 +30,7 @@ func main() {
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt2,
}
tube2 := pipeline.NewTube("step2", llm2, decoder.NewJSONDecoder(), nil)
tube2 := pipeline.NewTube(llm2).WithDecoder(decoder.NewJSONDecoder())

prompt3, _ := prompt.NewPromptTemplate(
"Oh! It seems you are a random JSON word generator. You generated two strings, first:'{{.First}}' and second:'{{.Second}}'. {{.value}}",
Expand All @@ -39,7 +39,7 @@ func main() {
},
)
llm1.Prompt = prompt3
tube3 := pipeline.NewTube("step3", llm1, decoder.NewRegExDecoder(`(\w+?)\s(\w+?)\s(.*)`), nil)
tube3 := pipeline.NewTube(llm1).WithDecoder(decoder.NewRegExDecoder(`(\w+?)\s(\w+?)\s(.*)`))

pipelineTubes := pipeline.New(
tube1,
Expand Down
16 changes: 3 additions & 13 deletions examples/pipeline/splitter/main.go
Expand Up @@ -13,19 +13,14 @@ import (

func main() {

llmOpenAI := openai.NewCompletion()
llmOpenAI := openai.NewCompletion().WithVerbose(true)

llm := pipeline.Llm{
LlmEngine: llmOpenAI,
LlmMode: pipeline.LlmModeCompletion,
Prompt: prompt.New("Hello how are you?"),
}
tube1 := pipeline.NewTube(
"step1",
llm,
nil,
nil,
)
tube1 := pipeline.NewTube(llm)

prompt2, _ := prompt.NewPromptTemplate(
"Consider the following sentence.\n\nSentence:\n{{.output}}\n\n"+
Expand Down Expand Up @@ -65,12 +60,7 @@ func main() {
nil,
)
llm.Prompt = prompt3
tube3 := pipeline.NewTube(
"step3",
llm,
nil,
nil,
)
tube3 := pipeline.NewTube(llm)

pipeLine := pipeline.New(
tube1,
Expand Down
20 changes: 10 additions & 10 deletions index/pinecone.go
Expand Up @@ -19,7 +19,7 @@ const (
defaultBatchUpsertSize = 32
)

type pinecone struct {
type Pinecone struct {
pineconeClient *pineconego.PineconeGo
indexName string
projectID string
Expand All @@ -37,7 +37,7 @@ type PineconeOptions struct {
BatchUpsertSize *int
}

func NewPinecone(options PineconeOptions, embedder Embedder) *pinecone {
func NewPinecone(options PineconeOptions, embedder Embedder) *Pinecone {

apiKey := os.Getenv("PINECONE_API_KEY")
environment := os.Getenv("PINECONE_ENVIRONMENT")
Expand All @@ -49,7 +49,7 @@ func NewPinecone(options PineconeOptions, embedder Embedder) *pinecone {
batchUpsertSize = *options.BatchUpsertSize
}

return &pinecone{
return &Pinecone{
pineconeClient: pineconeClient,
indexName: options.IndexName,
projectID: options.ProjectID,
Expand All @@ -60,20 +60,20 @@ func NewPinecone(options PineconeOptions, embedder Embedder) *pinecone {
}
}

func (p *pinecone) WithAPIKeyAndEnvironment(apiKey, environment string) *pinecone {
func (p *Pinecone) WithAPIKeyAndEnvironment(apiKey, environment string) *Pinecone {
p.pineconeClient = pineconego.New(environment, apiKey)
return p
}

func (s *pinecone) LoadFromDocuments(ctx context.Context, documents []document.Document) error {
func (s *Pinecone) LoadFromDocuments(ctx context.Context, documents []document.Document) error {
err := s.batchUpsert(ctx, documents)
if err != nil {
return fmt.Errorf("%s: %w", ErrInternal, err)
}
return nil
}

func (p *pinecone) IsEmpty(ctx context.Context) (bool, error) {
func (p *Pinecone) IsEmpty(ctx context.Context) (bool, error) {

req := &pineconerequest.VectorDescribeIndexStats{
IndexName: p.indexName,
Expand All @@ -99,7 +99,7 @@ func (p *pinecone) IsEmpty(ctx context.Context) (bool, error) {

}

func (p *pinecone) SimilaritySearch(ctx context.Context, query string, topK *int) ([]SearchResponse, error) {
func (p *Pinecone) SimilaritySearch(ctx context.Context, query string, topK *int) ([]SearchResponse, error) {

matches, err := p.similaritySearch(ctx, topK, query)
if err != nil {
Expand All @@ -111,7 +111,7 @@ func (p *pinecone) SimilaritySearch(ctx context.Context, query string, topK *int
return filterSearchResponses(searchResponses, topK), nil
}

func (p *pinecone) similaritySearch(ctx context.Context, topK *int, query string) ([]pineconeresponse.QueryMatch, error) {
func (p *Pinecone) similaritySearch(ctx context.Context, topK *int, query string) ([]pineconeresponse.QueryMatch, error) {
pineconeTopK := defaultPineconeTopK
if topK != nil {
pineconeTopK = *topK
Expand Down Expand Up @@ -143,7 +143,7 @@ func (p *pinecone) similaritySearch(ctx context.Context, topK *int, query string
return res.Matches, nil
}

func (p *pinecone) batchUpsert(ctx context.Context, documents []document.Document) error {
func (p *Pinecone) batchUpsert(ctx context.Context, documents []document.Document) error {

for i := 0; i < len(documents); i += defaultBatchUpsertSize {

Expand Down Expand Up @@ -176,7 +176,7 @@ func (p *pinecone) batchUpsert(ctx context.Context, documents []document.Documen
return nil
}

func (p *pinecone) vectorUpsert(ctx context.Context, vectors []pineconerequest.Vector) error {
func (p *Pinecone) vectorUpsert(ctx context.Context, vectors []pineconerequest.Vector) error {

req := &pineconerequest.VectorUpsert{
IndexName: p.indexName,
Expand Down