A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.langchain.com/docs/integrations/tools/fmp-data below:

FMP Data | 🦜️🔗 LangChain

FMP Data

Access financial market data through natural language queries.

Overview

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:

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.

Setup
!pip install -U langchain-fmp-data
import os


os.environ["FMP_API_KEY"] = "your-fmp-api-key"
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"

It's also helpful (but not needed) to set up LangSmith for best-in-class observability:

Instantiation

There are two main ways to instantiate the FMP LangChain integration:

  1. Using FMPDataToolkit
from langchain_fmp_data import FMPDataToolkit

query = "Get stock market prices and technical indicators"

toolkit = FMPDataToolkit(query=query)


market_toolkit = FMPDataToolkit(
query=query,
num_results=5,
)


custom_toolkit = FMPDataToolkit(
query="Financial analysis",
num_results=3,
similarity_threshold=0.4,
cache_dir="/custom/cache/path",
)
  1. Using FMPDataTool
from langchain_fmp_data import FMPDataTool
from langchain_fmp_data.tools import ResponseFormat


tool = FMPDataTool()


advanced_tool = FMPDataTool(
max_iterations=50,
temperature=0.2,
)
Invocation

The tools can be invoked in several ways:

Direct Invocation

tool_direct = FMPDataTool()



result = tool.invoke({"query": "What's Apple's current stock price?"})




detailed_result = tool_direct.invoke(
{
"query": "Compare Tesla and Ford's profit margins",
"response_format": ResponseFormat.BOTH,
}
)

Using with LangChain Agents
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI


llm = ChatOpenAI(temperature=0)
toolkit = FMPDataToolkit(
query="Stock analysis",
num_results=3,
)
tools = toolkit.get_tools()


prompt = "You are a helpful assistant. Answer the user's questions based on the provided context."
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
)



response = agent_executor.invoke({"input": "What's the PE ratio of Microsoft?"})

Advanced Usage

You can customize the tool's behavior:


advanced_tool = FMPDataTool(
max_iterations=50,
temperature=0.2,
)


query = """
Analyze Apple's financial health by:
1. Examining current ratios and debt levels
2. Comparing profit margins to industry average
3. Looking at cash flow trends
4. Assessing growth metrics
"""

response = advanced_tool.invoke(
{
"query": query,
"response_format": ResponseFormat.BOTH}
)

print("Detailed Financial Analysis:")
print(response)
Chaining

You can chain the tool similar to other tools simply by creating a chain with desired model.

from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI


llm = ChatOpenAI(temperature=0)
toolkit = FMPDataToolkit(query="Stock analysis", num_results=3)
tools = toolkit.get_tools()

llm_with_tools = llm.bind(functions=tools)
output_parser = StrOutputParser()

runner = llm_with_tools | output_parser



response = runner.invoke(
{
"input": "What's the PE ratio of Microsoft?"
}
)

API reference FMPDataToolkit

Main class for creating collections of FMP API tools:

from typing import Any

from langchain.tools import Tool


class FMPDataToolkit:
"""Creates a collection of FMP data tools based on queries."""

def __init__(
self,
query: str | None = None,
num_results: int = 3,
similarity_threshold: float = 0.3,
cache_dir: str | None = None,
): ...

def get_tools(self) -> list[Tool]:
"""Returns a list of relevant FMP API tools based on the query."""
...
FMPDataTool

Unified tool that automatically selects appropriate FMP endpoints:


class FMPDataTool:
"""Single unified tool for accessing FMP data through natural language."""

def __init__(
self,
max_iterations: int = 3,
temperature: float = 0.0,
): ...

def invoke(
self,
input: dict[str, Any],
) -> str | dict[str, Any]:
"""Execute a natural language query against FMP API."""
...


ResponseFormat

Enum for controlling response format:

from enum import Enum


class ResponseFormat(str, Enum):
RAW = "raw"
ANALYSIS = "text"
BOTH = "both"

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4