-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ajosh0504
committed
Jul 12, 2024
1 parent
18564b7
commit 328bfe8
Showing
10 changed files
with
213 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
# 📘 Tools, libraries, and concepts | ||
|
||
Here is a quick overview of tools, libraries and concepts that you will come across in this section of the lab: | ||
|
||
## [RunnableWithMessageHistory](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.history.RunnableWithMessageHistory.html) | ||
|
||
Runnable that manages (reads, updates) chat message history for another Runnable. By default, it organizes chat history based on a session ID. | ||
TO-DO |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 👐 Build the RAG application | ||
|
||
Let's create a simple RAG application that takes in a user query, retrieves contextually relevant documents from MongoDB Atlas, and passes the query and retrieved context to the _Llama 3 8B Instruct_ model to generate an answer to the user question. | ||
|
||
Fill in any `<CODE_BLOCK_N>` placeholders and run the cells under the **Step 9: Build the RAG application** section in the notebook to build the RAG application. | ||
|
||
The answers for code blocks in this section are as follows: | ||
|
||
**CODE_BLOCK_20** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
vector_search(user_query) | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_21** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
"\n\n".join([d.get("page_content", "") for d in context]) | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_22** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
response = fw_client.chat.completions.create( | ||
model=model, | ||
temperature=0, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": create_prompt(user_query), | ||
} | ||
], | ||
) | ||
print(response.choices[0].message.content) | ||
``` | ||
</div> | ||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 🦹 Stream responses from the RAG application | ||
|
||
By default, generation results are return once the generation is completed. Another option is to stream the results, which is useful for chat use cases where the user can incrementally see results as each token is generated. | ||
|
||
Fill in any `<CODE_BLOCK_N>` placeholders and run the cells under the **🦹♀️ Return streaming responses** section in the notebook to stream the results from your RAG application. | ||
|
||
The answers for code blocks in this section are as follows: | ||
|
||
**CODE_BLOCK_23** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
fw_client.chat.completions.create( | ||
model=model, | ||
temperature=0, | ||
stream=True, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": create_prompt(user_query), | ||
} | ||
], | ||
) | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_24** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
for chunk in response: | ||
if chunk.choices[0].delta.content: | ||
print(chunk.choices[0].delta.content, end="") | ||
``` | ||
</div> | ||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 📘 Tools, libraries, and concepts | ||
|
||
TO-DO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# 👐 Add memory to the RAG application | ||
|
||
In many Q&A applications we want to allow the user to have a back-and-forth conversation with the LLM, meaning the application needs some sort of "memory" of past questions and answers, and some logic for incorporating those into its current thinking. In this section, you will retrieve chat message history from MongoDB and incorporate it in your RAG application. | ||
|
||
Fill in any `<CODE_BLOCK_N>` placeholders and run the cells under the **Step 10: Add memory to the RAG application** section in the notebook to add memory to the RAG application. | ||
|
||
The answers for code blocks in this section are as follows: | ||
|
||
**CODE_BLOCK_25** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
history_collection.create_index("session_id") | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_26** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
{ | ||
"session_id": session_id, | ||
"role": role, | ||
"content": content, | ||
"timestamp": datetime.now(), | ||
} | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_27** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
history_collection.insert_one(message) | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_28** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
history_collection.find({"session_id": session_id}).sort("timestamp", 1) | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_29** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
{"role": msg["role"], "content": msg["content"]} for msg in cursor | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_30** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
message_history = retrieve_session_history(session_id) | ||
messages += message_history | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_31** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
user_message = {"role": "user", "content": user_query} | ||
messages.append(user_message) | ||
``` | ||
</div> | ||
</details> | ||
|
||
**CODE_BLOCK_32** | ||
|
||
<details> | ||
<summary>Answer</summary> | ||
<div> | ||
```python | ||
store_chat_message(session_id, "user", user_query) | ||
store_chat_message(session_id, "assistant", answer) | ||
``` | ||
</div> | ||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"label": "Add memory to the RAG application", | ||
"position": 8, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "Retrieve chat message history from MongoDB and incorporate it into the RAG application." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters