Skip to content

Commit

Permalink
Clean up sequential_chain_example to make it a bit more readable (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben authored Feb 24, 2024
1 parent 2286493 commit e8498f5
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions examples/sequential-chain-example/sequential_chain_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,68 @@ import (

func main() {
simpleSequentialChainExample()
fmt.Println()
sequentialChainExample()
}

// simpleSequentialChainExample demonstrates a chain with a single input.
func simpleSequentialChainExample() {
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}

template1 := `
You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {{.title}}
Playwright: This is a synopsis for the above play:
`
You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {{.title}}
Playwright: This is a synopsis for the above play:
`
chain1 := chains.NewLLMChain(llm, prompts.NewPromptTemplate(template1, []string{"title"}))

template2 := `
You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis:
{{.synopsis}}
Review from a New York Times play critic of the above play:
`
You are a play critic from the New York Times. Given the synopsis of a play, it is your job to write a review for that play.
Play Synopsis:
{{.synopsis}}
Review from a New York Times play critic of the above play:
`
chain2 := chains.NewLLMChain(llm, prompts.NewPromptTemplate(template2, []string{"synopsis"}))

simpleSeqChain, err := chains.NewSimpleSequentialChain([]chains.Chain{chain1, chain2})
if err != nil {
log.Fatal(err)
}

res, err := chains.Run(context.Background(), simpleSeqChain, "Tragedy at sunset on the beach")
title := "Tragedy at sunset on the beach"
res, err := chains.Run(context.Background(), simpleSeqChain, title)
if err != nil {
log.Fatal(err)
}

fmt.Println(res)
}

// sequentialChainExample demonstrates a chain with multiple inputs.
func sequentialChainExample() {
llm, err := openai.New()
if err != nil {
log.Fatal(err)
}

template1 := `
You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
Title: {{.title}}
Era: {{.era}}
Playwright: This is a synopsis for the above play:
`
You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
Title: {{.title}}
Era: {{.era}}
Playwright: This is a synopsis for the above play:
`
chain1 := chains.NewLLMChain(llm, prompts.NewPromptTemplate(template1, []string{"title", "era"}))
chain1.OutputKey = "synopsis"

template2 := `
You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis:
{{.synopsis}}
Review from a New York Times play critic of the above play:
`
You are a play critic from the New York Times. Given the synopsis of a play, it is your job to write a review for that play.
Play Synopsis:
{{.synopsis}}
Review from a New York Times play critic of the above play:
`
chain2 := chains.NewLLMChain(llm, prompts.NewPromptTemplate(template2, []string{"synopsis"}))
chain2.OutputKey = "review"

Expand All @@ -78,7 +82,11 @@ func sequentialChainExample() {
log.Fatal(err)
}

res, err := chains.Call(context.Background(), sequentialChain, map[string]any{"title": "Tragedy at sunset on the beach", "era": "Victorian"})
inputs := map[string]any{
"title": "Mystery in the haunted mansion",
"era": "1930s in Haiti",
}
res, err := chains.Call(context.Background(), sequentialChain, inputs)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit e8498f5

Please sign in to comment.