rpc and edits
Browse files- .chainlit/config.toml +2 -2
- chatxbt-assistant.py +9 -5
- run.py +27 -6
- src/libs/rpc_client.py +25 -10
- src/tools/crypto_swap_toolkit.py +10 -36
.chainlit/config.toml
CHANGED
|
@@ -54,13 +54,13 @@ auto_tag_thread = true
|
|
| 54 |
|
| 55 |
[UI]
|
| 56 |
# Name of the app and chatbot.
|
| 57 |
-
name = "
|
| 58 |
|
| 59 |
# Show the readme while the thread is empty.
|
| 60 |
show_readme_as_default = true
|
| 61 |
|
| 62 |
# Description of the app and chatbot. This is used for HTML tags.
|
| 63 |
-
# description = ""
|
| 64 |
|
| 65 |
# Large size content are by default collapsed for a cleaner ui
|
| 66 |
default_collapse_content = true
|
|
|
|
| 54 |
|
| 55 |
[UI]
|
| 56 |
# Name of the app and chatbot.
|
| 57 |
+
name = "ChatXBT"
|
| 58 |
|
| 59 |
# Show the readme while the thread is empty.
|
| 60 |
show_readme_as_default = true
|
| 61 |
|
| 62 |
# Description of the app and chatbot. This is used for HTML tags.
|
| 63 |
+
# description = "AI-powered unification and execution protocol assitant for web3"
|
| 64 |
|
| 65 |
# Large size content are by default collapsed for a cleaner ui
|
| 66 |
default_collapse_content = true
|
chatxbt-assistant.py
CHANGED
|
@@ -15,7 +15,7 @@ from phi.storage.assistant.postgres import PgAssistantStorage
|
|
| 15 |
from src.knowledge_bases.combined import knowledge_base
|
| 16 |
|
| 17 |
storage = PgAssistantStorage(
|
| 18 |
-
#
|
| 19 |
table_name="assistant_runs",
|
| 20 |
db_engine=sqlalchemy_engine
|
| 21 |
)
|
|
@@ -57,15 +57,19 @@ async def set_starters():
|
|
| 57 |
)
|
| 58 |
]
|
| 59 |
|
| 60 |
-
|
| 61 |
@cl.on_chat_start
|
| 62 |
-
def start():
|
| 63 |
is_dev_mode = True if os.getenv("DEV_MODE") else False
|
| 64 |
|
| 65 |
# Initialize the assistant
|
| 66 |
cxbt_assistant = Assistant(
|
| 67 |
llm=OpenAIChat(model="gpt-4o"),
|
| 68 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
show_tool_calls= is_dev_mode,
|
| 70 |
markdown=True,
|
| 71 |
knowledge_base=knowledge_base,
|
|
@@ -82,11 +86,11 @@ def start():
|
|
| 82 |
|
| 83 |
@cl.on_message
|
| 84 |
async def main(message: cl.Message):
|
|
|
|
| 85 |
# Retrieve the assistant from the user session
|
| 86 |
agent = cl.user_session.get("agent")
|
| 87 |
|
| 88 |
# Process the user message using the assistant
|
| 89 |
-
# response = agent.run(message.content, stream=True)
|
| 90 |
response = ""
|
| 91 |
for delta in agent.run(message.content, stream=True):
|
| 92 |
response += delta
|
|
|
|
| 15 |
from src.knowledge_bases.combined import knowledge_base
|
| 16 |
|
| 17 |
storage = PgAssistantStorage(
|
| 18 |
+
# stores runs in the ai.assistant_runs table
|
| 19 |
table_name="assistant_runs",
|
| 20 |
db_engine=sqlalchemy_engine
|
| 21 |
)
|
|
|
|
| 57 |
)
|
| 58 |
]
|
| 59 |
|
|
|
|
| 60 |
@cl.on_chat_start
|
| 61 |
+
async def start():
|
| 62 |
is_dev_mode = True if os.getenv("DEV_MODE") else False
|
| 63 |
|
| 64 |
# Initialize the assistant
|
| 65 |
cxbt_assistant = Assistant(
|
| 66 |
llm=OpenAIChat(model="gpt-4o"),
|
| 67 |
+
tools=[
|
| 68 |
+
DuckDuckGo(),
|
| 69 |
+
CryptoDataTools(),
|
| 70 |
+
CryptoSwapTools(),
|
| 71 |
+
YFinanceTools(stock_price=True)
|
| 72 |
+
],
|
| 73 |
show_tool_calls= is_dev_mode,
|
| 74 |
markdown=True,
|
| 75 |
knowledge_base=knowledge_base,
|
|
|
|
| 86 |
|
| 87 |
@cl.on_message
|
| 88 |
async def main(message: cl.Message):
|
| 89 |
+
|
| 90 |
# Retrieve the assistant from the user session
|
| 91 |
agent = cl.user_session.get("agent")
|
| 92 |
|
| 93 |
# Process the user message using the assistant
|
|
|
|
| 94 |
response = ""
|
| 95 |
for delta in agent.run(message.content, stream=True):
|
| 96 |
response += delta
|
run.py
CHANGED
|
@@ -1,14 +1,35 @@
|
|
| 1 |
import json
|
| 2 |
import pprint
|
|
|
|
| 3 |
import asyncio
|
| 4 |
|
| 5 |
-
# import src.knowledge_bases.pipeline as Pipeline
|
| 6 |
|
| 7 |
-
# asyncio.run(Pipeline.main())
|
| 8 |
|
| 9 |
-
from src.data_sources.coin_gecko import CoinGecko
|
| 10 |
-
from src.data_sources.cryptocompare import CryptoCompare
|
| 11 |
|
| 12 |
-
cgc = CoinGecko()
|
| 13 |
|
| 14 |
-
pprint.pprint(cgc.get_trending_coin_list())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import pprint
|
| 3 |
+
import httpx
|
| 4 |
import asyncio
|
| 5 |
|
| 6 |
+
# # import src.knowledge_bases.pipeline as Pipeline
|
| 7 |
|
| 8 |
+
# # asyncio.run(Pipeline.main())
|
| 9 |
|
| 10 |
+
# from src.data_sources.coin_gecko import CoinGecko
|
| 11 |
+
# from src.data_sources.cryptocompare import CryptoCompare
|
| 12 |
|
| 13 |
+
# cgc = CoinGecko()
|
| 14 |
|
| 15 |
+
# pprint.pprint(cgc.get_trending_coin_list())
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
from src.libs.rpc_client import rpc_call
|
| 19 |
+
|
| 20 |
+
# response = rpc_call(method_name="getSwapSources")
|
| 21 |
+
|
| 22 |
+
async def main():
|
| 23 |
+
method_name = 'getSwapSources'
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = await rpc_call(method_name)
|
| 27 |
+
if response is not None:
|
| 28 |
+
print("RPC call successful!")
|
| 29 |
+
print(response)
|
| 30 |
+
else:
|
| 31 |
+
print("RPC call failed.")
|
| 32 |
+
except httpx.RequestError as e:
|
| 33 |
+
print(f"Error in main function: {e}")
|
| 34 |
+
|
| 35 |
+
asyncio.run(main())
|
src/libs/rpc_client.py
CHANGED
|
@@ -1,14 +1,29 @@
|
|
| 1 |
-
import os
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
-
|
| 4 |
-
import httpx
|
| 5 |
-
from rpcpy.client import Client
|
| 6 |
-
from rpcpy.serializers import MsgpackSerializer
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import httpx, uuid, os
|
| 2 |
+
from typing import Optional, Union
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from src.libs.logger import logger
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
+
rpc_server_url = os.getenv('CHATXBT_RPC_SERVER_URL')
|
| 9 |
+
|
| 10 |
+
async def rpc_call(method_name, params: Optional[Union[dict, list]] = {}, url: str = rpc_server_url):
|
| 11 |
+
headers = {
|
| 12 |
+
'Content-Type': 'application/json',
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
payload = {
|
| 16 |
+
'method': method_name,
|
| 17 |
+
'params': params,
|
| 18 |
+
'jsonrpc': '2.0',
|
| 19 |
+
'id': str(uuid.uuid4()),
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
async with httpx.AsyncClient() as client:
|
| 24 |
+
response = await client.post(url, json=payload, headers=headers)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
+
return response.json()
|
| 27 |
+
except httpx.RequestError as e:
|
| 28 |
+
print(f"Error making RPC call: {e}")
|
| 29 |
+
return None
|
src/tools/crypto_swap_toolkit.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
|
|
| 1 |
import json
|
|
|
|
| 2 |
import requests
|
| 3 |
from web3 import Web3
|
| 4 |
from phi.tools import Toolkit
|
|
@@ -6,6 +8,7 @@ from phi.utils.log import logger
|
|
| 6 |
from src.libs.web3 import get_web3_instance
|
| 7 |
from src.libs.helper_functions import get_headers, get_private_key
|
| 8 |
from src.libs.token_approval_helper import TokenApprovalHelper
|
|
|
|
| 9 |
|
| 10 |
class CryptoSwapTools(Toolkit):
|
| 11 |
def __init__(self, web3: Web3 = get_web3_instance()):
|
|
@@ -45,10 +48,8 @@ class CryptoSwapTools(Toolkit):
|
|
| 45 |
'sellToken': sell_token,
|
| 46 |
'sellAmount': sell_amount
|
| 47 |
}
|
| 48 |
-
response =
|
| 49 |
-
response
|
| 50 |
-
# return response.json()
|
| 51 |
-
return f"{(response.json())}"
|
| 52 |
except requests.exceptions.RequestException as e:
|
| 53 |
logger.warning(f"Failed to get swap quote: {e}")
|
| 54 |
# return {"error": str(e)}
|
|
@@ -76,10 +77,8 @@ class CryptoSwapTools(Toolkit):
|
|
| 76 |
'sellToken': sell_token,
|
| 77 |
'buyAmount': buy_amount
|
| 78 |
}
|
| 79 |
-
response =
|
| 80 |
-
response
|
| 81 |
-
# return response.json()
|
| 82 |
-
return f"{(response.json())}"
|
| 83 |
except requests.exceptions.RequestException as e:
|
| 84 |
logger.warning(f"Failed to get swap price: {e}")
|
| 85 |
# return {"error": str(e)}
|
|
@@ -96,15 +95,9 @@ class CryptoSwapTools(Toolkit):
|
|
| 96 |
>>> get_swap_sources()
|
| 97 |
"""
|
| 98 |
logger.info("Fetching swap sources")
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# return response.json()
|
| 103 |
-
return f"{(response.json())}"
|
| 104 |
-
except requests.exceptions.RequestException as e:
|
| 105 |
-
logger.warning(f"Failed to get swap sources: {e}")
|
| 106 |
-
# return {"error": str(e)}
|
| 107 |
-
return f"Error: {e}"
|
| 108 |
|
| 109 |
def execute_swap(self, buy_token: str, sell_token: str, sell_amount: str, eth_address: str) -> str:
|
| 110 |
"""
|
|
@@ -161,22 +154,3 @@ class CryptoSwapTools(Toolkit):
|
|
| 161 |
logger.warning(f"Failed to execute swap: {e}")
|
| 162 |
# return {"error": str(e)}
|
| 163 |
return f"Error: {e}"
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# Example usage
|
| 167 |
-
if __name__ == "__main__":
|
| 168 |
-
eth_address = '0xYourEthereumAddress' # Replace with your Ethereum address
|
| 169 |
-
|
| 170 |
-
# Initialize Web3
|
| 171 |
-
web3 = Web3(Web3.HTTPProvider('YOUR_INFURA_OR_ALCHEMY_URL'))
|
| 172 |
-
|
| 173 |
-
# Initialize CryptoSwapTools
|
| 174 |
-
swap_tool = CryptoSwapTools(web3)
|
| 175 |
-
|
| 176 |
-
# Fetch and print swap sources
|
| 177 |
-
sources = swap_tool.get_swap_sources()
|
| 178 |
-
print(f"Swap Sources: {sources}")
|
| 179 |
-
|
| 180 |
-
# Execute a swap (example)
|
| 181 |
-
receipt = swap_tool.execute_swap('DAI', 'ETH', '1000000000000000000', eth_address)
|
| 182 |
-
print(f"Swap Transaction Receipt: {receipt}")
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
import json
|
| 3 |
+
import pprint
|
| 4 |
import requests
|
| 5 |
from web3 import Web3
|
| 6 |
from phi.tools import Toolkit
|
|
|
|
| 8 |
from src.libs.web3 import get_web3_instance
|
| 9 |
from src.libs.helper_functions import get_headers, get_private_key
|
| 10 |
from src.libs.token_approval_helper import TokenApprovalHelper
|
| 11 |
+
from src.libs.rpc_client import rpc_call
|
| 12 |
|
| 13 |
class CryptoSwapTools(Toolkit):
|
| 14 |
def __init__(self, web3: Web3 = get_web3_instance()):
|
|
|
|
| 48 |
'sellToken': sell_token,
|
| 49 |
'sellAmount': sell_amount
|
| 50 |
}
|
| 51 |
+
response = asyncio.run(rpc_call(method_name="getSwapQuote", params=params))
|
| 52 |
+
return f"{response}"
|
|
|
|
|
|
|
| 53 |
except requests.exceptions.RequestException as e:
|
| 54 |
logger.warning(f"Failed to get swap quote: {e}")
|
| 55 |
# return {"error": str(e)}
|
|
|
|
| 77 |
'sellToken': sell_token,
|
| 78 |
'buyAmount': buy_amount
|
| 79 |
}
|
| 80 |
+
response = asyncio.run(rpc_call(method_name="getSwapPrice", params=params))
|
| 81 |
+
return f"{response}"
|
|
|
|
|
|
|
| 82 |
except requests.exceptions.RequestException as e:
|
| 83 |
logger.warning(f"Failed to get swap price: {e}")
|
| 84 |
# return {"error": str(e)}
|
|
|
|
| 95 |
>>> get_swap_sources()
|
| 96 |
"""
|
| 97 |
logger.info("Fetching swap sources")
|
| 98 |
+
|
| 99 |
+
response = asyncio.run(rpc_call(method_name="getSwapSources"))
|
| 100 |
+
return f"{response}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
def execute_swap(self, buy_token: str, sell_token: str, sell_amount: str, eth_address: str) -> str:
|
| 103 |
"""
|
|
|
|
| 154 |
logger.warning(f"Failed to execute swap: {e}")
|
| 155 |
# return {"error": str(e)}
|
| 156 |
return f"Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|