# Using NodeJS

#### Project Setup

```bash
mkdir lazai-inference
cd lazai-inference
npm init -y
```

#### Install Alith

```bash
npm i alith@latest
```

#### Create TypeScript Configuration

Create a file named `tsconfig.json` with the following content:

```json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "allowJs": true,
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true
  },
  "ts-node": {
    "esm": true
  },
  "include": ["*.ts"],
  "exclude": ["node_modules"]
}
```

#### Set Environment Variables

For OpenAI/ChatGPT API:

```bash
export PRIVATE_KEY=<your wallet private key>
export OPENAI_API_KEY=<your openai api key>
```

For other OpenAI-compatible APIs (DeepSeek, Gemini, etc.):

```bash
export PRIVATE_KEY=<your wallet private key>
export LLM_API_KEY=<your api key>
export LLM_BASE_URL=<your api base url>
```

#### Step 1: Request Inference via LazAI Client

Create a file named `app.ts` with the following content:

```typescript
import { ChainConfig, Client } from "alith/lazai";
import { Agent } from "alith";
 
// Set up the private key for authentication
process.env.PRIVATE_KEY = "<your wallet private key>";
 
const node = "0xc3e98E8A9aACFc9ff7578C2F3BA48CA4477Ecf49"; // Replace with your own inference node address
const client = new Client(ChainConfig.testnet());
 
await client.getUser(client.getWallet().address);
 
console.log(
  "The inference account of user is",
  await client.getInferenceAccount(client.getWallet().address, node)
);
 
const fileId = 10;
const nodeInfo = await client.getInferenceNode(node);
const url = nodeInfo.url;
const agent = new Agent({
  // OpenAI-compatible inference server URL
  baseUrl: `${url}/v1`,
  model: "gpt-3.5-turbo",
  // Extra headers for settlement and DAT file anchoring
  extraHeaders: await client.getRequestHeaders(node, BigInt(fileId)),
});
console.log(await agent.prompt("What is Alith?"));
```

#### Step 2: Run the Application

```bash
npx tsx app.ts
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lazai.network/private-data-inference/lazai-api/using-nodejs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
