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

Clean up sequential_chain_example to make it a bit more readable #635

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading