Skip to content

Commit 8f9f691

Browse files
author
ICP Ninja
committed
ICP Ninja export
1 parent 4af6eef commit 8f9f691

18 files changed

Lines changed: 497 additions & 2 deletions

.devcontainer/devcontainer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "ICP Dev Environment",
3+
"image": "ghcr.io/dfinity/icp-dev-env-slim:22",
4+
"forwardPorts": [4943, 5173],
5+
"portsAttributes": {
6+
"4943": {
7+
"label": "dfx",
8+
"onAutoForward": "ignore"
9+
},
10+
"5173": {
11+
"label": "vite",
12+
"onAutoForward": "openBrowser"
13+
}
14+
},
15+
"customizations": {
16+
"vscode": {
17+
"extensions": ["dfinity-foundation.vscode-motoko"]
18+
}
19+
}
20+
}

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.dfx/
2+
**/.icp/cache/
3+
build/
4+
node_modules/
5+
package-lock.json
6+
Cargo.lock
7+
dist/
8+
.DS_Store
9+
_MACOSX
10+
.vscode/
11+
target/
12+
*.old.did
13+
.idea
14+
.mops
15+
.env
16+
**/frontend/src/bindings/

BUILD.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Continue building locally
2+
3+
Projects deployed through ICP Ninja are temporary; they will only be live for 20 minutes before they are removed. The command-line tool `dfx` can be used to continue building your ICP Ninja project locally and deploy it to the mainnet.
4+
5+
To migrate your ICP Ninja project off of the web browser and develop it locally, follow these steps.
6+
7+
### 1. Install developer tools.
8+
9+
You can install the developer tools natively or use Dev Containers.
10+
11+
#### Option 1: Natively install developer tools
12+
13+
> Installing `dfx` natively is currently only supported on macOS and Linux systems. On Windows, it is recommended to use the Dev Containers option.
14+
15+
1. Install `dfx` with the following command:
16+
17+
```
18+
19+
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
20+
21+
```
22+
23+
> On Apple Silicon (e.g., Apple M1 chip), make sure you have Rosetta installed (`softwareupdate --install-rosetta`).
24+
25+
2. [Install NodeJS](https://nodejs.org/en/download/package-manager).
26+
27+
3. For Rust projects, you will also need to:
28+
29+
- Install [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo): `curl https://sh.rustup.rs -sSf | sh`
30+
31+
- Install [candid-extractor](https://crates.io/crates/candid-extractor): `cargo install candid-extractor`
32+
33+
4. For Motoko projects, you will also need to:
34+
35+
- Install the Motoko package manager [Mops](https://docs.mops.one/quick-start#2-install-mops-cli): `npm i -g ic-mops`
36+
37+
Lastly, navigate into your project's directory that you downloaded from ICP Ninja.
38+
39+
#### Option 2: Dev Containers
40+
41+
Continue building your projects locally by installing the [Dev Container extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) for VS Code and [Docker](https://docs.docker.com/engine/install/).
42+
43+
Make sure Docker is running, then navigate into your project's directory that you downloaded from ICP Ninja and start the Dev Container by selecting `Dev-Containers: Reopen in Container` in VS Code's command palette (F1 or Ctrl/Cmd+Shift+P).
44+
45+
> Note that local development ports (e.g. the ports used by `dfx` or `vite`) are forwarded from the Dev Container to your local machine. In the VS code terminal, use Ctrl/Cmd+Click on the displayed local URLs to open them in your browser. To view the current port mappings, click the "Ports" tab in the VS Code terminal window.
46+
47+
### 2. Start the local development environment.
48+
49+
```
50+
dfx start --background
51+
```
52+
53+
### 3. Create a local developer identity.
54+
55+
To manage your project's canisters, it is recommended that you create a local [developer identity](https://internetcomputer.org/docs/building-apps/getting-started/identities) rather than use the `dfx` default identity that is not stored securely.
56+
57+
To create a new identity, run the commands:
58+
59+
```
60+
61+
dfx identity new IDENTITY_NAME
62+
63+
dfx identity use IDENTITY_NAME
64+
65+
```
66+
67+
Replace `IDENTITY_NAME` with your preferred identity name. The first command `dfx start --background` starts the local `dfx` processes, then `dfx identity new` will create a new identity and return your identity's seed phase. Be sure to save this in a safe, secure location.
68+
69+
The third command `dfx identity use` will tell `dfx` to use your new identity as the active identity. Any canister smart contracts created after running `dfx identity use` will be owned and controlled by the active identity.
70+
71+
Your identity will have a principal ID associated with it. Principal IDs are used to identify different entities on ICP, such as users and canisters.
72+
73+
[Learn more about ICP developer identities](https://internetcomputer.org/docs/building-apps/getting-started/identities).
74+
75+
### 4. Deploy the project locally.
76+
77+
Deploy your project to your local developer environment with:
78+
79+
```
80+
npm install
81+
dfx deploy
82+
83+
```
84+
85+
Your project will be hosted on your local machine. The local canister URLs for your project will be shown in the terminal window as output of the `dfx deploy` command. You can open these URLs in your web browser to view the local instance of your project.
86+
87+
### 5. Obtain cycles.
88+
89+
To deploy your project to the mainnet for long-term public accessibility, first you will need [cycles](https://internetcomputer.org/docs/building-apps/getting-started/tokens-and-cycles). Cycles are used to pay for the resources your project uses on the mainnet, such as storage and compute.
90+
91+
> This cost model is known as ICP's [reverse gas model](https://internetcomputer.org/docs/building-apps/essentials/gas-cost), where developers pay for their project's gas fees rather than users pay for their own gas fees. This model provides an enhanced end user experience since they do not need to hold tokens or sign transactions when using a dapp deployed on ICP.
92+
93+
> Learn how much a project may cost by using the [pricing calculator](https://internetcomputer.org/docs/building-apps/essentials/cost-estimations-and-examples).
94+
95+
Cycles can be obtained through [converting ICP tokens into cycles using `dfx`](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/dfx-cycles#dfx-cycles-convert).
96+
97+
### 6. Deploy to the mainnet.
98+
99+
Once you have cycles, run the command:
100+
101+
```
102+
103+
dfx deploy --network ic
104+
105+
```
106+
107+
After your project has been deployed to the mainnet, it will continuously require cycles to pay for the resources it uses. You will need to [top up](https://internetcomputer.org/docs/building-apps/canister-management/topping-up) your project's canisters or set up automatic cycles management through a service such as [CycleOps](https://cycleops.dev/).
108+
109+
> If your project's canisters run out of cycles, they will be removed from the network.
110+
111+
## Additional examples
112+
113+
Additional code examples and sample applications can be found in the [DFINITY examples repo](https://github.com/dfinity/examples).

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
# LLM-Chatbot
2-
LLM Chatbot - created on ICP Ninja
1+
# LLM Chatbot
2+
3+
The LLM Chatbot example demonstrates how an ICP smart contract can be used to interact with a large language model (LLM) to generate text. The user can input a prompt, and the smart contract will use the LLM to generate a response.
4+
The response is then returned to the user, and the user can submit some follow-up prompts to continue the conversation.
5+
6+
## Deploying from ICP Ninja
7+
8+
When viewing this project in ICP Ninja, you can deploy it directly to the mainnet for free by clicking "Run" in the upper right corner. Open this project in ICP Ninja:
9+
10+
[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/i?g=https://github.com/Lilmoki91/LLM-Chatbot)
11+
12+
## Build and deploy from the command-line
13+
14+
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
15+
16+
### 2. Setting up Ollama
17+
18+
To be able to test the agent locally, you'll need a server for processing the agent's prompts. For that, we'll use `ollama`, which is a tool that can download and serve LLMs.
19+
See the documentation on the [Ollama website](https://ollama.com/) to install it. Once it's installed, run:
20+
21+
```
22+
ollama serve
23+
# Expected to start listening on port 11434
24+
```
25+
26+
The above command will start the Ollama server, so that it can process requests by the agent. Additionally, and in a separate window, run the following command to download the LLM that will be used by the agent:
27+
28+
```
29+
ollama run llama3.1:8b
30+
```
31+
32+
The above command will download an 8B parameter model, which is around 4GiB. Once the command executes and the model is loaded, you can terminate it. You won't need to do this step again.
33+
34+
### 3. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/)
35+
36+
### 4. Navigate into the project's directory.
37+
38+
### 5. Deploy the project to your local environment:
39+
40+
```
41+
dfx start --background --clean && dfx deploy
42+
```
43+
44+
## Security considerations and best practices
45+
46+
If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices.

backend/app.mo

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import LLM "mo:llm";
2+
3+
persistent actor {
4+
public func prompt(prompt : Text) : async Text {
5+
await LLM.prompt(#Llama3_1_8B, prompt);
6+
};
7+
8+
public func chat(messages : [LLM.ChatMessage]) : async Text {
9+
let response = await LLM.chat(#Llama3_1_8B).withMessages(messages).send();
10+
11+
switch (response.message.content) {
12+
case (?text) text;
13+
case null "";
14+
};
15+
};
16+
};

dfx.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"canisters": {
3+
"backend": {
4+
"dependencies": ["llm"],
5+
"main": "backend/app.mo",
6+
"type": "motoko",
7+
"args": "--enhanced-orthogonal-persistence"
8+
},
9+
"frontend": {
10+
"dependencies": ["backend"],
11+
"frontend": {
12+
"entrypoint": "frontend/index.html"
13+
},
14+
"source": ["frontend/dist"],
15+
"type": "assets"
16+
},
17+
"llm": {
18+
"candid": "https://github.com/dfinity/llm/releases/latest/download/llm-canister-ollama.did",
19+
"type": "custom",
20+
"specified_id": "w36hm-eqaaa-aaaal-qr76a-cai",
21+
"remote": {
22+
"id": {
23+
"ic": "w36hm-eqaaa-aaaal-qr76a-cai"
24+
}
25+
},
26+
"wasm": "https://github.com/dfinity/llm/releases/latest/download/llm-canister-ollama.wasm"
27+
}
28+
},
29+
"output_env_file": ".env",
30+
"defaults": {
31+
"build": {
32+
"packtool": "mops sources"
33+
}
34+
}
35+
}

frontend/index.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

frontend/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>LLM Chatbot</title>
7+
<link rel="icon" href="/favicon.ico" />
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="src/main.jsx"></script>
12+
</body>
13+
</html>

frontend/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "frontend",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"prebuild": "npm i --include=dev && dfx generate backend",
7+
"build": "vite build",
8+
"dev": "vite"
9+
},
10+
"dependencies": {
11+
"@dfinity/agent": "2.4.1",
12+
"@dfinity/auth-client": "2.4.1",
13+
"@dfinity/candid": "2.4.1",
14+
"@dfinity/principal": "2.4.1",
15+
"react-json-view-lite": "2.3.0",
16+
"react": "18.3.1",
17+
"react-dom": "18.3.1"
18+
},
19+
"devDependencies": {
20+
"@types/react": "18.3.12",
21+
"@types/react-dom": "18.3.1",
22+
"@vitejs/plugin-react": "4.3.3",
23+
"autoprefixer": "^10.4.20",
24+
"postcss": "8.4.48",
25+
"tailwindcss": "3.4.14",
26+
"vite": "5.4.11",
27+
"vite-plugin-environment": "1.1.3"
28+
}
29+
}

frontend/postcss.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
plugins: {
3+
autoprefixer: {},
4+
tailwindcss: {}
5+
}
6+
};

0 commit comments

Comments
 (0)