# Self-Hosting AI-Agent-Server

### Introduction

By following this example, we will be able to self-host the AI element of the SDK, specifically the `AI agent client`, independently. This offers developers and projects greater control and flexibility compared to using the AI agent API directly. Underneath, the AI agent client utilizes the package to obtain data and interact with Cronos Chains. The example is fully opensource and can be found in this [repository](https://github.com/crypto-com/developer-platform-sdk-examples/tree/main/ai/cryptocom-ai-agent-service)<br>

Step 1 - Clone the repository:

```
git clone https://github.com/crypto-com/developer-platform-sdk-examples.git
cd developer-platform-sdk-examples/ai/cryptocom-ai-agent-service
```

#### Step 2 - **Install dependencies**:

```
npm install
```

**Step 3 - Create a `.env` file with the following content:**

```
NODE_ENV=development
EXPLORER_API_KEY=<Explorer_API_Key>
```

{% hint style="info" %}
We can follow the guide in [#explorer-api-keys](https://ai-agent-sdk-docs.crypto.com/resources/resources-for-developers#explorer-api-keys "mention") to obtain our `Explorer_API_Key`, the explorer API keys of different Cronos chains
{% endhint %}

{% hint style="info" %}
The example is configured for the ***Cronos zkEVM Testnet.*** When connecting to another network, please update the chain in [Client.init](https://github.com/crypto-com/developer-platform-sdk-examples/blob/main/ai/cryptocom-ai-agent-service/src/services/agent/agent.service.ts#L33) within `agent.service.ts` accordingly.

```javascript
// Cronos EVM mainnet
CronosEvm.Mainnet 
// Cronos EVM testnet
CronosEvm.Testnet
// Cronos zkEVM mainnet
CronosZkEvm.Mainnet
```

{% endhint %}

**Step 4 - Run the development server / Production build:**

**Run the development server by**

```
npm run dev
```

Once done, the `AI agent client` will be running under `localhost:8000`

OR&#x20;

**Production build by**&#x20;

```
npm run build
npm start
```

Afterwards, we can interact with the `AI agent client` in a similar manner&#x20;

***

### API Endpoints

#### Health Check

* **Endpoint**: `/healthcheck`
* **Method**: `GET`
* **Description**: Returns the uptime and health status of the service.
* **Response Example**:

  ```
  {
    "status": "success",
    "result": {
      "uptime": 120.34,
      "responsetime": [0, 252939],
      "message": "OK",
      "timestamp": 1632846348163
    }
  }
  ```

#### Query Route

{% hint style="info" %}
More query example can be found in [functions-and-example-queries-of-ai-agent-services](https://ai-agent-sdk-docs.crypto.com/outdated-contents/functions-and-example-queries-of-ai-agent-services "mention")
{% endhint %}

* **Endpoint**: `/api/v1/cdc-ai-agent-service/query`
* **Method**: `POST`
* **Description**: Takes a natural language query, maps it to a blockchain command via OpenAI, and executes the command.
* **Request Body Example**:

  <pre><code>{
    "query": "get latest block",
    "options": {
      "openAI": {
        "apiKey": "&#x3C;your-openai-api-key>"
      },
  <strong>    "chain": {
  </strong>      "id": "388",
        "name": "cronos-zkevm-testnet",
        "rpc": "https://testnet.zkevm.cronos.org"
      },
      "explorer": {
        "apiKey": "&#x3C;your-explorer-api-key>"
      }
    }
  }
  </code></pre>
* **Response Example**:

  ```
  {
    "status": "success",
    "result": {
      "action": "getBlock",
      "message": "Retrieved latest block",
      "data": {
        "blockNumber": 123456,
        "timestamp": "2023-09-12T10:12:15Z"
      }
    }
  }

  ```

#### **Querying with DeepSeek Model**

* **Endpoint**: `/api/v1/cdc-ai-agent-service/query`
* **Method**: `POST`
* **Description**: Takes a natural language query, maps it to a blockchain command via DeepSeek, and executes the command.
* **Request Body Example**:

  ```
  {
    "query": "get latest block",
    "options": {
      "deepSeek": {
        "apiKey": "<your-deepseek-api-key>",
        "model": "deepseek-chat"
      },
      "llmProvider": "deepseek",
      "chain": {
        "id": "388",
        "name": "cronos-zkevm-testnet",
        "rpc": "https://testnet.zkevm.cronos.org"
      },
      "explorer": {
        "apiKey": "<your-explorer-api-key>"
      }
    }
  }
  ```
* **Response Example**:

  ```
  {
    "status": "success",
    "result": {
      "action": "getBlock",
      "message": "Retrieved latest block",
      "data": {
        "blockNumber": 123456,
        "timestamp": "2023-09-12T10:12:15Z"
      }
    }
  }
  ```

#### **Querying with M**istral **Models**

* **Endpoint**: `/api/v1/cdc-ai-agent-service/query`
* **Method**: `POST`
* **Description**: Takes a natural language query, maps it to a blockchain command via DeepSeek, and executes the command.
* Supported models: `ministral-3b-latest`, `ministral-8b-latest`, `mistral-large-latest`, `mistral-small-latest`
* **Request Body Example With** `mistral-large-latest` :

  ```
  {
    "query": "get latest block",
    "options": {
      "mistral": {
        "apiKey": "<your-mistral-api-key>",
        "model": "mistral-large-latest"
      },
      "llmProvider": "mistral",
      "chain": {
        "id": "388",
        "name": "cronos-zkevm-testnet",
        "rpc": "https://testnet.zkevm.cronos.org"
      },
      "explorer": {
        "apiKey": "<your-explorer-api-key>"
      }
    }
  }
  ```
* **Response Example**:

  ```
  {
    "status": "success",
    "result": {
      "action": "getBlock",
      "message": "Retrieved latest block",
      "data": {
        "blockNumber": 123456,
        "timestamp": "2023-09-12T10:12:15Z"
      }
    }
  }
  ```

## Experimental feature - Assigning a key to the AI Agent

{% hint style="warning" %}
Please note that the following is an experimental feature intended for demonstration purposes only. Use this feature with caution, as it may result in the loss of funds.
{% endhint %}

As introduced in this [pull request](https://github.com/crypto-com/developer-platform-sdk-examples/pull/12), developers can follow the [instructions](https://github.com/crypto-com/developer-platform-sdk-examples/pull/12/files#diff-8fdb674999321ed747ca07aa825782ce00251fe4a96ef303079243423e34495fR172) to assign a private key directly to the AI agent, enabling it to perform transactions on the user's behalf.

In particular, we need to update the `agent.service.ts` file and the `.env` file with additional variables, such as the key to be assigned, the RPC URL for transaction broadcasting (see [#cronos-zkevm-sepolia-testnet-urls](https://ai-agent-sdk-docs.crypto.com/resources/resources-for-developers#cronos-zkevm-sepolia-testnet-urls "mention")) and the DEX router for swaping:&#x20;

```
NODE_ENV=development
EXPLORER_API_KEY=
PRIVATE_KEY=
RPC_URL=
DEX_ROUTER_ADDRESS=
```

<figure><img src="https://524814724-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FktUMyU5bBece2vVjrTg7%2Fuploads%2FCgwY6fmzIJuyERaYSvgT%2FScreenshot%202024-11-19%20at%2016.28.44.png?alt=media&#x26;token=fbb06a06-3480-47fb-8d7f-8cd0db7eeb19" alt=""><figcaption><p>Example of an Agent performing a transfer using the key assigned with <a data-mention href="../crypto.com-ai-agent-sdk/quick-start-guide-simulation-entry-point">quick-start-guide-simulation-entry-point</a></p></figcaption></figure>

Developers may also refer to the `experimental` branch of this [repository](https://github.com/lezzokafka/developer-platform-sdk-examples/tree/experimental/ai/cryptocom-ai-agent-service) for further reference.&#x20;
