How to make an "AI agent" to monitor the cryptocurrency market in real time with the help of external data sources. You can expand more functions on this basis, I will briefly show you the idea.
1. Install dependencies
• openai: used to call Mars API.
• langchain: build AI Agent and manage tool calls.
• requests: used to request cryptocurrency market APIs (such as CoinGecko, Binance, etc.).
2. Prepare the "Tool" for obtaining cryptocurrency quotes
We will first use the CoinGecko free quote interface as an example. You can obtain prices without registration, but there is a speed limit.
Write a Python function that uses requests to call CoinGecko and returns the USD price of a certain cryptocurrency. You can use other exchanges or data sources, such as Binance, Coinbase, etc.
If you want to support multiple currencies or more details, such as trading volume, market capitalization, etc., you can call other CoinGecko APIs and return richer data.
3. Package this function into a Chain "Tool"
In Chain, each "Tool" needs to contain three core elements:
• Name (name)
• Function to be executed (func)
• Simple description (description)
In this way, the Chain Agent knows when to use this tool and how to use it.
4. Create a simplest AI agent
Use the initialize_agent function to quickly create an agent and specify:
1. tools: a list of tools that can be used.
2. llm: large language model (ChatGPT/GPT-4), etc.
3. agent type: dialogue strategy, such as ZERO_SHOT_REACT_DESCRIPTION (commonly used).
4. verbose: whether to print the internal reasoning process (useful for debugging).
4.1 Initialize LLM
First, pass in your API Key. Remember to replace the following "YOUR_OPENAI_API_KEY" with the key you generated on the AI platform.
4.2 Initializing Agent
It understands natural language and will call crypto_price_tool to get real-time data if necessary.
5. Talk to the agent and check the currency price
You can call
agent.run() like this:
# User asks in natural language
question = "Hello, I want to know how much Bitcoin is now?"
response =
agent.run(question)
print("Agent answer:", response)
# You can also directly enter the CoinGecko ID, such as 'ethereum'
question = "Check the current price of ethereum"
response =
agent.run(question)
print("Agent answer:", response)
6. Real-time monitoring (the simplest polling example)
If you want to do the most basic real-time monitoring, you can write a loop in the Python script, for example, automatically query the price every 60 seconds, and let the Agent interpret the market trend.
I wish you a happy realization and have fun!