Skip to content

Commit

Permalink
Simplify and restructure FMPData integration tutorial
Browse files Browse the repository at this point in the history
Replaced verbose and redundant notebook content with a cleaner, modular structure. Focused on providing clear setup instructions, concise component descriptions, and flexible usage examples for FMPDataToolkit and FMPDataTool.
  • Loading branch information
MehdiZare committed Jan 12, 2025
1 parent 042d514 commit 97405f2
Showing 1 changed file with 97 additions and 114 deletions.
211 changes: 97 additions & 114 deletions docs/docs/integrations/tools/fmp-data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"source": [
"---\n",
"sidebar_label: FmpData\n",
"sidebar_label: FMPData\n",
"---"
]
},
Expand All @@ -15,12 +15,14 @@
"id": "a6f91f20",
"metadata": {},
"source": [
"## LangChain FMP Data Tutorial\n",
"This notebook demonstrates how to use the langchain-fmp-data package to access financial market data through LangChain.\n",
"## Overview\n",
"The FMP (Financial Modeling Prep) LangChain integration provides a seamless way to access financial market data through natural language queries. This integration offers two main components:\n",
"\n",
"## Setup\n",
"First, let's install the required packages:\n",
"\n"
"- FMPDataToolkit: Creates collections of tools based on natural language queries\n",
"- FMPDataTool: A single unified tool that automatically selects and uses the appropriate endpoints\n",
"\n",
"The integration leverages LangChain's semantic search capabilities to match user queries with the most relevant FMP API endpoints, making financial data access more intuitive and efficient.\n",
"## Setup"
]
},
{
Expand All @@ -31,21 +33,6 @@
"outputs": [],
"source": "!pip install -U langchain-fmp-data"
},
{
"cell_type": "markdown",
"id": "b15e9266",
"metadata": {},
"source": [
"### Credentials\n",
"\n",
"To use this package, you'll need:\n",
"\n",
"- An FMP (Financial Modeling Prep) API key from financialmodelingprep.com\n",
"- An OpenAI API key\n",
"\n",
"Let's set up our environment variables:"
]
},
{
"cell_type": "code",
"execution_count": 2,
Expand Down Expand Up @@ -80,61 +67,79 @@
]
},
{
"cell_type": "markdown",
"id": "1c97218f-f366-479d-8bf7-fe9f2f6df73f",
"metadata": {},
"cell_type": "markdown",
"source": [
"## Using FMPDataToolkit\n",
"The FMPDataToolkit provides a collection of tools based on your natural language query. Here are some examples:\n"
]
"## Instantiation\n",
"There are two main ways to instantiate the FMP LangChain integration:\n",
"1. Using FMPDataToolkit"
],
"id": "b15e9266"
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8b3ddfe9-ca79-494c-a7ab-1f56d9407a64",
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from langchain_fmp_data import FMPDataToolkit\n",
"\n",
"# Example 1: Getting stock market data tools\n",
"market_toolkit = FMPDataToolkit(\n",
" query=\"Get stock market prices and technical indicators\", num_results=5\n",
")\n",
"market_tools = market_toolkit.get_tools()\n",
"print(\"\\nMarket Analysis Tools:\")\n",
"for tool in market_tools:\n",
" print(f\"- {tool.name}: {tool.description}\")\n",
"# Basic instantiation\n",
"toolkit = FMPDataToolkit()\n",
"\n",
"# Example 2: Getting fundamental analysis tools\n",
"fundamental_toolkit = FMPDataToolkit(\n",
" query=\"Company financial statements and fundamental ratios\",\n",
"# Instantiation with specific query focus\n",
"market_toolkit = FMPDataToolkit(\n",
" query=\"Get stock market prices and technical indicators\",\n",
" num_results=5\n",
")\n",
"fundamental_tools = fundamental_toolkit.get_tools()\n",
"print(\"\\nFundamental Analysis Tools:\")\n",
"for tool in fundamental_tools:\n",
" print(f\"- {tool.name}: {tool.description}\")\n",
"\n",
"# Example 3: Getting economic data tools\n",
"economic_toolkit = FMPDataToolkit(\n",
" query=\"Economic indicators and market statistics\",\n",
" num_results=5\n",
")\n",
"economic_tools = economic_toolkit.get_tools()\n",
"print(\"\\nEconomic Analysis Tools:\")\n",
"for tool in economic_tools:\n",
" print(f\"- {tool.name}: {tool.description}\")"
]
"# Instantiation with custom configuration\n",
"custom_toolkit = FMPDataToolkit(\n",
" query=\"Financial analysis\",\n",
" num_results=3,\n",
" similarity_threshold=0.4,\n",
" cache_dir=\"/custom/cache/path\"\n",
")"
],
"id": "615375140978a557"
},
{
"metadata": {},
"cell_type": "markdown",
"id": "74147a1a",
"source": "2. Using FMPDataTool\n",
"id": "fd72621083777e68"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": 4,
"source": [
"## Integrating with LangChain Agents\n",
"You can use these tools with LangChain agents:\n"
]
"from langchain_fmp_data import FMPDataTool\n",
"from langchain_fmp_data.tools import ResponseFormat\n",
"\n",
"# Basic instantiation\n",
"tool = FMPDataTool()\n",
"\n",
"# Advanced instantiation with custom settings\n",
"advanced_tool = FMPDataTool(\n",
" max_iterations=50,\n",
" temperature=0.2,\n",
")"
],
"id": "8b3ddfe9-ca79-494c-a7ab-1f56d9407a64"
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Invocation\n",
"The tools can be invoked in several ways:\n",
"\n",
"### Direct Invocation\n",
"\n"
],
"id": "74147a1a"
},
{
"cell_type": "code",
Expand All @@ -143,45 +148,26 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langchain.agents import create_openai_functions_agent\n",
"from langchain.agents import AgentExecutor\n",
"\n",
"# Initialize the LLM\n",
"llm = ChatOpenAI(temperature=0)\n",
"\n",
"# Create toolkit and get tools\n",
"toolkit = FMPDataToolkit(\n",
" query=\"Stock prices and company financials\",\n",
" num_results=3\n",
")\n",
"tools = toolkit.get_tools()\n",
"\n",
"# Create and run agent\n",
"agent = create_openai_functions_agent(llm, tools)\n",
"agent_executor = AgentExecutor(agent=agent, tools=tools)\n",
"# Using FMPDataTool\n",
"tool = FMPDataTool()\n",
"\n",
"# Example queries\n",
"queries = [\n",
" \"What's the current stock price of Apple?\",\n",
" \"Show me Microsoft's revenue growth over the last year\",\n",
" \"Compare Tesla and Ford's profit margins\",\n",
"]\n",
"# Basic query\n",
"result = tool.invoke({\n",
" \"query\": \"What's Apple's current stock price?\"\n",
"})\n",
"\n",
"for query in queries:\n",
" print(f\"\\nQuery: {query}\")\n",
" response = agent_executor.invoke({\"input\": query})\n",
" print(f\"Response: {response['output']}\")"
"# Advanced query with specific format\n",
"detailed_result = tool.invoke({\n",
" \"query\": \"Compare Tesla and Ford's profit margins\",\n",
" \"response_format\": ResponseFormat.BOTH\n",
"})"
]
},
{
"cell_type": "markdown",
"id": "d6e73897",
"metadata": {},
"source": [
"## Using FMPDataTool\n",
"The FMPDataTool provides a single, powerful tool that can answer financial queries by automatically selecting the right endpoints:\n"
]
"source": "### Using with LangChain Agents"
},
{
"cell_type": "code",
Expand All @@ -190,34 +176,22 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_fmp_data import FMPDataTool\n",
"from langchain_fmp_data.tools import ResponseFormat\n",
"\n",
"# Initialize the tool\n",
"tool = FMPDataTool()\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain.agents import create_openai_functions_agent, AgentExecutor\n",
"\n",
"# Example 1: Basic stock price query\n",
"query1 = \"What's the current price of Bitcoin?\"\n",
"response1 = tool.invoke({\"query\": query1})\n",
"print(f\"\\nQuery: {query1}\")\n",
"print(f\"Response: {response1}\")\n",
"# Setup\n",
"llm = ChatOpenAI(temperature=0)\n",
"toolkit = FMPDataToolkit(query=\"Stock analysis\", num_results=3)\n",
"tools = toolkit.get_tools()\n",
"\n",
"# Example 2: Complex financial analysis\n",
"query2 = \"Compare the debt ratios of Apple and Microsoft\"\n",
"response2 = tool.invoke(\n",
" {\"query\": query2, \"response_format\": ResponseFormat.BOTH}\n",
")\n",
"print(f\"\\nQuery: {query2}\")\n",
"print(f\"Response: {response2}\")\n",
"# Create agent\n",
"agent = create_openai_functions_agent(llm, tools)\n",
"agent_executor = AgentExecutor(agent=agent, tools=tools)\n",
"\n",
"# Example 3: Market analysis with technical indicators\n",
"query3 = \"Show me the RSI and MACD indicators for Tesla stock\"\n",
"response3 = tool.invoke({\n",
" \"query\": query3,\n",
" \"response_format\": ResponseFormat.DATA_STRUCTURE # Get just the structured data\n",
"})\n",
"print(f\"\\nQuery: {query3}\")\n",
"print(f\"Response: {response3}\")"
"# Run query\n",
"response = agent_executor.invoke({\n",
" \"input\": \"What's the PE ratio of Microsoft?\"\n",
"})"
]
},
{
Expand Down Expand Up @@ -260,6 +234,15 @@
"print(\"Detailed Financial Analysis:\")\n",
"print(response)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"### API Reference\n",
"### FMPDataToolkit\n"
],
"id": "9b9dc04b8a68e94c"
}
],
"metadata": {
Expand Down

0 comments on commit 97405f2

Please sign in to comment.