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:
FMPDataToolkit: Creates collections of tools based on natural language queries
FMPDataTool: A single unified tool that automatically selects and uses the appropriate endpoints
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.
import os# Replace with your actual API keysos.environ["FMP_API_KEY"] = "your-fmp-api-key" # pragma: allowlist secretos.environ["OPENAI_API_KEY"] = "your-openai-api-key" # pragma: allowlist secret
It’s also helpful (but not needed) to set up LangSmith for best-in-class observability:
from langchain.agents import AgentExecutor, create_openai_functions_agentfrom langchain_openai import ChatOpenAI# Setupllm = ChatOpenAI(temperature=0)toolkit = FMPDataToolkit( query="Stock analysis", num_results=3,)tools = toolkit.get_tools()# Create agentprompt = "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,)# Run query# fmt: offresponse = agent_executor.invoke({"input": "What's the PE ratio of Microsoft?"})# fmt: on
# Initialize with custom settingsadvanced_tool = FMPDataTool( max_iterations=50, # Increase max iterations for complex queries temperature=0.2, # Adjust temperature for more/less focused responses)# Example of a complex multi-part analysisquery = """Analyze Apple's financial health by:1. Examining current ratios and debt levels2. Comparing profit margins to industry average3. Looking at cash flow trends4. Assessing growth metrics"""# fmt: offresponse = advanced_tool.invoke( { "query": query, "response_format": ResponseFormat.BOTH})# fmt: onprint("Detailed Financial Analysis:")print(response)
Main class for creating collections of FMP API tools:
Copy
Ask AI
from typing import Anyfrom langchain.tools import Toolclass 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.""" ...
from enum import Enumclass ResponseFormat(str, Enum): RAW = "raw" # Raw API response ANALYSIS = "text" # Natural language analysis BOTH = "both" # Both raw data and analysis
Assistant
Responses are generated using AI and may contain mistakes.