-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangchain_tool.py
More file actions
43 lines (31 loc) · 1.25 KB
/
Copy pathlangchain_tool.py
File metadata and controls
43 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""LangChain tool that fetches trading signals via x402 payments."""
import asyncio
from typing import Optional, Type
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
from x402_agent_kit import SignalFuse
class SignalInput(BaseModel):
symbol: str = Field(description="Cryptocurrency symbol, e.g. BTC, ETH, SOL")
class SignalFuseTool(BaseTool):
name: str = "signalfuse_signal"
description: str = (
"Fetch the latest trading signal for a cryptocurrency symbol. "
"Pays for the API call with USDC on Base via x402."
)
args_schema: Type[BaseModel] = SignalInput
wallet_key: str
def _run(self, symbol: str) -> dict:
sf = SignalFuse(wallet_key=self.wallet_key)
return asyncio.run(sf.get_signal(symbol))
async def _arun(self, symbol: str) -> dict:
sf = SignalFuse(wallet_key=self.wallet_key)
return await sf.get_signal(symbol)
# Usage with a LangChain agent:
#
# from langchain.agents import initialize_agent, AgentType
# from langchain_openai import ChatOpenAI
#
# tool = SignalFuseTool(wallet_key="0xYOUR_KEY")
# llm = ChatOpenAI()
# agent = initialize_agent([tool], llm, agent=AgentType.OPENAI_FUNCTIONS)
# agent.run("What is the signal for ETH?")