Skip to content

Commit 8910d03

Browse files
committed
feat: added examples and execution instructions
1 parent ea69216 commit 8910d03

File tree

7 files changed

+157
-8
lines changed

7 files changed

+157
-8
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ const response = await client.chat.completions.create({
6161
console.log(response.choices[0].message.content);
6262
```
6363

64+
65+
To execute it:
66+
```
67+
pnpm i
68+
pnpm exec tsx examples/0-api-key.ts
69+
```
70+
6471
### 2. Delegation Token Mode (Advanced)
6572

6673
For more secure, distributed access where you want to separate server credentials from client usage.
@@ -107,6 +114,13 @@ const response = await client.chat.completions.create({
107114
console.log(response.choices[0].message.content);
108115
```
109116

117+
118+
To execute it:
119+
```
120+
pnpm i
121+
pnpm exec tsx examples/1-delegation-token.ts
122+
```
123+
110124
### 3. Environment Configuration
111125

112126
Create a `.env` file for your credentials:

examples/0-api-key.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "dotenv/config";
2+
import { NilaiOpenAIClient, NilAuthInstance } from "@nillion/nilai-ts";
3+
4+
// To obtain an API key, navigate to https://nilpay.vercel.app/
5+
// and create a new subscription.
6+
// The API key will be displayed in the subscription details.
7+
// The NilaiOpenAIClient class automatically handles the NUC token creation and management.
8+
9+
const API_KEY = process.env.NILLION_API_KEY;
10+
11+
async function main() {
12+
// Initialize the client in API key mode
13+
// For sandbox, use the following:
14+
const client = new NilaiOpenAIClient({
15+
baseURL: "https://nilai-a779.nillion.network/nuc/v1/",
16+
apiKey: API_KEY,
17+
nilauthInstance: NilAuthInstance.SANDBOX,
18+
// For production, use the following:
19+
// nilauthInstance: NilAuthInstance.PRODUCTION,
20+
});
21+
22+
// Make a request to the Nilai API
23+
const response = await client.chat.completions.create({
24+
model: "meta-llama/Llama-3.2-3B-Instruct",
25+
messages: [
26+
{ role: "user", content: "Hello! Can you help me with something?" }
27+
],
28+
});
29+
30+
console.log(`Response: ${response.choices[0].message.content}`);
31+
}
32+
33+
// Run the example
34+
main().catch(console.error);

examples/1-delegation-token.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import "dotenv/config";
2+
import {
3+
NilaiOpenAIClient,
4+
DelegationTokenServer,
5+
AuthType,
6+
type DelegationTokenRequest,
7+
type DelegationTokenResponse,
8+
NilAuthInstance,
9+
} from "@nillion/nilai-ts";
10+
11+
// To obtain an API key, navigate to https://nilpay.vercel.app/
12+
// and create a new subscription.
13+
// The API key will be displayed in the subscription details.
14+
15+
const API_KEY = process.env.NILLION_API_KEY;
16+
17+
async function main() {
18+
if (!API_KEY) {
19+
throw new Error("NILLION_API_KEY environment variable is required");
20+
}
21+
22+
// >>> Server initializes a delegation token server
23+
// The server is responsible for creating delegation tokens
24+
// and managing their expiration and usage.
25+
const server = new DelegationTokenServer(
26+
API_KEY,
27+
{
28+
nilauthInstance: NilAuthInstance.SANDBOX,
29+
expirationTime: 10, // 10 seconds validity of delegation tokens
30+
tokenMaxUses: 1, // 1 use of a delegation token
31+
},
32+
// For production instances, use the following:
33+
// NilAuthInstance.PRODUCTION,
34+
);
35+
36+
// >>> Client initializes a client
37+
// The client is responsible for making requests to the Nilai API.
38+
// We do not provide an API key but we set the auth type to DELEGATION_TOKEN
39+
const client = new NilaiOpenAIClient({
40+
baseURL: "https://nilai-a779.nillion.network/nuc/v1/",
41+
authType: AuthType.DELEGATION_TOKEN,
42+
// For production instances, use the following:
43+
// nilauthInstance: NilAuthInstance.PRODUCTION,
44+
});
45+
46+
// >>> Client produces a delegation request
47+
const delegationRequest: DelegationTokenRequest = client.getDelegationRequest();
48+
49+
// <<< Server creates a delegation token
50+
const delegationToken: DelegationTokenResponse = await server.createDelegationToken(
51+
delegationRequest
52+
);
53+
54+
// >>> Client sets internally the delegation token
55+
client.updateDelegation(delegationToken);
56+
57+
// >>> Client uses the delegation token to make a request
58+
const response = await client.chat.completions.create({
59+
model: "meta-llama/Llama-3.2-3B-Instruct",
60+
messages: [
61+
{ role: "user", content: "Hello! Can you help me with something?" }
62+
],
63+
});
64+
65+
console.log(`Response: ${response.choices[0].message.content}`);
66+
}
67+
68+
// Run the example
69+
main().catch(console.error);

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"lint": "biome lint .",
2424
"lint:fix": "biome lint --write .",
2525
"ci": "biome check .",
26-
"prepublishOnly": "pnpm run build"
26+
"prepublishOnly": "pnpm run build",
27+
"example:api-key": "tsx examples/0-api-key.ts",
28+
"example:delegation-token": "tsx examples/1-delegation-token.ts"
2729
},
2830
"keywords": [
2931
"openai",
@@ -52,6 +54,7 @@
5254
"dotenv": "^16.5.0",
5355
"eslint": "^9.29.0",
5456
"tsup": "^8.5.0",
57+
"tsx": "^4.20.3",
5558
"typescript": "^5.8.3",
5659
"vite-tsconfig-paths": "^5.1.4",
5760
"vitest": "^2.1.9"

pnpm-lock.yaml

Lines changed: 32 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { NilaiOpenAIClient } from "./client";
2-
//export { DelegationTokenServer } from './server';
2+
export { DelegationTokenServer } from './server';
33
export {
44
AuthType,
55
NilAuthInstance,

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525

2626
"baseUrl": ".",
2727
"paths": {
28-
"#/*": ["src/*"]
28+
"#/*": ["src/*"],
29+
"@nillion/nilai-ts": ["src/index.ts"]
2930
}
3031
},
31-
"include": ["./src/**/*.ts", "./tests/**/*.ts"]
32+
"include": ["./src/**/*.ts", "./tests/**/*.ts", "./examples/**/*.ts"]
3233
}

0 commit comments

Comments
 (0)