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

Add Google Finance Integration #7289

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
265 changes: 265 additions & 0 deletions docs/core_docs/docs/integrations/tools/google_finance.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
{
"cells": [
{
"cell_type": "raw",
"id": "10238e62-3465-4973-9279-606cbb7ccf16",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"sidebar_label: Google Finance Tool\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "a6f91f20",
"metadata": {},
"source": [
"# Google Finance Tool\n",
"\n",
"This notebook provides a quick overview for getting started with [`Google Finance Tool`](/docs/integrations/tools/). For detailed documentation of all `Google Finance Tool` features and configurations head to the [API reference](https://v03.api.js.langchain.com/modules/_langchain_community.tools_google_finance.html).\n",
"\n",
"This tool uses the [SerpApi Google Finance API](https://serpapi.com/google-finance-api), so you will need a [SerpApi API key](https://serpapi.com/dashboard).\n",
"\n",
"## Overview\n",
"\n",
"### Integration details\n",
"\n",
"| Class | Package | [PY support](https://python.langchain.com/docs/integrations/tools/google_finance/) | Package latest |\n",
"| :--- | :--- | :---: | :---: |\n",
"| [`SERPGoogleFinanceAPITool`](https://v03.api.js.langchain.com/modules/_langchain_community.tools_google_finance.html) | [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) | ✅ | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
"\n",
"## Setup\n",
"\n",
"The integration lives in the `@langchain/community` package.\n",
"\n",
"```{=mdx}\n",
"import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n",
"import Npm2Yarn from \"@theme/Npm2Yarn\";\n",
"\n",
"<IntegrationInstallTooltip></IntegrationInstallTooltip>\n",
"\n",
"<Npm2Yarn>\n",
" @langchain/community @langchain/core\n",
"</Npm2Yarn>\n",
"```\n",
"\n",
"### Credentials\n",
"\n",
"```typescript\n",
"process.env.SERPAPI_API_KEY=\"your-serpapi-api-key\"\n",
"```\n",
"\n",
"It's also helpful (but not needed) to set up [LangSmith](https://smith.langchain.com/) for best-in-class observability:\n",
"\n",
"```typescript\n",
"process.env.LANGCHAIN_TRACING_V2=\"true\"\n",
"process.env.LANGCHAIN_API_KEY=\"your-api-key\"\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "1c97218f-f366-479d-8bf7-fe9f2f6df73f",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"You can import and instantiate an instance of the `SERPGoogleFinanceAPITool` tool like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b3ddfe9-ca79-494c-a7ab-1f56d9407a64",
"metadata": {},
"outputs": [],
"source": [
"import { SERPGoogleFinanceAPITool } from \"@langchain/community/tools/google_finance\";\n",
"\n",
"const tool = new SERPGoogleFinanceAPITool({\n",
" // optional, either pass in your api key here or set it as an environment variable\n",
" apiKey: \"your-serpapi-api-key\",\n",
"});"
]
},
{
"cell_type": "markdown",
"id": "74147a1a",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"### [Invoke directly with args](/docs/concepts/#invoke-with-just-the-arguments)\n",
"\n",
"The input is a query string for anything you might search for on Google Finance (e.g. GOOG:NASDAQ):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65310a8b-eb0c-4d9e-a618-4f4abe2414fc",
"metadata": {},
"outputs": [],
"source": [
"await tool.invoke(\"GOOG:NASDAQ\")"
]
},
{
"cell_type": "markdown",
"id": "d6e73897",
"metadata": {},
"source": [
"### [Invoke with ToolCall](/docs/concepts/#invoke-with-toolcall)\n",
"\n",
"We can also invoke the tool with a model-generated `ToolCall`, in which case a `ToolMessage` will be returned:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f90e33a7",
"metadata": {},
"outputs": [],
"source": [
"// This is usually generated by a model, but we'll create a tool call directly for demo purposes.\n",
"const modelGeneratedToolCall = {\n",
" args: {\n",
" input: \"What is the price of GOOG:NASDAQ?\"\n",
" },\n",
" id: \"1\",\n",
" name: tool.name,\n",
" type: \"tool_call\",\n",
"}\n",
"await tool.invoke(modelGeneratedToolCall)"
]
},
{
"cell_type": "markdown",
"id": "659f9fbd-6fcf-445f-aa8c-72d8e60154bd",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can use our tool in a chain by first binding it to a [tool-calling model](/docs/how_to/tool_calling/) and then calling it:\n",
"\n",
"```{=mdx}\n",
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
"\n",
"<ChatModelTabs customVarName=\"llm\" />\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af3123ad-7a02-40e5-b58e-7d56e23e5830",
"metadata": {},
"outputs": [],
"source": [
"// @lc-docs-hide-cell\n",
"\n",
"import { ChatOpenAI } from \"@langchain/openai\"\n",
"\n",
"const llm = new ChatOpenAI({\n",
" model: \"gpt-4o-mini\",\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fdbf35b5-3aaf-4947-9ec6-48c21533fb95",
"metadata": {},
"outputs": [],
"source": [
"import { HumanMessage } from \"@langchain/core/messages\";\n",
"import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
"import { RunnableLambda } from \"@langchain/core/runnables\";\n",
"\n",
"const prompt = ChatPromptTemplate.fromMessages(\n",
" [\n",
" [\"system\", \"You are a helpful assistant.\"],\n",
" [\"placeholder\", \"{messages}\"],\n",
" ]\n",
")\n",
"\n",
"const llmWithTools = llm.bindTools([tool]);\n",
"\n",
"const chain = prompt.pipe(llmWithTools);\n",
"\n",
"const toolChain = RunnableLambda.from(\n",
" async (userInput: string, config) => {\n",
" const humanMessage = new HumanMessage(userInput,);\n",
" const aiMsg = await chain.invoke({\n",
" messages: [new HumanMessage(userInput)],\n",
" }, config);\n",
" const toolMsgs = await tool.batch(aiMsg.tool_calls, config);\n",
" return chain.invoke({\n",
" messages: [humanMessage, aiMsg, ...toolMsgs],\n",
" }, config);\n",
" }\n",
");\n",
"\n",
"const toolChainResult = await toolChain.invoke(\"What is the price of GOOG:NASDAQ?\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ac188a2",
"metadata": {},
"outputs": [],
"source": [
"const { tool_calls, content } = toolChainResult;\n",
"\n",
"console.log(\"AIMessage\", JSON.stringify({\n",
" tool_calls,\n",
" content,\n",
"}, null, 2));"
]
},
{
"cell_type": "markdown",
"id": "93848b02",
"metadata": {},
"source": [
"## Agents\n",
"\n",
"For guides on how to use LangChain tools in agents, see the [LangGraph.js](https://langchain-ai.github.io/langgraphjs/) docs."
]
},
{
"cell_type": "markdown",
"id": "4ac8146c",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all `Google Finance Tool` features and configurations, head to the [API reference](https://v03.api.js.langchain.com/modules/_langchain_community.tools_google_finance.html)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"language": "typescript",
"name": "deno"
},
"language_info": {
"codemirror_mode": "typescript",
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"name": "typescript",
"nbconvert_exporter": "script",
"pygments_lexer": "typescript",
"version": "5.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading