Skip to content

Commit

Permalink
fix title sentence case for /building llm system
Browse files Browse the repository at this point in the history
  • Loading branch information
zlatanpham committed Nov 21, 2024
1 parent c254903 commit 9e00507
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 417 deletions.
215 changes: 108 additions & 107 deletions AI/Building LLM system/evaluation-guideline-for-LLM-application.md

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions AI/Building LLM system/graphrag.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tags:
- llm
- rag
title: GraphRAG - Building a knowledge graph for RAG system
description: "In baseline Retrieval Augmented Generation (RAG), sometimes the result might not be accurate as expected since the query itself have multiple layers of reasoning or the answer requires traversing disparate pieces of information through their shared attributes in order to provide new synthesized insights. In this post, we will explore a new approach called GraphRAG which combines the strengths of knowledge graphs and large language models to improve the accuracy of RAG systems"
description: 'In baseline Retrieval Augmented Generation (RAG), sometimes the result might not be accurate as expected since the query itself have multiple layers of reasoning or the answer requires traversing disparate pieces of information through their shared attributes in order to provide new synthesized insights. In this post, we will explore a new approach called GraphRAG which combines the strengths of knowledge graphs and large language models to improve the accuracy of RAG systems'
date: 2024-11-01
authors:
- hoangnnh
Expand All @@ -13,9 +13,10 @@ In baseline Retrieval Augmented Generation (RAG), sometimes the result might not

## What is Knowledge Graph?

A knowledge graph is an organized representation of real-world entities and their relationships. It is typically stored in a graph database, which natively stores the relationships between data entities. Entities in a knowledge graph can represent objects, events, situations, or concepts. Knowledge graphs contain 2 key chracteristics:
- **Nodes**: Represent entities such as people, places, organizations, events, or concepts,... Each node can have properties or attributes that describe it. For example, A node with type Person might have properties like name, age, and occupation.
- **Edges**: Represent the relationships or connections between entities. Edges can have types and properties as well. For example, an edge with type FRIEND_OF might have a property called "since", indicating when the friendship began.
A knowledge graph is an organized representation of real-world entities and their relationships. It is typically stored in a graph database, which natively stores the relationships between data entities. Entities in a knowledge graph can represent objects, events, situations, or concepts. Knowledge graphs contain 2 key chracteristics:

- **Nodes**: Represent entities such as people, places, organizations, events, or concepts,... Each node can have properties or attributes that describe it. For example, A node with type Person might have properties like name, age, and occupation.
- **Edges**: Represent the relationships or connections between entities. Edges can have types and properties as well. For example, an edge with type FRIEND_OF might have a property called "since", indicating when the friendship began.

![Knowledge Graph](assets/graphrag-knowledge-graph.webp)

Expand All @@ -27,47 +28,47 @@ Naive RAG systems built with keyword or similarity search-based retrieval fail i

![GraphRAG Workflow](assets/graphrag-workflow.webp)

GraphRAG workflow contain 2 main stage: Index and Query.
GraphRAG workflow contain 2 main stage: Index and Query.

### Index

Indexing in GraphRAG is data pipeline and transformation suite that is designed to extract meaningful, structured data from unstructured text using LLMs. Following above diagram, Index stage contain 6 main steps:
- **Compose TextUnits**: TextUnit is a chunk of text that is used for our graph extraction techniques. In this step, we will split the raw text into TextUnits.
- **Graph Extraction**: In this step, we will use LLM to extract entities and relationships from TextUnits.
![Graph Extraction](assets/graphrag-graph-extraction.webp)
Entity will have name, type, description propeties. Relationship will have source, target, descrption properties. Each entity and relationship will have a short summary description.

| Entity Example | Relationship Example |
|----------------|----------------------|
- **Compose TextUnits**: TextUnit is a chunk of text that is used for our graph extraction techniques. In this step, we will split the raw text into TextUnits.
- **Graph Extraction**: In this step, we will use LLM to extract entities and relationships from TextUnits. ![Graph Extraction](assets/graphrag-graph-extraction.webp) Entity will have name, type, description propeties. Relationship will have source, target, descrption properties. Each entity and relationship will have a short summary description.

| Entity Example | Relationship Example |
| ---------------------------------------------- | ----------------------------------------------------------- |
| ![Entity Example](assets/graphrag-entity.webp) | ![Relationship Example](assets/graphrag-relationships.webp) |

- **Graph Augmentation**: In this step, we generate a hierarchy of entity communities using the [Hierarchical Leiden Algorithm](https://en.wikipedia.org/wiki/Leiden_algorithm). The purpose to group nodes into comunity is represent closely-related groups of information that can be summarized independently.
- **Graph Augmentation**: In this step, we generate a hierarchy of entity communities using the [Hierarchical Leiden Algorithm](https://en.wikipedia.org/wiki/Leiden_algorithm). The purpose to group nodes into comunity is represent closely-related groups of information that can be summarized independently.

- **Community Summarization**: At this point, we have a functional graph of entities and relationships, a hierarchy of communities for the entities. We use LLM to summarize each community. These summaries are independently useful in their own right as a way to understand the global structure and semantics of the dataset, and may themselves be used to make sense of a corpus in the absence of a question
- **Community Summarization**: At this point, we have a functional graph of entities and relationships, a hierarchy of communities for the entities. We use LLM to summarize each community. These summaries are independently useful in their own right as a way to understand the global structure and semantics of the dataset, and may themselves be used to make sense of a corpus in the absence of a question

![GraphRAG Community](assets/graphrag-community.webp)


### Query

Query stage is the process of answering a question using the graph and the summaries of the communities. The query has 2 mode: Local Query and Global Query.

- **Local Query**: Local query method generates answers by combining relevant data from the AI-extracted knowledge-graph with text chunks of the raw documents. It is well-suited for answering questions that require an understanding of specific entities mentioned in the input documents. For example: "Who is Ebenezer Scroog".
- **Local Query**: Local query method generates answers by combining relevant data from the AI-extracted knowledge-graph with text chunks of the raw documents. It is well-suited for answering questions that require an understanding of specific entities mentioned in the input documents. For example: "Who is Ebenezer Scroog".

![GraphRAG Local Query](assets/graphrag-local-query.webp)

Following above diagrams, the user query will be extracted entities. Then, these entities will be semantic-searched though knowledge graph to find relevant informations. Then it flow to some filter and sorting steps to get the final answer.

- **Global Query**: Global query method generates answers by searching over all AI-generated community reports in a map-reduce fashion. It is well-suited for reasoning about holistic questions related to the whole data corpus by leveraging the community summaries. For example: "Who is the most famous author in the corpus?".
- **Global Query**: Global query method generates answers by searching over all AI-generated community reports in a map-reduce fashion. It is well-suited for reasoning about holistic questions related to the whole data corpus by leveraging the community summaries. For example: "Who is the most famous author in the corpus?".

![GraphRAG Global Query](assets/graphrag-global-query.webp)

In this mode, the collections of communiites will be used to generate response to user query in a map-reduce manner. At the Map step, community reports are segmented into text chunks of pre-defined size. Each text chunk is then used to produce an intermediate response containing a list of point, each of which is accompanied by a numerical rating indicating the importance of the point. And in Reduce step, the intermediate responses will be filtered and re-ranking and then aggregrated to produce the final answer.

## Conclusion

GraphRAG is ideal for tackling complex tasks such as multi-hop reasoning and answering comprehensive questions that require linking disparate pieces of information. However, using a lot of LLM calls in both index and query stage make it expensive and should be in consideration.

## References

- https://arxiv.org/abs/2404.16130
- https://microsoft.github.io/graphrag/
- https://medium.com/@zilliz_learn/graphrag-explained-enhancing-rag-with-knowledge-graphs-3312065f99e1
- https://medium.com/@zilliz_learn/graphrag-explained-enhancing-rag-with-knowledge-graphs-3312065f99e1
35 changes: 18 additions & 17 deletions AI/Building LLM system/guardrails-in-llm.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
tags:
- llm
title: Guardrails in LLM
title: Guardrails in llm
description: "Inspite of having strength to process and produce highly coherent human-like, behavior of LLM is unpredictable, so the need of a safety mechanisms and boundaries that control and direct an AI model's behavior to ensure it operates safely, ethically, and within intended parameters is crucial..."
date: 2024-10-24
authors:
Expand All @@ -14,17 +14,17 @@ Inspite of having strength to process and produce highly coherent human-like, be

Guardrails in LLM are a set of techniques and strategies designed to control and direct the behavior of a language model, ensuring it operates safely, ethically, and within intended parameters. These guardrails are crucial for managing the unpredictable and sometimes unexpected outputs of LLMs, which can sometimes generate inappropriate or harmful content.

## Types of guardrails

## Types of Guardrails
![Guardrails in LLM](./assets/guardrails-in-llm.webp)

1. **Input Guardrails**: This involves pre-processing the input to the model to remove or modify any potentially harmful or inappropriate content. This can include filtering out profanity, hate speech, or sensitive information. Some common usecases:
- **Topical Guardrails**: Limit the model's responses to a specific topic or domain to prevent it from generating off-topic or irrelevant content.
1. **Input guardrails**: This involves pre-processing the input to the model to remove or modify any potentially harmful or inappropriate content. This can include filtering out profanity, hate speech, or sensitive information. Some common usecases:
- **Topical guardrails**: Limit the model's responses to a specific topic or domain to prevent it from generating off-topic or irrelevant content.
- **Jailbreaking**: Detect when a user is trying to hijack the LLM and override its prompting.
- **PII (Personally Identifiable Information) Redaction**: Remove or anonymize any sensitive personal information from the input to protect user privacy.
- **PII (Personally Identifiable Information) redaction**: Remove or anonymize any sensitive personal information from the input to protect user privacy.

```python
## Example of Topical Guardrails
## Example of topical guardrails
validate_prompt="""
Your task is to evaluate questions and determine if they comply with the allowed topics: technology only. Respond with:
- 'allowed' if the question is about technology
Expand All @@ -43,13 +43,13 @@ Guardrails in LLM are a set of techniques and strategies designed to control and
return llm(question)
```

2. **Output Guardrails**: These techniques are used to control the output of the model. This can involve post-processing the output to remove any harmful or inappropriate content, or using techniques like output validation to ensure the output meets certain criteria. These can take many forms, with some of the most common being:
2. **Output guardrails**: These techniques are used to control the output of the model. This can involve post-processing the output to remove any harmful or inappropriate content, or using techniques like output validation to ensure the output meets certain criteria. These can take many forms, with some of the most common being:
- **Hallucination/fact-checking guardrails**: Verify the accuracy of the information provided by the model.
- **Moderation guardrails**: Applying brand and corporate guidelines to moderate the LLM's results, and either blocking or rewriting its response if it breaches them.
- **Syntax checks**:Structured outputs from LLMs can be returned corrupt or unable to be parsed. This is a common control to apply with function calling.

```python
## Example of Moderation Guardrails
## Example of moderation guardrails

domain = "technology"

Expand Down Expand Up @@ -96,23 +96,24 @@ Guardrails in LLM are a set of techniques and strategies designed to control and
if llm(moderation_prompt) > 3:
response = llm(f"Rewrite the following response to not recommend specific technologies: {response}")
return response


```

## Trade-offs

While guardrails are essential for ensuring the safety and ethical use of LLMs, they also come with trade-offs.
- Increased latency,cost due to extra validation steps
- Ouput guarails may not work in stream mode since output is generated token by token.
- Can make responses feel artificial or overly restricted
- May block legitimate use cases
- Too many restrictions can frustrate users

- Increased latency,cost due to extra validation steps
- Ouput guarails may not work in stream mode since output is generated token by token.
- Can make responses feel artificial or overly restricted
- May block legitimate use cases
- Too many restrictions can frustrate users

## Conclusion
Apply Guardrails into LLM pipeline is a should-have strategy to ensure the safety, ethical, and intended use of LLMs. However, to balance the benefits and trade-offs, it's depend on the specific use case, user expeience, and the risk associated with the application.

Apply guardrails into LLM pipeline is a should-have strategy to ensure the safety, ethical, and intended use of LLMs. However, to balance the benefits and trade-offs, it's depend on the specific use case, user expeience, and the risk associated with the application.

## References

- https://www.ml6.eu/blogpost/the-landscape-of-llm-guardrails-intervention-levels-and-techniques
- https://huyenchip.com/2024/07/25/genai-platform.html#query_rewriting
- https://cookbook.openai.com/examples/how_to_use_guardrails
- https://cookbook.openai.com/examples/how_to_use_guardrails
33 changes: 17 additions & 16 deletions AI/Building LLM system/intent-classification-by-LLM.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
---
authors:
- "hoangnnh"
date: "2024-10-09"
description: "User intent classification is a crucial aspect of conversational AI, start with machine learning models, but now advanced language models (LLMs) are being explored for this task. Unlike the old methods which is need to labeled datasets exhaustively, LLMs can understand what users mean without all that preparation. This memo explores the application of LLMs in intent classification, highlighting their potential to streamline the process and overcome traditional NLU limitations."
- 'hoangnnh'
date: '2024-10-09'
description: 'User intent classification is a crucial aspect of conversational AI, start with machine learning models, but now advanced language models (LLMs) are being explored for this task. Unlike the old methods which is need to labeled datasets exhaustively, LLMs can understand what users mean without all that preparation. This memo explores the application of LLMs in intent classification, highlighting their potential to streamline the process and overcome traditional NLU limitations.'
hashnode_meta:
id: "670f4d434d1924a802ea8935"
slug: "intent-classification-by-llm"
sync: "hashnode"
id: '670f4d434d1924a802ea8935'
slug: 'intent-classification-by-llm'
sync: 'hashnode'
tags:
- "llm"
- "intent-classification"
- "prompting"
title: "Intent Classification by LLM"
- 'llm'
- 'intent-classification'
- 'prompting'
title: 'Intent classification by LLM'
---

User intent classification is a crucial aspect of conversational AI, start with machine learning models, but now advanced language models (LLMs) are being explored for this task. Unlike the old methods which is need to labeled datasets exhaustively, LLMs can understand what users mean without all that preparation. This memo explores the application of LLMs in intent classification, highlighting their potential to streamline the process and overcome traditional NLU limitations.

## Introduction

Intent Classification is the process of determining the purpose or goal behind a user's input in a conversational AI system. There are many methods to capture it, it can be human involving, machine learning. With LLM, we take adavantage of its ability to understand context and nuance, allowing it to accurately classify user intents without the need for extensive labeled data.
Intent classification is the process of determining the purpose or goal behind a user's input in a conversational AI system. There are many methods to capture it, it can be human involving, machine learning. With LLM, we take advantage of its ability to understand context and nuance, allowing it to accurately classify user intents without the need for extensive labeled data.

## Example

We have an chatbot agent for an e-commerce platform. We will use LLM to classify user intent and based on that, the agent flow will be different.
We have a chatbot agent for an e-commerce platform. We will use LLM to classify user intent and based on that, the agent flow will be different.

```python
prompt= """
Expand Down Expand Up @@ -109,18 +110,18 @@ Intent: CustomerSupport

Besides the above tips, there are some limitations to consider when using LLMs for intent classification:

**Handling Multiple Intents**: It is easy to understand right? Too many lalbel will make the variation of output increase. It can make model confuse when making decision.
**Handling multiple intents**: It is easy to understand right? Too many label will make the variation of output increase. It can make model confuse when making decision.

**Hallucination**: The common problem of any LLM model, hallucination can lead to incorrect intent classifications.

**Lack of Explainability**: Sometime, without CoT applied, the underlying decision-making process of LLMs is still largely a black box.
**Lack of explainability**: Sometime, without CoT applied, the underlying decision-making process of LLMs is still largely a black box.

## Conclusion

Intent classification is a crucial step in building a conversational AI system. Taking adavantage of LLM power, we can easy extract user intent, It support a lot in workflow of a LLM applications.
Intent classification is a crucial step in building a conversational AI system. Taking advantage of LLM power, we can easy extract user intent, It support a lot in workflow of a LLM applications.

## References

- https://www.vellum.ai/blog/how-to-build-intent-detection-for-your-chatbot
- https://www.linkedin.com/pulse/leveraging-large-language-models-intent-bassel-mokabel-wj1vc/
- https://docs.voiceflow.com/docs/llm-intent-classification-method
- https://docs.voiceflow.com/docs/llm-intent-classification-method
Loading

0 comments on commit 9e00507

Please sign in to comment.