From 328bfe8e8c8ce630e19b8a04cc065c4c09525efa Mon Sep 17 00:00:00 2001 From: ajosh0504 Date: Fri, 12 Jul 2024 14:13:31 -0700 Subject: [PATCH] Adding the rest --- .../_category_.json | 2 +- docs/70-build-rag-app/1-concepts.mdx | 6 +- docs/70-build-rag-app/2-adding-memory.mdx | 63 ----------- docs/70-build-rag-app/2-build-rag-app.mdx | 50 +++++++++ docs/70-build-rag-app/3-stream-responses.mdx | 41 +++++++ docs/70-build-rag-app/_category_.json | 2 +- docs/80-add-memory/1-concepts.mdx | 3 + docs/80-add-memory/2-add-memory.mdx | 103 ++++++++++++++++++ docs/80-add-memory/_category_.json | 8 ++ docs/summary.mdx | 10 +- 10 files changed, 213 insertions(+), 75 deletions(-) delete mode 100644 docs/70-build-rag-app/2-adding-memory.mdx create mode 100644 docs/70-build-rag-app/2-build-rag-app.mdx create mode 100644 docs/70-build-rag-app/3-stream-responses.mdx create mode 100644 docs/80-add-memory/1-concepts.mdx create mode 100644 docs/80-add-memory/2-add-memory.mdx create mode 100644 docs/80-add-memory/_category_.json diff --git a/docs/60-perform-semantic-search/_category_.json b/docs/60-perform-semantic-search/_category_.json index 7dac56de..29c2313d 100644 --- a/docs/60-perform-semantic-search/_category_.json +++ b/docs/60-perform-semantic-search/_category_.json @@ -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." diff --git a/docs/70-build-rag-app/1-concepts.mdx b/docs/70-build-rag-app/1-concepts.mdx index 0b9ad88b..ff75346e 100644 --- a/docs/70-build-rag-app/1-concepts.mdx +++ b/docs/70-build-rag-app/1-concepts.mdx @@ -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. \ No newline at end of file +TO-DO \ No newline at end of file diff --git a/docs/70-build-rag-app/2-adding-memory.mdx b/docs/70-build-rag-app/2-adding-memory.mdx deleted file mode 100644 index 45d709c3..00000000 --- a/docs/70-build-rag-app/2-adding-memory.mdx +++ /dev/null @@ -1,63 +0,0 @@ -# ๐Ÿ‘ Add memory to agents using MongoDB - -The final step in this lab is to add conversational message history as a form of memory for the agent. - -Message history in this case will be stored in and retrieved from a MongoDB collection. - -Fill in any `` placeholders and run the cells under the **Step 9: Add memory to agents using MongoDB** section in the notebook to add memory to the basic tool calling agent we created previously. - -The answers for code blocks in this section are as follows: - -**CODE_BLOCK_22** - -
-Answer -
-```python -return MongoDBChatMessageHistory( - MONGODB_URI, session_id, database_name=DB_NAME, collection_name="history" -) -``` -
-
- -**CODE_BLOCK_23** - -
-Answer -
-```python -MessagesPlaceholder("chat_history") -``` -
-
- -**CODE_BLOCK_24** - -
-Answer -
-```python -RunnableWithMessageHistory( - agent_executor, - get_message_history, - input_messages_key="input", - history_messages_key="chat_history", -) -``` -
-
- -**CODE_BLOCK_25** - -
-Answer -
-```python -agent_with_chat_history.invoke( - {"input": "What is the title of the first paper you found?"}, - config={"configurable": {"session_id": "my-session"}}, -) -``` -
-
diff --git a/docs/70-build-rag-app/2-build-rag-app.mdx b/docs/70-build-rag-app/2-build-rag-app.mdx new file mode 100644 index 00000000..1de61684 --- /dev/null +++ b/docs/70-build-rag-app/2-build-rag-app.mdx @@ -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 `` 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** + +
+Answer +
+```python +vector_search(user_query) +``` +
+
+ +**CODE_BLOCK_21** + +
+Answer +
+```python +"\n\n".join([d.get("page_content", "") for d in context]) +``` +
+
+ +**CODE_BLOCK_22** + +
+Answer +
+```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) +``` +
+
\ No newline at end of file diff --git a/docs/70-build-rag-app/3-stream-responses.mdx b/docs/70-build-rag-app/3-stream-responses.mdx new file mode 100644 index 00000000..b761818e --- /dev/null +++ b/docs/70-build-rag-app/3-stream-responses.mdx @@ -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 `` 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** + +
+Answer +
+```python +fw_client.chat.completions.create( + model=model, + temperature=0, + stream=True, + messages=[ + { + "role": "user", + "content": create_prompt(user_query), + } + ], +) +``` +
+
+ +**CODE_BLOCK_24** + +
+Answer +
+```python +for chunk in response: + if chunk.choices[0].delta.content: + print(chunk.choices[0].delta.content, end="") +``` +
+
\ No newline at end of file diff --git a/docs/70-build-rag-app/_category_.json b/docs/70-build-rag-app/_category_.json index 831a91cf..b35ee912 100644 --- a/docs/70-build-rag-app/_category_.json +++ b/docs/70-build-rag-app/_category_.json @@ -1,6 +1,6 @@ { "label": "Build the RAG Application", - "position": 8, + "position": 7, "link": { "type": "generated-index", "description": "Build the RAG application." diff --git a/docs/80-add-memory/1-concepts.mdx b/docs/80-add-memory/1-concepts.mdx new file mode 100644 index 00000000..ff75346e --- /dev/null +++ b/docs/80-add-memory/1-concepts.mdx @@ -0,0 +1,3 @@ +# ๐Ÿ“˜ Tools, libraries, and concepts + +TO-DO \ No newline at end of file diff --git a/docs/80-add-memory/2-add-memory.mdx b/docs/80-add-memory/2-add-memory.mdx new file mode 100644 index 00000000..5db2f63a --- /dev/null +++ b/docs/80-add-memory/2-add-memory.mdx @@ -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 `` 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** + +
+Answer +
+```python +history_collection.create_index("session_id") +``` +
+
+ +**CODE_BLOCK_26** + +
+Answer +
+```python +{ + "session_id": session_id, + "role": role, + "content": content, + "timestamp": datetime.now(), +} +``` +
+
+ +**CODE_BLOCK_27** + +
+Answer +
+```python +history_collection.insert_one(message) +``` +
+
+ +**CODE_BLOCK_28** + +
+Answer +
+```python +history_collection.find({"session_id": session_id}).sort("timestamp", 1) +``` +
+
+ +**CODE_BLOCK_29** + +
+Answer +
+```python +{"role": msg["role"], "content": msg["content"]} for msg in cursor +``` +
+
+ +**CODE_BLOCK_30** + +
+Answer +
+```python +message_history = retrieve_session_history(session_id) +messages += message_history +``` +
+
+ +**CODE_BLOCK_31** + +
+Answer +
+```python +user_message = {"role": "user", "content": user_query} +messages.append(user_message) +``` +
+
+ +**CODE_BLOCK_32** + +
+Answer +
+```python +store_chat_message(session_id, "user", user_query) +store_chat_message(session_id, "assistant", answer) +``` +
+
\ No newline at end of file diff --git a/docs/80-add-memory/_category_.json b/docs/80-add-memory/_category_.json new file mode 100644 index 00000000..88293ebf --- /dev/null +++ b/docs/80-add-memory/_category_.json @@ -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." + } +} \ No newline at end of file diff --git a/docs/summary.mdx b/docs/summary.mdx index 3faf57dc..b09410c1 100644 --- a/docs/summary.mdx +++ b/docs/summary.mdx @@ -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)