Skip to content

Commit

Permalink
Adding the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
ajosh0504 committed Jul 12, 2024
1 parent 18564b7 commit 328bfe8
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/60-perform-semantic-search/_category_.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"label": "Perform Semantic Search on Your Data",
"position": 7,
"position": 6,
"link": {
"type": "generated-index",
"description": "Retrieve semantically relevant documents to the user query using vector search."
Expand Down
6 changes: 1 addition & 5 deletions docs/70-build-rag-app/1-concepts.mdx
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
63 changes: 0 additions & 63 deletions docs/70-build-rag-app/2-adding-memory.mdx

This file was deleted.

50 changes: 50 additions & 0 deletions docs/70-build-rag-app/2-build-rag-app.mdx
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>
41 changes: 41 additions & 0 deletions docs/70-build-rag-app/3-stream-responses.mdx
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>
2 changes: 1 addition & 1 deletion docs/70-build-rag-app/_category_.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"label": "Build the RAG Application",
"position": 8,
"position": 7,
"link": {
"type": "generated-index",
"description": "Build the RAG application."
Expand Down
3 changes: 3 additions & 0 deletions docs/80-add-memory/1-concepts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 📘 Tools, libraries, and concepts

TO-DO
103 changes: 103 additions & 0 deletions docs/80-add-memory/2-add-memory.mdx
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>
8 changes: 8 additions & 0 deletions docs/80-add-memory/_category_.json
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."
}
}
10 changes: 5 additions & 5 deletions docs/summary.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ sidebar_position: 100
# 🎯 Summary

Congratulations! Following this lab, you have successfully:
* learned what are AI agents
* learned when to use AI agents
* built a basic tool-calling agent
* built a ReAct agent
* built an agent with memory
* learned what is Retrieval Augmented Generation a.k.a. RAG
* learned when to use RAG
* learned how to perform semantic search against data in MongoDB
* built a RAG application
* added memory to your RAG application

Here are some resources that you might find helpful:
* [MongoDB Developer Center](https://mongodb.com/developer/?utm_campaign=devrel&utm_source=workshop&utm_medium=cta&utm_content=ai_agents_workshop&utm_term=apoorva_joshi)
Expand Down

0 comments on commit 328bfe8

Please sign in to comment.