You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[LangChain](https://www.langchain.com/) is an open-source framework designed to simplify the creation of applications that use large language models (LLMs). LangChain has a vibrant community of developers and contributors and is used by many companies and organizations. LangChain utilizes proven Prompt Engineering patterns and techniques to optimize LLMs, ensuring successful and accurate results through verified and tested best practices.
4
4
5
-
Part of the appeal of LangChain syntax is the capability of breaking down large complex interactions with LLMs into smaller, more manageable steps by composing a reusable [chain](https://python.langchain.com/docs/modules/chains/) process. LangChain provides a syntax for chains([LCEL](https://python.langchain.com/docs/modules/chains/#lcel)), the ability to integrate with external systems through [tools](https://python.langchain.com/docs/integrations/tools/), and end-to-end [agents](https://python.langchain.com/docs/modules/agents/) for common applications.
5
+
Part of the appeal of LangChain syntax is the capability of breaking down large complex interactions with LLMs into smaller, more manageable steps by composing a reusable chain process. LangChain provides a syntax for chains([LCEL](https://python.langchain.com/docs/concepts/#langchain-expression-language-lcel)), the ability to integrate with external systems through [tools](https://python.langchain.com/docs/concepts/#tools), and end-to-end [agents](https://python.langchain.com/docs/concepts/#agents) for common applications.
6
6
7
7
The concept of an agent is quite similar to that of a chain in LangChain but with one fundamental difference. A chain in LangChain is a hard-coded sequence of steps executed in a specific order. Conversely, an agent leverages the LLM to assess the incoming request with the current context to decide what steps or actions need to be executed and in what order.
8
8
9
-
LangChain agents can leverage tools and toolkits. A tool can be an integration into an external system, custom code, or even another chain. A toolkit is a collection of tools that can be used to solve a specific problem.
9
+
LangChain agents can leverage tools and toolkits. A tool can be an integration into an external system, custom code, a retriever, or even another chain. A toolkit is a collection of tools that can be used to solve a specific problem.
10
10
11
11
## LangChain RAG pattern
12
12
@@ -16,47 +16,117 @@ Earlier in this guide, the RAG (Retrieval Augmented Generation) pattern was intr
16
16
17
17
When an incoming message is received, the retriever will vectorize the message and perform a vector search to find the most relevant documents for the given query. The retriever returns a list of documents that are then used to augment the prompt. The augmented prompt is then passed to the LLM (generator) to reason over the prompt and context. The output from the LLM is then parsed and returned as the final message.
18
18
19
-
> **Note**: A vector store retriever is only one type of retriever that can be used in the RAG pattern. Learn more about retrievers in the [LangChain documentation](https://python.langchain.com/docs/modules/data_connection/retrievers/).
19
+
> **Note**: A vector store retriever is only one type of retriever that can be used in the RAG pattern. Learn more about retrievers in the [LangChain documentation](https://python.langchain.com/docs/concepts/#retrievers).
20
20
21
21
## Lab - Vector search and RAG using LangChain
22
22
23
23
In this lab uses LangChain to re-implement the RAG pattern introduced in the previous lab. Take note of the readability of the code and how easy it is to compose a reusable RAG chain using LangChain that queries the products vector index in Azure Cosmos DB for NoSQL. The lab concludes with the creation of an agent with various tools for the LLM to leverage to fulfill the incoming request.
24
24
25
-
This lab also requires the data provided in the previous lab titled [Load data into Azure Cosmos DB API for NoSQL containers](../08_Load_Data/README.md#lab---load-data-into-azure-cosmos-db-api-for-mongodb-collections) as well as the populated vector index created in the lab titled [Vector Search using Azure Cosmos DB for NoSQL](../09_Vector_Search_Cosmos_DB/README.md#lab---use-vector-search-on-embeddings-in-vcore-based-azure-cosmos-db-for-mongodb). Run all cells in both notebooks to prepare the data for use in this lab.
25
+
This lab also requires the data provided in the previous lab titled [Load data into Azure Cosmos DB for NoSQL containers](../08_Load_Data/README.md#lab---load-data-into-azure-cosmos-db-api-for-nosql-containers) as well as the populated vector index created in the lab titled [Vector Search using Azure Cosmos DB for NoSQL](../09_Vector_Search_Cosmos_DB/README.md#lab---use-vector-search-on-embeddings-in-azure-cosmos-db-for-nosql). Run all cells in both notebooks to prepare the data for use in this lab.
26
26
27
27
>**Note**: It is highly recommended to use a [virtual environment](https://python.land/virtual-environments/virtualenv) for all labs.
28
28
29
29
Please visit the lab repository to complete [this lab](../Labs/lab_4_langchain.ipynb).
30
30
31
31
Some highlights of the lab include:
32
32
33
-
### Instantiating a vector store reference
33
+
### Creating a custom LangChain retriever for Azure Cosmos DB for NoSQL
Tools are selected by the Large Language model at runtime. In this case, depending on the incoming user request the LLM will decide which container in the database to query. The following code shows how to create a tool for the LLM to use to query the products collection in the database.
71
141
72
142
```python
73
-
# create a chain on the retriever to format the documents as JSON
description="Searches Cosmic Works product information for similar products based on the question. Returns the product information in JSON format."
81
-
)
82
-
]
143
+
# Create a tool that will use the product vector search in Azure Cosmos DB for NoSQL
144
+
products_retriever_tool = create_retriever_tool(
145
+
retriever= products_retriever,
146
+
name="vector_search_products",
147
+
description="Searches Cosmic Works product information for similar products based on the question. Returns the product information in JSON format."
148
+
)
149
+
tools = [products_retriever_tool]
83
150
```
84
151
85
152
### Creating tools that call Python functions
86
153
87
154
Users may query for information that does not have a semantic meaning, such as an ID GUID value or a SKU number. Providing agents with tools to call Python functions to retrieve documents based on these fields is a common practice. The following is an example of adding tools that call out to Python functions for the products collection.
88
155
89
156
```python
90
-
db = pymongo.MongoClient(CONNECTION_STRING).cosmic_works
### Creating an agent armed with tools for vector search and Python functions calling
120
188
121
189
```python
122
-
system_message = SystemMessage(
123
-
content="""
190
+
agent_instructions ="""
124
191
You are a helpful, fun and friendly sales assistant for Cosmic Works, a bicycle and bicycle accessories store.
125
-
126
192
Your name is Cosmo.
127
-
128
193
You are designed to answer questions about the products that Cosmic Works sells, the customers that buy them, and the sales orders that are placed by customers.
129
-
130
-
If you don't know the answer to a question, respond with "I don't know."
131
-
194
+
If you don't know the answer to a question, respond with "I don't know."
132
195
Only answer questions related to Cosmic Works products, customers, and sales orders.
133
-
134
196
If a question is not related to Cosmic Works products, customers, or sales orders,
135
197
respond with "I only answer questions about Cosmic Works"
0 commit comments