Copy
---
title: AINetwork Toolkit
---
>[AI Network](https://www.ainetwork.ai/build-on-ain)는 [$AIN token](https://www.ainetwork.ai/token)으로 구동되는 탈중앙화 GPU 네트워크를 활용하여 대규모 AI 모델을 수용하도록 설계된 레이어 1 블록체인으로, AI 기반 `NFT`(`AINFT`)를 강화합니다.
>
>`AINetwork Toolkit`은 [AINetwork Blockchain](https://www.ainetwork.ai/public/whitepaper.pdf)과 상호작용하기 위한 도구 모음입니다. 이 도구들을 사용하면 `AIN`을 전송하고, 값을 읽고 쓰며, 앱을 생성하고, 블록체인 데이터베이스 내 특정 경로에 대한 권한을 설정할 수 있습니다.
## 종속성 설치
AINetwork Toolkit을 사용하기 전에 ain-py 패키지를 설치해야 합니다. pip로 설치할 수 있습니다:
```python
pip install -qU ain-py langchain-community
환경 변수 설정
AIN Blockchain Account Private Key를AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY 환경 변수로 설정해야 합니다.
Copy
import os
os.environ["AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY"] = ""
AIN Blockchain private key 가져오기
Copy
import os
from ain.account import Account
if os.environ.get("AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY", None):
account = Account(os.environ["AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY"])
else:
account = Account.create()
os.environ["AIN_BLOCKCHAIN_ACCOUNT_PRIVATE_KEY"] = account.private_key
print(
f"""
address: {account.address}
private_key: {account.private_key}
"""
)
# IMPORTANT: If you plan to use this account in the future, make sure to save the
# private key in a secure place. Losing access to your private key means losing
# access to your account.
Copy
address: 0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac
private_key: f5e2f359bb6b7836a2ac70815473d1a290c517f847d096f5effe818de8c2cf14
AINetwork Toolkit 초기화
다음과 같이 AINetwork Toolkit을 초기화할 수 있습니다:Copy
from langchain_community.agent_toolkits.ainetwork.toolkit import AINetworkToolkit
toolkit = AINetworkToolkit()
tools = toolkit.get_tools()
address = tools[0].interface.wallet.defaultAccount.address
AINetwork Toolkit으로 Agent 초기화
다음과 같이 AINetwork Toolkit으로 agent를 초기화할 수 있습니다:Copy
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
llm = ChatOpenAI(temperature=0)
agent = create_agent(model=llm, tools=tools)
사용 예제
다음은 AINetwork Toolkit으로 agent를 사용하는 방법의 몇 가지 예제입니다:테스트할 App 이름 정의
Copy
appName = f"langchain_demo_{address.lower()}"
AINetwork Blockchain 데이터베이스에 app 생성
Copy
print(
agent.invoke(
{
"messages": f"Create an app in the AINetwork Blockchain database with the name {appName}"
}
)
)
Copy
> Entering new AgentExecutor chain...
Invoking: `AINappOps` with `{'type': 'SET_ADMIN', 'appName': 'langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac'}`
{"tx_hash": "0x018846d6a9fc111edb1a2246ae2484ef05573bd2c584f3d0da155fa4b4936a9e", "result": {"gas_amount_total": {"bandwidth": {"service": 4002, "app": {"langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac": 2}}, "state": {"service": 1640}}, "gas_cost_total": 0, "func_results": {"_createApp": {"op_results": {"0": {"path": "/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac", "result": {"code": 0, "bandwidth_gas_amount": 1}}, "1": {"path": "/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac", "result": {"code": 0, "bandwidth_gas_amount": 1}}, "2": {"path": "/manage_app/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/config/admin", "result": {"code": 0, "bandwidth_gas_amount": 1}}}, "code": 0, "bandwidth_gas_amount": 2000}}, "code": 0, "bandwidth_gas_amount": 2001, "gas_amount_charged": 5642}}The app with the name "langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac" has been created in the AINetwork Blockchain database.
> Finished chain.
The app with the name "langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac" has been created in the AINetwork Blockchain database.
AINetwork Blockchain 데이터베이스의 지정된 경로에 값 설정
Copy
print(
agent.run(f"Set the value {{1: 2, '34': 56}} at the path /apps/{appName}/object .")
)
Copy
> Entering new AgentExecutor chain...
Invoking: `AINvalueOps` with `{'type': 'SET', 'path': '/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/object', 'value': {'1': 2, '34': 56}}`
{"tx_hash": "0x3d1a16d9808830088cdf4d37f90f4b1fa1242e2d5f6f983829064f45107b5279", "result": {"gas_amount_total": {"bandwidth": {"service": 0, "app": {"langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac": 1}}, "state": {"service": 0, "app": {"langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac": 674}}}, "gas_cost_total": 0, "code": 0, "bandwidth_gas_amount": 1, "gas_amount_charged": 0}}The value {1: 2, '34': 56} has been set at the path /apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/object.
> Finished chain.
The value {1: 2, '34': 56} has been set at the path /apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/object.
AINetwork Blockchain 데이터베이스의 경로에 대한 권한 설정
Copy
print(
agent.run(
f"Set the write permissions for the path /apps/{appName}/user/$from with the"
" eval string auth.addr===$from ."
)
)
Copy
> Entering new AgentExecutor chain...
Invoking: `AINruleOps` with `{'type': 'SET', 'path': '/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/user/$from', 'eval': 'auth.addr===$from'}`
{"tx_hash": "0x37d5264e580f6a217a347059a735bfa9eb5aad85ff28a95531c6dc09252664d2", "result": {"gas_amount_total": {"bandwidth": {"service": 0, "app": {"langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac": 1}}, "state": {"service": 0, "app": {"langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac": 712}}}, "gas_cost_total": 0, "code": 0, "bandwidth_gas_amount": 1, "gas_amount_charged": 0}}The write permissions for the path `/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/user/$from` have been set with the eval string `auth.addr===$from`.
> Finished chain.
The write permissions for the path `/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac/user/$from` have been set with the eval string `auth.addr===$from`.
AINetwork Blockchain 데이터베이스의 경로에 대한 권한 조회
Copy
print(agent.run(f"Retrieve the permissions for the path /apps/{appName}."))
Copy
> Entering new AgentExecutor chain...
Invoking: `AINownerOps` with `{'type': 'GET', 'path': '/apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac'}`
{".owner": {"owners": {"0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac": {"branch_owner": true, "write_function": true, "write_owner": true, "write_rule": true}}}}The permissions for the path /apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac are as follows:
- Address: 0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac
- branch_owner: true
- write_function: true
- write_owner: true
- write_rule: true
> Finished chain.
The permissions for the path /apps/langchain_demo_0x5beb4defa2ccc274498416fd7cb34235dbc122ac are as follows:
- Address: 0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac
- branch_owner: true
- write_function: true
- write_owner: true
- write_rule: true
faucet에서 AIN 받기
Copy
!curl http://faucet.ainetwork.ai/api/test/{address}/
Copy
{"result":"0x0eb07b67b7d0a702cb60e865d3deafff3070d8508077ef793d69d6819fd92ea3","time":1692348112376}
AIN Balance 조회
Copy
print(agent.run(f"Check AIN balance of {address}"))
Copy
> Entering new AgentExecutor chain...
Invoking: `AINvalueOps` with `{'type': 'GET', 'path': '/accounts/0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac/balance'}`
100The AIN balance of address 0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac is 100 AIN.
> Finished chain.
The AIN balance of address 0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac is 100 AIN.
AIN 전송
Copy
print(
agent.run(
"Transfer 100 AIN to the address 0x19937b227b1b13f29e7ab18676a89ea3bdea9c5b"
)
)
Copy
> Entering new AgentExecutor chain...
Invoking: `AINtransfer` with `{'address': '0x19937b227b1b13f29e7ab18676a89ea3bdea9c5b', 'amount': 100}`
{"tx_hash": "0xa59d15d23373bcc00e413ac8ba18cb016bb3bdd54058d62606aec688c6ad3d2e", "result": {"gas_amount_total": {"bandwidth": {"service": 3}, "state": {"service": 866}}, "gas_cost_total": 0, "func_results": {"_transfer": {"op_results": {"0": {"path": "/accounts/0x5BEB4Defa2ccc274498416Fd7Cb34235DbC122Ac/balance", "result": {"code": 0, "bandwidth_gas_amount": 1}}, "1": {"path": "/accounts/0x19937B227b1b13f29e7AB18676a89EA3BDEA9C5b/balance", "result": {"code": 0, "bandwidth_gas_amount": 1}}}, "code": 0, "bandwidth_gas_amount": 0}}, "code": 0, "bandwidth_gas_amount": 1, "gas_amount_charged": 869}}The transfer of 100 AIN to the address 0x19937b227b1b13f29e7ab18676a89ea3bdea9c5b was successful. The transaction hash is 0xa59d15d23373bcc00e413ac8ba18cb016bb3bdd54058d62606aec688c6ad3d2e.
> Finished chain.
The transfer of 100 AIN to the address 0x19937b227b1b13f29e7ab18676a89ea3bdea9c5b was successful. The transaction hash is 0xa59d15d23373bcc00e413ac8ba18cb016bb3bdd54058d62606aec688c6ad3d2e.
Copy
---
<Callout icon="pen-to-square" iconType="regular">
[Edit the source of this page on GitHub.](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/ainetwork.mdx)
</Callout>
<Tip icon="terminal" iconType="regular">
[Connect these docs programmatically](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
</Tip>