Skip to content

Commit

Permalink
Merge pull request langchain-ai#766 from jacobrosenthal/jacob/serpapi…
Browse files Browse the repository at this point in the history
…-location

serpapi results coming back in another region/languge
  • Loading branch information
nfcampos authored Apr 13, 2023
2 parents 2886ad9 + 7ebeaad commit 4f67ed3
Show file tree
Hide file tree
Showing 20 changed files with 186 additions and 24 deletions.
8 changes: 7 additions & 1 deletion docs/docs/getting-started/guide-chat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@ And finally, we can use the AgentExecutor to run an agent:

```typescript
// Define the list of tools the agent can use
const tools = [new SerpAPI()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
];
// Create the agent from the chat model and the tools
const agent = ChatAgent.fromLLMAndTools(new ChatOpenAI(), tools);
// Create an executor, which calls to the agent until an answer is found
Expand Down
9 changes: 8 additions & 1 deletion docs/docs/getting-started/guide-llm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ import { SerpAPI } from "langchain/tools";
import { Calculator } from "langchain/tools/calculator";
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const executor = await initializeAgentExecutor(
tools,
Expand Down
9 changes: 8 additions & 1 deletion docs/docs/modules/agents/executor/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ import { SerpAPI } from "langchain/tools";
import { Calculator } from "langchain/tools/calculator";

const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
10 changes: 9 additions & 1 deletion docs/docs/modules/agents/tools/agents_with_vectorstores.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ const qaTool = new ChainTool({
Now you can construct and using the tool just as you would any other!

```typescript
const tools = [new SerpAPI(), new Calculator(), qaTool];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
qaTool,
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
27 changes: 24 additions & 3 deletions docs/docs/production/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ import process from "process";
export const run = async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down Expand Up @@ -55,7 +62,14 @@ import {
export const run = async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down Expand Up @@ -90,7 +104,14 @@ export const run = async () => {
callbackManager.addHandler(new LangChainTracer());

const model = new OpenAI({ temperature: 0, callbackManager });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
for (const tool of tools) {
tool.callbackManager = callbackManager;
}
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/chat_convo_with_tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import { BufferMemory } from "langchain/memory";
export const run = async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new ChatOpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/chat_mrkl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { Calculator } from "langchain/tools/calculator";

export const run = async () => {
const model = new ChatOpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/chat_mrkl_with_tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { Calculator } from "langchain/tools/calculator";
export const run = async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new ChatOpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
18 changes: 16 additions & 2 deletions examples/src/agents/concurrent_mrkl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import {
export const run = async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down Expand Up @@ -47,7 +54,14 @@ export const run = async () => {
callbackManager.addHandler(new LangChainTracer());

const model = new OpenAI({ temperature: 0, callbackManager });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
for (const tool of tools) {
tool.callbackManager = callbackManager;
}
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/custom_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { LLMChain } from "langchain/chains";

export const run = async () => {
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const prefix = `Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:`;
const suffix = `Begin! Remember to speak as a pirate when giving your final answer. Use lots of "Args"
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/custom_llm_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ class CustomOutputParser extends AgentActionOutputParser {

export const run = async () => {
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const llmChain = new LLMChain({
prompt: new CustomPromptTemplate({
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/custom_llm_agent_chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ class CustomOutputParser extends AgentActionOutputParser {

export const run = async () => {
const model = new ChatOpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const llmChain = new LLMChain({
prompt: new CustomPromptTemplate({
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/load_from_hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { Calculator } from "langchain/tools/calculator";

export const run = async () => {
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const agent = await loadAgent(
"lc://agents/zero-shot-react-description/agent.json",
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/mrkl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { Calculator } from "langchain/tools/calculator";

export const run = async () => {
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
9 changes: 8 additions & 1 deletion examples/src/agents/mrkl_with_tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import process from "process";
export const run = async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
8 changes: 7 additions & 1 deletion examples/src/chat/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
} from "langchain/prompts";

export const run = async () => {
const tools = [new SerpAPI()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
];

const prompt = ZeroShotAgent.createPrompt(tools, {
prefix: `Answer the following questions as best you can, but speaking as a pirate might speak. You have access to the following tools:`,
Expand Down
8 changes: 7 additions & 1 deletion examples/src/chat/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ export const run = async () => {
// other abilities, such as search, or a calculator

// Define the list of tools the agent can use
const tools = [new SerpAPI()];
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
];
// Create the agent from the chat model and the tools
const agent = ChatAgent.fromLLMAndTools(new ChatOpenAI(), tools);
// Create an executor, which calls to the agent until an answer is found
Expand Down
18 changes: 16 additions & 2 deletions langchain/src/agents/tests/agent.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { initializeAgentExecutor } from "../initialize.js";

test("Run agent from hub", async () => {
const model = new OpenAI({ temperature: 0, modelName: "text-babbage-001" });
const tools: Tool[] = [new SerpAPI(), new Calculator()];
const tools: Tool[] = [
new SerpAPI(undefined, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const agent = await loadAgent(
"lc://agents/zero-shot-react-description/agent.json",
{ llm: model, tools }
Expand All @@ -27,7 +34,14 @@ test("Run agent from hub", async () => {

test("Run agent locally", async () => {
const model = new OpenAI({ temperature: 0, modelName: "text-babbage-001" });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(undefined, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
9 changes: 8 additions & 1 deletion langchain/src/callbacks/tests/langchain_tracer.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ test("Test LangChain tracer", async () => {
test.skip("Test Traced Agent with concurrency (skipped until we fix concurrency)", async () => {
process.env.LANGCHAIN_HANDLER = "langchain";
const model = new OpenAI({ temperature: 0 });
const tools = [new SerpAPI(), new Calculator()];
const tools = [
new SerpAPI(undefined, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];

const executor = await initializeAgentExecutor(
tools,
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/tools/serpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { Tool } from "./base.js";
* when used in `jest` tests. Part of the issue seems to be that the `serpapi`
* package imports a wasm module to use instead of native `fetch`, which we
* don't want anyway.
*
* NOTE: you must provide location, gl and hl or your region and language will
* may not match your location, and will not be deterministic.
*/

// Copied over from `serpapi` package
Expand Down Expand Up @@ -45,7 +48,7 @@ interface GoogleParameters extends BaseParameters {
* Location
* Parameter defines from where you want the search to originate. If several
* locations match the location requested, we'll pick the most popular one. Head to
* the [/locations.json API](https://serpapi.com/locations-api) if you need more
* [/locations.json API](https://serpapi.com/locations-api) if you need more
* precise control. location and uule parameters can't be used together. Avoid
* utilizing location when setting the location outside the U.S. when using Google
* Shopping and/or Google Product API.
Expand Down

0 comments on commit 4f67ed3

Please sign in to comment.