Wednesday, March 06, 2024

Low code No Code drag drop Flowise AI

 Flowise AI is a low-code/no-code platform that utilizes a drag-and-drop interface to allow users to build applications powered by Large Language Models (LLMs). This means:

Low-code/no-code: Requires minimal to no coding knowledge, making it accessible to a wider range of users.
Drag-and-drop: Users can visually build their applications by dragging and dropping pre-built components onto a canvas.
Large Language Models (LLMs): Flowise leverages the power of LLMs, like ChatGPT, to enable functionalities like chatbots, text summarization, and data analysis.
Here’s a breakdown of Flowise AI’s key aspects:

Purpose: Simplifies LLM development, making it accessible to non-programmers.
Functionality: Drag-and-drop interface for building LLM applications.
Applications: Chatbots, Q&A systems, data analysis tools, and more.
Benefits: Democratizes LLM technology, reduces development time, and empowers non-technical users.
Overall, Flowise AI aims to bridge the gap between complex LLM technology and user-friendly application development.

In our last blogitem we had used langchain, Open AI LLM and Astra DB to upload the pdf file and chat with PDF file using Springboot application.

https://siddharathadhumale.blogspot.com/2024/02/rag-using-langchain4j-and-aster-vector.html

Now lets say if same can be done without any code ðŸ™‚
Yes you heard it right.. here comes no code AI tool Flowise.

Lets start the same

Step 1:- Down load the Apache 2 license source code of Flowise from below git hub

https://github.com/FlowiseAI/Flowise

Flowise has 3 different modules in a single mono repository.

server: Node backend to serve API logics

ui: React frontend

components: Integrations components

There are three ways to run the code

1- Quick Start

1- Install Flowise
npm install -g flowise
2- Start Flowise
3- npx flowise start
Open http://localhost:3000

2- Using Docker

Docker Compose
1- Go to docker folder at the root of the project
2- Copy the .env.example file and paste it as another file named .env
3- docker-compose up -d
4- Open http://localhost:3000
5- You can bring the containers down by docker-compose stop

Docker Image

1- Build the image locally:
docker build –no-cache -t flowise .

2- Run image:
docker run -d –name flowise -p 3000:3000 flowise

3- Stop image:
docker stop flowise

For Developer

1- Install Yarn
npm i -g yarn

Setup

1- Clone the repository
git clone https://github.com/FlowiseAI/Flowise.git

2- Go into repository folder
cd Flowise

3- Install all dependencies of all modules:
yarn install

4- Build all the code:
yarn build

5- Start the app:
yarn start

6- You can now access the app on http://localhost:3000

7- For development build: if you want to run in different port number.
Create .env file and specify the PORT (refer to .env.example) in packages/ui
Create .env file and specify the PORT (refer to .env.example) in packages/server
yarn dev

Once start the server on local you will get this first Flowise screen.

Now lets recreate below scenario with no code plateform

user uplaod the pdf file -> Convert the pdf file to vector and store in Astra DB -> user ask question -> related data will be found from Astra DB -> user question and data from astra DB will be send to Open AI LLM-> Finally we get reply from OpenAI LLM.

For that Flow wise has given ready made flows in market place as shown below

Now lets use conversational retrieval QA chain option

Now save this flow and change the txt component to pdf as shown below.

Also remove Pinecone componet with Astra

Now your flow will look like this.

Now lets add the required items

1- Open API key at the belwo location

2- Change the Astra component data as shown below

From where we will get Astra configuration data

1- Create a new DB

DB Name:- flowisedb
Name space:- flowise4chat
db id: c7c1d19b-4d10-4337-81f3-b92081d9c53e
Token Details :- AstraCS:DOMS56e95 API end point :- https://***.datastax.com

Now lets save our flow and upload a pdf file

and upload the pdf to Astra by clicking this icon on the screen.

Once the data is uploaded check it is uploaded on the Astra DB

Now lets execute our chat by cliking this icon

Tuesday, February 20, 2024

RAG using Langchain4j and Astra Vector Db for reading PDF file and generating AI response

 Before starting Retrieval Augmented Generation (RAG), we need to first understand.

1- What is AI,Machine Learning & Deep Learning etc. As this post is most on using RAG concept to read the PDF and generate answer from the user questions we will just try to bullet few of the points

  • 🧠 Understanding Artificial Intelligence and Its Subfields
  • Artificial intelligence (AI) is a broad field encompassing various subfields.
  • Machine learning is a subset of AI, focusing on training models to make predictions from data.
  • Deep learning, a subset of machine learning, utilizes artificial neural networks inspired by the human brain.
  • 🤖 Types and Differences of Machine Learning Models
  • Supervised learning utilizes labeled data for training, while unsupervised learning uses unlabeled data.
  • Supervised learning models make predictions based on labeled data points, while unsupervised learning identifies patterns in unlabeled data.
  • After making predictions, supervised learning models compare them to training data to improve accuracy.
  • 🧠 Understanding Deep Learning and Its Applications
  • Deep learning involves training models with artificial neural networks, enabling complex pattern recognition.
  • Semi-supervised learning allows models to learn from a combination of labeled and unlabeled data.
  • Deep learning models can be discriminative, focusing on classifying data points, or generative, creating new data based on learned patterns.
  • 🎨 Generative AI Model Types and Applications
  • Generative AI creates new samples such as text, images, video, or 3D assets based on learned patterns.
  • Different types of generative AI models include text-to-text, text-to-image, text-to-video, text-to-3D, and text-to-task models.
  • These models have diverse applications ranging from content creation to specific task automation.
  • 💬 Large Language Models (LLMs) and Fine-Tuning
  • LLMs are pre-trained with extensive data and then fine-tuned for specific tasks using domain-specific datasets.
  • Fine-tuning LLMs with domain-specific data enhances their performance in specialized areas such as healthcare, finance, and retail.
  • This approach enables smaller institutions to benefit from LLMs developed by larger companies, improving efficiency and accuracy.

Now lets try to understand what is Retrieval Augmented Generation (RAG) and LangChain

  • Retrieval Augmented Generation (RAG) merges generative and retrieval models to produce contextually relevant content.
  • RAG combines the strengths of generative models like GPT with the precision of retrieval systems.
  • LangChain facilitates interactions with various large language models (LLMs) and enables the creation of Chains to bridge multiple LLMs.
  • Vector databases store and process data in vector format, allowing efficient storage, retrieval, and manipulation of vast datasets.
  • RAG, LangChain, and Vector Databases work together to interpret user queries, retrieve relevant information, and generate coherent responses.
  • Implementing question answering in LangChain involves loading data, splitting documents, and utilizing embeddings and vector databases.
  • Hallucinations in Generative AI applications, such as RAG, can be mitigated through targeted queries, rich context incorporation, confidence scores, and human validation loops.
  • Evaluating RAG applications involves component-wise and end-to-end evaluations to ensure system quality and effectiveness.
  • Ethical considerations, including privacy, bias, fairness, accountability, and transparency, are crucial in the development and deployment of advanced information technologies like RAG, LangChain, and Vector Databases.
  • The collaboration between RAG, LangChain, and Vector Databases enhances content generation, enables more advanced virtual assistants and chatbots, and ensures technology serves as an augmentation of human capabilities.

Lets try to understand our usecase now

In our usecase user upload the PDF files from which he/she expect the generative answer. we upload the data in Vector Database Astra from casssandra and we are using Open AI LLM.

So lets first cretat OpenAI API key.

Then create a Vectore Database using Astra and note down the DB namd. Name Space , DB id and Token ID. We will need this in our code.

Astra:-
DB name :-langchain4j-siddhu
Name Space:- langchain4jsiddhu
db id: ad1e59c Token Details :- AstraCS:MZXz33a7bbb

Now lets create a maven project and following dependencies in it

1- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.1</version>
  </parent>
  <groupId>com.siddhu</groupId>
  <artifactId>langchain4j-demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>langchain4j-demo</name>
  <description>This Springboot application shows the use of langchain4j</description>
  <properties>
        <java.version>17.0.5</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j</artifactId>
            <version>0.25.0</version>
        </dependency>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-open-ai</artifactId>
            <version>0.25.0</version>
        </dependency>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId>
            <version>0.25.0</version>
        </dependency>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-document-parser-apache-pdfbox</artifactId>
            <version>0.25.0</version>
        </dependency>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-cassandra</artifactId>
            <version>0.25.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2- SiddhuPdfAssistantApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.siddhu;
 
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.parser.apache.pdfbox.ApachePdfBoxDocumentParser;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
 
import static dev.langchain4j.data.document.loader.FileSystemDocumentLoader.loadDocument;
 
@SpringBootApplication
public class SiddhuPdfAssistantApplication {
    private final EmbeddingStoreIngestor embeddingStoreIngestor;
 
    public SiddhuPdfAssistantApplication(EmbeddingStoreIngestor embeddingStoreIngestor) {
        this.embeddingStoreIngestor = embeddingStoreIngestor;
    }
 
    @PostConstruct
    public void init() {
        Document document = loadDocument(toPath("cassandra.pdf"), new ApachePdfBoxDocumentParser());
        embeddingStoreIngestor.ingest(document);
    }
 
    private static Path toPath(String fileName) {
        try {
            URL fileUrl = SiddhuPdfAssistantApplication.class.getClassLoader().getResource(fileName);
            return Paths.get(fileUrl.toURI());
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static void main(String[] args) {
        SpringApplication.run(SiddhuPdfAssistantApplication.class, args);
    }
 
}

3- SiddhuChatController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.siddhu;
 
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import dev.langchain4j.chain.ConversationalRetrievalChain;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.retriever.EmbeddingStoreRetriever;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.cassandra.AstraDbEmbeddingConfiguration;
import dev.langchain4j.store.embedding.cassandra.AstraDbEmbeddingStore;
import lombok.RequiredArgsConstructor;
 
@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
 
public class SiddhuChatController {
 
    //private final ConversationalRetrievalChain conversationalRetrievalChain;
    private static Logger LOGGER = LoggerFactory.getLogger(SiddhuChatController.class);
     
  
    public EmbeddingModel embeddingModel() {
        LOGGER.debug("Returning AllMiniLmL6V2EmbeddingModel Instance");
        return new AllMiniLmL6V2EmbeddingModel();
    }
 
 
    public AstraDbEmbeddingStore astraDbEmbeddingStore() {
        String astraToken = "<your-astradb-token>";
        String databaseId = "<your-database-id>";
        LOGGER.debug("Returning AstraDbEmbeddingStore");
        return new AstraDbEmbeddingStore(AstraDbEmbeddingConfiguration
                .builder()
                .token(astraToken)
                .databaseId(databaseId)
                .databaseRegion("us-east1")
                .keyspace("langchain4jsiddhu")
                .table("pdfchat")
                .dimension(384)
                .build());
    }
 
   
    public EmbeddingStoreIngestor embeddingStoreIngestor() {
        LOGGER.debug("Returning EmbeddingStoreIngestor");
        return EmbeddingStoreIngestor.builder()
                .documentSplitter(DocumentSplitters.recursive(300, 0))
                .embeddingModel(embeddingModel())
                .embeddingStore(astraDbEmbeddingStore())
                .build();
    }
 
   
    public ConversationalRetrievalChain conversationalRetrievalChain() {
        return ConversationalRetrievalChain.builder()
                .chatLanguageModel(OpenAiChatModel.withApiKey("your-open-api-key"))
                .retriever(EmbeddingStoreRetriever.from(astraDbEmbeddingStore(), embeddingModel()))
                .build();
    }
  
 
    @PostMapping
    public String chatWithPdf(@RequestBody String text) {
        var answer = this.conversationalRetrievalChain().execute(text);
        LOGGER.debug("Answer provided by the Langchain4j using Astra as Vector DB is - {}", answer);
        return answer;
    }
}

Finally put our pdf in resource folder as show below

now lets start the application and once started hit the below url using POST man tool

Note:- You can download the source code from belwo location
Git Location :- https://github.com/shdhumale/langchain4j-demo.git