🐢Dify | Install

For the Japanese version, click [here](https://qiita.com/vb-long/items/97d8ab51ff6c44a85cd4)

## Dify
[Dify](https://github.com/langgenius/dify) is an open-source GUI platform designed to build AGI apppication. This time, I use Dify to quickly prototype and test before development in [Langchain](https://github.com/langchain-ai/langchain).

## Local installation
For an easy installation, simply use the default settings of Dify.
### Requirement
Ensure your machine meets these minimum system requirements and has [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) are installed:
- CPU >= 2 Core
- RAM >= 4GB

### Step 1: Clone Dify
Clone the Dify repository to your machine:
```powershell
git clone https://github.com/langgenius/dify.git
```
Acttualy you only need the [dify/docker](https://github.com/langgenius/dify/tree/main/docker) from repository

### Step 2. Start Dify
Navigate to the docker directory and run following command to start Dify:
```powershell
cd dify/docker
docker compose up -d
```

Results:
```powershell
[+] Running 11/11
 ✔ Network docker_default             Created    0.0s
 ✔ Network docker_ssrf_proxy_network  Created    0.0s
 ✔ Container docker-ssrf_proxy-1      Started    1.7s
 ✔ Container docker-db-1              Started    1.6s
 ✔ Container docker-weaviate-1        Started    1.6s
 ✔ Container docker-redis-1           Started    1.7s
 ✔ Container docker-sandbox-1         Started    1.7s
 ✔ Container docker-web-1             Started    1.7s
 ✔ Container docker-api-1             Started    2.3s
 ✔ Container docker-worker-1          Started    2.2s
 ✔ Container docker-nginx-1           Started    2.8s
 ```
 
 ### Step 3. Verify container status:
 Run the following command to check if all containers are running successfully:
 ```powershell
docker compose ps
 ```

Results:
 ```powershell
NAME                  IMAGE                              COMMAND                   SERVICE      CREATED         STATUS                   PORTS
docker-api-1          langgenius/dify-api:0.6.10         "/bin/bash /entrypoi…"   api          5 minutes ago   Up 5 minutes             5001/tcp
docker-db-1           postgres:15-alpine                 "docker-entrypoint.s…"   db           5 minutes ago   Up 5 minutes (healthy)   5432/tcp
docker-nginx-1        nginx:latest                       "/docker-entrypoint.…"   nginx        5 minutes ago   Up 5 minutes             0.0.0.0:80->80/tcp
docker-redis-1        redis:6-alpine                     "docker-entrypoint.s…"   redis        5 minutes ago   Up 5 minutes (healthy)   6379/tcp
docker-sandbox-1      langgenius/dify-sandbox:0.2.1      "/main"                   sandbox      5 minutes ago   Up 5 minutes
docker-ssrf_proxy-1   ubuntu/squid:latest                "entrypoint.sh -f /e…"   ssrf_proxy   5 minutes ago   Up 5 minutes             3128/tcp
docker-weaviate-1     semitechnologies/weaviate:1.19.0   "/bin/weaviate --hos…"   weaviate     5 minutes ago   Up 5 minutes
docker-web-1          langgenius/dify-web:0.6.10         "/bin/sh ./entrypoin…"   web          5 minutes ago   Up 5 minutes             3000/tcp
docker-worker-1       langgenius/dify-api:0.6.10         "/bin/bash /entrypoi…"   worker       5 minutes ago   Up 5 minutes             5001/tcp
```
## Access Dify
Acesss [http://localhost/install](http://localhost/install) to use Dify.

Since I have used Dify before, it displays the login screen. If this is your first run, you will need to create an account.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/74571120-ef95-801c-cf85-d18f2d9978c3.png)

Your workspace will look like this:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/ee7fc8a8-92ba-cb54-186e-d9fbd9422d16.png)

## Setup the model
Click on top right corner, select `Settings > Model Provider`
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/50a44ec4-7433-3365-5053-a901e0238a1e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/2145ecd7-f935-0144-f9ec-f2515ee9026f.png)

### Using [Ollama](https://www.ollama.com/)
1. Visit [https://www.ollama.com/](https://www.ollama.com/) and download the Ollama client for your system
2. Run Ollama (I will use Llama2, but you can choose any model that suit your needs, visit [Olama models](https://ollama.com/library) for more details)
```powershell
ollama run llama2
```
3. Verify the launch at `http://localhost:11434`
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/cc8eb386-e43b-44f3-15ad-5b28d818bd05.png)

4. Integrate Ollama in Dify
    - Scroll down and choose Ollama
    ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/c43d4e3d-5331-d987-6234-3adaa65c73ba.png)
    - Fill in:
        - Model Name: llama2
        - Base URL: `http://<your-ollama-endpoint-domain>:11434`
        I run Dify from docker, so I will set the base URL to `http://host.docker.internal:11434`
        Enter the base URL where the Ollama service is accessible.
        - Model Type: Chat
        - Model Context Length: 4096
        The maximum context length of the model. If unsure, use the default value of 4096.
        - Maximum Token Limit: 4096
        The maximum number of tokens returned by the model. If there are no specific requirements for the model, this can be consistent with the model context length.
        - Support for Vision: Yes
        Check this option if the model supports image understanding (multimodal), like llava.
    ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/9e4d8a5a-fc31-bfab-5511-07e0d0d10e12.png)

### Using [OpenAI](https://openai.com/index/openai-api/)
I am curently using OpenAI create prototypes and test.

Choose OpenAi and fill in, just the API Key is enough
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/4aacbfd6-bc3e-f03b-221d-8328c7cc7cdf.png)

And you're all set
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/b6f83131-bc9f-8619-c1d9-75dd604125cf.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/1390d0b1-c254-9084-cd20-712bcc8b2480.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3809913/8e653d98-9f59-4006-b432-2f8d197013bb.png)