Skip to content

Commit 5e07ca9

Browse files
committed
Added lazai content
1 parent a74a8cc commit 5e07ca9

6 files changed

Lines changed: 598 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: LazAI Client
3+
---
4+
5+
What if we want to connect it to a larger, global network? How can we share data, offer services, or use services from other AI agents around the world in a secure and verifiable way?
6+
7+
This is where the LazAI network comes in, and your gateway to it is the LazAI Client.
8+
9+
## Your Passport to a Decentralized AI Nation
10+
11+
Imagine the LazAI network is a new digital country. This country has its own government (a blockchain), public records office, and a marketplace for AI services. To do anything in this country—like register property, open a business, or hire someone—you need a passport and a way to interact with its systems.
12+
13+
The `alith.lazai.Client` is your personal passport and remote control for the LazAI network.
14+
15+
You use it to:
16+
17+
- **Establish Your Identity:** It manages your digital wallet, which is your unique ID on the network.
18+
- **Interact with the "Government":** It lets you perform official actions on the blockchain, like registering data or services.
19+
- **Participate in the Economy:** It allows you to request services from other network members and handle payments.
20+
21+
Our goal is simple but fundamental: we'll use the `LazAI Client` to perform our first official act on the network: **registering a piece of data (a file's location) in the public record.**
22+
23+
## Getting Your Passport: Creating a Client
24+
25+
The first step to entering any new country is to get your passport. With Alith, this is incredibly easy.
26+
27+
**1. Import and Create the Client**
28+
29+
```python
30+
from alith.lazai import Client
31+
32+
# This one line connects you to the LazAI network
33+
client = Client()
34+
```
35+
36+
That's it! When you create an instance of `Client`, Alith automatically creates a new digital wallet for you (or loads an existing one). This wallet is your identity. You can think of it as your unique passport number.
37+
38+
**2. See Your New Identity**
39+
40+
Every passport has a unique number. You can see your wallet's public address like this:
41+
42+
```python
43+
print("My wallet address:", client.wallet.address)
44+
```
45+
46+
**Example Output:**
47+
48+
```
49+
My wallet address: 0xAbC123...dE45F6
50+
```
51+
52+
This address is your public identity on the LazAI network. You'll use it to sign transactions and prove ownership.
53+
54+
## Your First Official Act: Registering a File
55+
56+
Now that we have our passport, let's interact with the "public records office" (the blockchain). We're going to register the location of a file. This doesn't upload the file itself; it just creates a permanent, verifiable record on the blockchain that says, "This file, at this URL, is associated with me."
57+
58+
**1. Define the File's Location**
59+
60+
First, let's specify the URL of the file we want to register.
61+
62+
```python
63+
# The location of the data we want to register
64+
url = "https://example.com/my-awesome-dataset.csv"
65+
```
66+
67+
**2. Add the File to the LazAI Registry**
68+
69+
Next, we use our `client` to call the `add_file` function. This is like going to the records office and officially filing paperwork.
70+
71+
```python
72+
# Register the file on the blockchain
73+
file_id = client.add_file(url)
74+
75+
print("My file has been registered with ID:", file_id)
76+
```
77+
78+
The `client.add_file(url)` function sends a request to the LazAI network. The network processes it, adds the record to the blockchain, and gives you back a unique `file_id`. This ID is like your receipt or tracking number.
79+
80+
**3. Verify Your Registration**
81+
82+
How can we be sure our file was actually registered? We can ask the network to look it up for us using its URL.
83+
84+
```python
85+
# Ask the network to find the ID for our URL
86+
retrieved_id = client.get_file_id_by_url(url)
87+
88+
if retrieved_id == file_id:
89+
print("Success! The network confirms our file is registered.")
90+
```
91+
92+
This confirms that our transaction was successful and the record is now permanently on the blockchain for anyone to see. This simple process is the foundation for data contribution, which is described in detail in the next section.
93+
94+
## How Does It Work Under the Hood?
95+
96+
When you call `client.add_file()`, a few important things happen behind the scenes to ensure the process is secure and verifiable.
97+
98+
1. **Prepare the "Paperwork":** Your `Client` creates a formal request, called a "transaction," that says "Add this URL to the registry."
99+
2. **Sign with Your Passport:** The `Client` uses your private wallet key to digitally "sign" this transaction. This is like an official signature that proves the request came from you and nobody else.
100+
3. **Submit to the Government:** The signed transaction is sent to the LazAI blockchain.
101+
4. **An Official Record is Made:** The network validators check your signature, confirm the request is valid, and permanently add the information (the URL and your ownership) to the public ledger.
102+
5. **A Receipt is Issued:** The network assigns a new, unique `file_id` for this record and sends it back to your `Client`.
103+
104+
Here is a diagram showing the flow:
105+
106+
```mermaid
107+
sequenceDiagram
108+
participant YourScript as "Your Script"
109+
participant LazAIClient as LazAI Client
110+
participant Wallet
111+
participant LazChain as LazChain
112+
113+
YourScript->>LazAIClient: client.add_file("https://...")
114+
LazAIClient->>Wallet: "Please sign this transaction"
115+
Wallet-->>LazAIClient: Returns a digital signature
116+
LazAIClient->>LazChain: Submits the signed transaction
117+
LazChain-->>LazAIClient: Confirms transaction, returns file_id
118+
LazAIClient-->>YourScript: Returns the file_id
119+
```
120+
121+
## More Than Just Files: Becoming a Network Participant
122+
123+
The `LazAI Client` is your all-purpose tool for network interaction. Registering a file is just the beginning.
124+
125+
As seen in `lazai_inference_settlement.py`, before you can request paid services, you need to officially register as a "user" and deposit a small amount of funds to cover transaction fees.
126+
127+
```python
128+
# A simplified example of becoming a network user
129+
try:
130+
# Check if I'm already registered
131+
client.get_user(client.wallet.address)
132+
except Exception:
133+
# If not, register and deposit funds for fees
134+
client.add_user(1000000)
135+
```
136+
137+
This is a one-time setup step, like opening a bank account in our new digital country.
138+
139+
Furthermore, you can use the `Client` to register yourself as a **service provider** (a "node"). As shown in `lazai_add_node.py`, you can tell the network that you are ready to do work for others.
140+
141+
```python
142+
# A simplified example of registering your own service
143+
my_service_url = "https://my-ai-service.com"
144+
my_public_key = "-----BEGIN RSA PUBLIC KEY-----..."
145+
146+
# Announce your service to the network
147+
client.add_node(client.wallet.address, my_service_url, my_public_key)
148+
```
149+
150+
This action tells the LazAI network, "I'm open for business! Anyone who needs my AI service can find me at this address."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: LazAI
3+
---
4+
5+
Alith builds a decentralized ecosystem for AI services. It allows users to run and monetize AI tasks like _inference_, _data querying_, and _fine-tuning models_. The core idea is that **Agents** act as intelligent orchestrators, using various tools and data stores to complete tasks. All services provided by the network's **AI Nodes** are managed and paid for through a secure **blockchain-based settlement system**.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
title: Inference & Settlement
3+
---
4+
5+
Previously, we got our "passport" to the LazAI network. We learned how to create a digital identity and register a piece of data on the blockchain. We are now official citizens of this new digital country.
6+
7+
But what do we _do_ here? The most exciting part of the LazAI network is its bustling marketplace of AI services. What if your agent needs to think with a super-powerful AI brain that you can't run on your own computer? How do you securely "rent" time on someone else's machine and pay them for it?
8+
9+
## The Pay-As-You-Go AI Marketplace
10+
11+
Imagine you have an AI Agent but you want it to use a massive, cutting-edge AI model. Running this model requires expensive, powerful hardware that you don't have.
12+
13+
In a centralized world, you'd subscribe to a single company's service. In the LazAI network, you can access a global, decentralized marketplace of "inference nodes"—people and organizations all over the world who are renting out their AI processing power.
14+
15+
**Our goal:** We will take a simple `Agent` and make it run its "thinking" process (inference) on a remote machine in the LazAI network. We'll see how payment is handled automatically and securely, without needing to trust the node operator directly.
16+
17+
## The "Digital Check": How Settlement Works
18+
19+
How can a node operator trust that you'll pay them _after_ they've done the work? And how can you be sure you're only paying for the work you requested?
20+
21+
The solution is a "digital check" that you sign _before_ you even send your request. In Alith, these are called **settlement headers**.
22+
23+
When you want to ask a remote AI model a question, here's what happens:
24+
25+
1. **You Write a Digital Check:** Your `LazAI Client` creates a special set of data (the headers) that says, "I, wallet `0x123...`, promise to pay node `0xABC...` for this one request."
26+
2. **You Sign It:** You digitally sign this "check" with your wallet's private key. This is a cryptographic guarantee that it came from you.
27+
3. **You Attach It to Your Request:** Your AI's question and this signed check are sent together to the remote node.
28+
4. **The Node Verifies the Check:** Before doing any work, the node operator can quickly verify your signature and check the blockchain to see if you have enough funds deposited to cover the cost.
29+
5. **Work is Done & Payment is Claimed:** Once they see the check is valid, they run the AI model and send you the answer. Later, they can submit your signed "check" to the blockchain to claim their payment.
30+
31+
This creates a fair, trustless, and pay-as-you-go system.
32+
33+
## Using a Remote AI Model: A Step-by-Step Guide
34+
35+
Let's modify our `Agent` to use a remote AI model. We'll be referencing the logic in the [`lazai_inference_settlement.py`](https://github.com/0xLazAI/alith/blob/main/sdks/python/examples/lazai_inference_settlement.py) example file.
36+
37+
**1. One-Time Setup: Join the Club and Add Funds**
38+
39+
Before you can shop in the marketplace, you need to register as a user and deposit a small amount of funds to cover service fees. This is like opening a bank account in the new country.
40+
41+
_This is a one-time operation._ Once you've done it, you're ready to make requests.
42+
43+
```python
44+
from alith.lazai import LazAIClient
45+
46+
# This is the "address" of the service provider group we want to join
47+
LAZAI_IDAO_ADDRESS = "0x34d9E02F9bB4E4C8836e38DF4320D4a79106F194"
48+
client = LazAIClient()
49+
50+
# This block is only run once to set up your account
51+
try:
52+
client.get_user(client.wallet.address)
53+
print("User already registered!")
54+
except Exception:
55+
print("Registering user and depositing funds for services...")
56+
client.add_user(10000000) # Register on the network
57+
client.deposit_inference(LAZAI_IDAO_ADDRESS, 5000000) # Deposit funds
58+
```
59+
60+
This code first checks if you're already a registered user. If not, it registers you and deposits some funds into a special account for paying for inference services.
61+
62+
**2. Find a Service Provider (Inference Node)**
63+
64+
Now, let's find a shop to buy from. We'll ask our `client` for the web address (URL) of a registered inference node.
65+
66+
```python
67+
# Get the URL of a node that provides the service
68+
url = client.get_inference_node(LAZAI_IDAO_ADDRESS)[1]
69+
70+
print(f"Found an inference node at: {url}")
71+
```
72+
73+
This fetches a list of available providers from the blockchain and gives us the URL for one of them.
74+
75+
**3. Get Your Signed "Digital Check" (Settlement Headers)**
76+
77+
This is the most important step. We ask our `client` to generate the signed settlement headers for our request.
78+
79+
```python
80+
# Create the signed "digital check" for our request
81+
headers = client.get_request_headers(LAZAI_IDAO_ADDRESS)
82+
83+
print("Generated settlement headers:", headers)
84+
```
85+
86+
**Example Output:**
87+
88+
```
89+
Generated settlement headers: {'X-LazAI-User': '0x...', 'X-LazAI-Node': '0x34d9E...', 'X-LazAI-Nonce': 16, 'X-LazAI-Signature': '0x...'}
90+
```
91+
92+
This dictionary contains your identity (`X-LazAI-User`), the provider's address (`X-LazAI-Node`), a unique number for this request (`X-LazAI-Nonce`), and your unforgeable digital signature (`X-LazAI-Signature`).
93+
94+
**4. Create an Agent Pointing to the Remote Node**
95+
96+
Now, we create our `Agent`, but instead of letting it use a local model, we tell it to send its requests to the remote node's URL and to include our "digital check" with every request.
97+
98+
```python
99+
from alith import Agent
100+
101+
agent = Agent(
102+
model="Qwen-2.5", # The name of the model on the remote server
103+
base_url=f"{url}/v1", # The remote server's API address
104+
extra_headers=headers, # Attach our signed "digital check"
105+
)
106+
```
107+
108+
The `base_url` tells the agent _where_ to send its thoughts, and the `extra_headers` ensures it gets paid for.
109+
110+
**5. Talk to the Remote Agent!**
111+
112+
Now, you can use the agent just like before. But this time, the hard work is being done on a powerful remote machine!
113+
114+
```python
115+
response = agent.prompt("What is Alith?")
116+
print(response)
117+
```
118+
119+
When you run this line, your agent sends the question "What is Alith?" _plus_ your signed settlement headers to the remote node. The node verifies your payment guarantee, runs the `Qwen-2.5` model, and streams the answer back to you.
120+
121+
### How It Works Under the Hood
122+
123+
The process seems simple from your side, but it involves a beautifully coordinated dance between your script, the remote node, and the blockchain.
124+
125+
1. **You call `agent.prompt()`:** Your script wants an answer.
126+
2. **Agent Prepares Request:** The `Agent` creates a standard web request for the AI model.
127+
3. **Client Attaches Headers:** It automatically includes the `extra_headers` you provided.
128+
4. **Request Sent:** The complete package (prompt + headers) is sent over the internet to the remote inference node.
129+
5. **Node Verifies Signature:** The _first thing_ the remote node does is check your `X-LazAI-Signature`. It uses a public function to confirm that the request really came from your wallet address and hasn't been tampered with. It also checks the blockchain to ensure you have enough funds deposited.
130+
6. **Node Does the Work:** If the signature is valid, the node performs the computationally expensive AI inference.
131+
7. **Response Returned:** The AI's answer is sent back to your `Agent`.
132+
8. **Settlement Occurs:** The node now holds your signed "check" (the headers) as proof of work. It can submit this proof to the blockchain at any time to have the funds transferred from your deposit to its own account.
133+
134+
Here is a diagram of the flow:
135+
136+
```mermaid
137+
sequenceDiagram
138+
participant You as Alith Agent
139+
participant RemoteNode as Remote Inference Node
140+
participant LazChain as LazChain
141+
142+
You->>RemoteNode: Sends prompt + signed settlement headers
143+
RemoteNode->>LazChain: "Is this signature from this user valid?"
144+
LazChain-->>RemoteNode: "Yes, and they have funds."
145+
RemoteNode->>RemoteNode: Runs the AI computation
146+
RemoteNode-->>You: Returns the final AI-generated answer
147+
```
148+
149+
The code in [`lazai_inference_server.py`](https://github.com/0xLazAI/alith/blob/main/sdks/python/examples/lazai_inference_server.py) shows the other side of this transaction. It's a server that listens for requests, and its first step is to check for and validate these exact `X-LazAI-*` headers before it proceeds with any AI computation.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "LazAI",
3+
"pages": ["index", "client", "inference", "proof", "tee"]
4+
}

0 commit comments

Comments
 (0)