Skip to content

Commit

Permalink
Use agents in langchain example (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole White authored Oct 27, 2023
1 parent 0843e47 commit ed3fa01
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 91 deletions.
12 changes: 10 additions & 2 deletions JavaScript/langchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ npm install
npm run start
```

## View logs in Autoblocks
## View the trace in Autoblocks

After you run the script, you can find the trace on the [explore page](https://app.autoblocks.ai/explore).
Go to the [explore page](https://app.autoblocks.ai/explore). When you find the trace, switch to the Trace Tree view to
see a tree of all the spans in the trace.

The first span that is selected will show you the overall question and answer of the LangChain pipeline, but you can also
drill into individual spans by clicking on them to understand how LangChain is working under the hood.

![explore-trace-top-level-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/590e232a-eeaf-46a1-b9e3-3c0a8648234b)

![explore-trace-nested-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/b32d7776-8378-49cc-a866-7ba6bd08f5e5)
50 changes: 35 additions & 15 deletions JavaScript/langchain/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion JavaScript/langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"dependencies": {
"@autoblocks/client": "^0.0.15",
"dotenv-cli": "^7.3.0",
"langchain": "^0.0.166"
"langchain": "^0.0.173"
}
}
59 changes: 22 additions & 37 deletions JavaScript/langchain/src/index.js
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();
12 changes: 10 additions & 2 deletions Python/langchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ poetry install
poetry run python main.py
```

## View logs in Autoblocks
## View the trace in Autoblocks

Go to the [explore page](https://app.autoblocks.ai/explore) to see the trace.
Go to the [explore page](https://app.autoblocks.ai/explore). When you find the trace, switch to the Trace Tree view to
see a tree of all the spans in the trace.

The first span that is selected will show you the overall question and answer of the LangChain pipeline, but you can also
drill into individual spans by clicking on them to understand how LangChain is working under the hood.

![explore-trace-top-level-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/941c09b7-86e9-4e0b-9df4-2a9be0b32771)

![explore-teace-nested-span](https://github.com/autoblocksai/autoblocks-examples/assets/7498009/99f02ba9-c3ea-4645-aa9d-17f6d83be790)
63 changes: 34 additions & 29 deletions Python/langchain/main.py
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")
49 changes: 45 additions & 4 deletions Python/langchain/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Python/langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ readme = "README.md"
python = "^3.9"
autoblocksai = "^0.0.11"
python-dotenv = "^1.0.0"
langchain = "^0.0.319"
openai = "^0.28.1"
numexpr = "^2.8.7"
langchain = "^0.0.323"

1 comment on commit ed3fa01

@vercel
Copy link

@vercel vercel bot commented on ed3fa01 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.