-
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.
Use agents in langchain example (#21)
- Loading branch information
Nicole White
authored
Oct 27, 2023
1 parent
0843e47
commit ed3fa01
Showing
8 changed files
with
159 additions
and
91 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,49 +1,34 @@ | ||
import { AutoblocksCallbackHandler } from '@autoblocks/client/langchain'; | ||
|
||
import { SimpleSequentialChain, LLMChain } from "langchain/chains"; | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { PromptTemplate } from "langchain/prompts"; | ||
import { DynamicTool } from "langchain/tools"; | ||
import { Calculator } from "langchain/tools/calculator"; | ||
import { initializeAgentExecutorWithOptions } from "langchain/agents"; | ||
|
||
const main = async () => { | ||
// 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, | ||
}); | ||
const model = new OpenAI({ temperature: 0 }); | ||
const tools = [ | ||
new Calculator(), | ||
new DynamicTool({ | ||
name: "Today's Date", | ||
description: | ||
"call this to get today's date. input should be an empty string.", | ||
func: () => new Date().getDate(), | ||
}), | ||
]; | ||
|
||
// This is the overall chain where we run these two chains in sequence. | ||
const overallChain = new SimpleSequentialChain({ | ||
chains: [synopsisChain, reviewChain], | ||
const executor = await initializeAgentExecutorWithOptions(tools, model, { | ||
agentType: 'structured-chat-zero-shot-react-description', | ||
}); | ||
|
||
// Run the chain | ||
const handler = new AutoblocksCallbackHandler(); | ||
const review = await overallChain.run("Tragedy at sunset on the beach", { callbacks: [handler] }); | ||
const output = await executor.run( | ||
"What is today's date? What is that date divided by 2?", | ||
{ callbacks: [handler] | ||
}); | ||
|
||
console.log('Review:'); | ||
console.log(review); | ||
console.log(`Output: ${output}`); | ||
console.log('\n'); | ||
console.log('View your trace: https://app.autoblocks.ai/explore') | ||
} | ||
|
||
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
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,39 +1,44 @@ | ||
import dotenv | ||
from autoblocks.vendor.langchain import AutoblocksCallbackHandler | ||
from langchain.chains import LLMChain | ||
from langchain.chains import SimpleSequentialChain | ||
from langchain.agents import AgentType | ||
from langchain.agents import initialize_agent | ||
from langchain.chains import LLMMathChain | ||
from langchain.llms import OpenAI | ||
from langchain.prompts import PromptTemplate | ||
from langchain.tools import Tool | ||
from langchain.tools import tool | ||
from datetime import datetime | ||
|
||
dotenv.load_dotenv(".env") | ||
|
||
|
||
if __name__ == "__main__": | ||
# 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. | ||
@tool | ||
def todays_date(*args, **kwargs) -> int: | ||
"""Returns today's date""" | ||
return datetime.now().day | ||
|
||
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 | ||
if __name__ == "__main__": | ||
llm = OpenAI(temperature=0) | ||
llm_math_chain = LLMMathChain.from_llm(llm) | ||
tools = [ | ||
Tool.from_function( | ||
func=llm_math_chain.run, | ||
name="Calculator", | ||
description="useful for when you need to answer questions about math", | ||
), | ||
todays_date, | ||
] | ||
agent = initialize_agent( | ||
tools, | ||
llm, | ||
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, | ||
) | ||
handler = AutoblocksCallbackHandler() | ||
review = overall_chain.run("Tragedy at sunset on the beach", callbacks=[handler]) | ||
|
||
print("Review:") | ||
print(review) | ||
output = agent.run( | ||
"What is today's date? What is that date divided by 2?", | ||
callbacks=[handler], | ||
) | ||
|
||
print(f"Output: {output}") | ||
print() | ||
print("View your trace: https://app.autoblocks.ai/explore") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
ed3fa01
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
autoblocks-examples – ./
autoblocks-examples-git-main-autoblocks.vercel.app
autoblocks-examples-autoblocks.vercel.app
chatbot-example.autoblocks.ai