-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicole White
committed
Oct 23, 2023
1 parent
9b2ff36
commit cc99d95
Showing
2 changed files
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
import { AutoblocksCallbackHandler } from '@autoblocks/client/langchain'; | ||
|
||
import { LLMChain } from 'langchain/chains'; | ||
import { OpenAI } from 'langchain/llms/openai'; | ||
import { PromptTemplate } from 'langchain/prompts'; | ||
import { SimpleSequentialChain, LLMChain } from "langchain/chains"; | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { PromptTemplate } from "langchain/prompts"; | ||
|
||
const main = async () => { | ||
const llm = new OpenAI({ temperature: 0 }); | ||
const prompt = PromptTemplate.fromTemplate('2 + {number} ='); | ||
const chain = new LLMChain({ prompt, llm }); | ||
// This is an LLMChain to write a synopsis given a title of a play. | ||
const synopsisLLM = new OpenAI({ temperature: 0.7 }); | ||
const titleTemplate = `You are a playwright. Given the title of a play, it is your job to write a synopsis for that title. | ||
Title: {title} | ||
Playwright: This is a synopsis for the above play:`; | ||
const synopsisTemplate = new PromptTemplate({ | ||
template: titleTemplate, | ||
inputVariables: ["title"], | ||
}); | ||
const synopsisChain = new LLMChain({ llm: synopsisLLM, prompt: synopsisTemplate }); | ||
|
||
// This is an LLMChain to write a review of a play given a synopsis. | ||
const reviewLLM = new OpenAI({ temperature: 0.7 }); | ||
const reviewTemplate = `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:`; | ||
const reviewPromptTemplate = new PromptTemplate({ | ||
template: reviewTemplate, | ||
inputVariables: ["synopsis"], | ||
}); | ||
const reviewChain = new LLMChain({ | ||
llm: reviewLLM, | ||
prompt: reviewPromptTemplate, | ||
}); | ||
|
||
// This is the overall chain where we run these two chains in sequence. | ||
const overallChain = new SimpleSequentialChain({ | ||
chains: [synopsisChain, reviewChain], | ||
}); | ||
|
||
// Run the chain | ||
const handler = new AutoblocksCallbackHandler(); | ||
await chain.call({ number: 2 }, { callbacks: [handler] }); | ||
const review = await overallChain.run("Tragedy at sunset on the beach", { callbacks: [handler] }); | ||
|
||
console.log('Review:'); | ||
console.log(review); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,39 @@ | ||
import dotenv | ||
from autoblocks.vendor.langchain import AutoblocksCallbackHandler | ||
from langchain.chains import LLMChain | ||
from langchain.chains import SimpleSequentialChain | ||
from langchain.llms import OpenAI | ||
from langchain.prompts import PromptTemplate | ||
|
||
dotenv.load_dotenv("../../.env") | ||
|
||
|
||
if __name__ == "__main__": | ||
llm = OpenAI() | ||
# This is an LLMChain to write a synopsis given a title of a play. | ||
synopsis_llm = OpenAI(temperature=0.7) | ||
synopsis_template = """You are a playwright. Given the title of a play, it is your job to write a synopsis for that title. | ||
Title: {title} | ||
Playwright: This is a synopsis for the above play:""" | ||
synopsis_prompt_template = PromptTemplate(input_variables=["title"], template=synopsis_template) | ||
synopsis_chain = LLMChain(llm=synopsis_llm, prompt=synopsis_prompt_template) | ||
|
||
# This is an LLMChain to write a review of a play given a synopsis. | ||
review_llm = OpenAI(temperature=0.7) | ||
review_template = """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:""" | ||
review_prompt_template = PromptTemplate(input_variables=["synopsis"], template=review_template) | ||
review_chain = LLMChain(llm=review_llm, prompt=review_prompt_template) | ||
|
||
# This is the overall chain where we run these two chains in sequence. | ||
overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain]) | ||
|
||
# Run the chain | ||
handler = AutoblocksCallbackHandler() | ||
llm.predict("hello, world!", callbacks=[handler]) | ||
review = overall_chain.run("Tragedy at sunset on the beach", callbacks=[handler]) | ||
|
||
print("Review:") | ||
print(review) |