Skip to content

Commit

Permalink
Merge pull request #103 from elsantorini/es
Browse files Browse the repository at this point in the history
  • Loading branch information
denniszielke authored May 3, 2024
2 parents f3d784a + fb04045 commit e7d0ac8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
32 changes: 22 additions & 10 deletions labs/03-orchestration/03-VectorStore/mongo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We will now load the values from the `.env` file in the root of the repository and intantiate the mongo and openAI clients"
"We will now load the values from the `.env` file in the root of the repository and instantiate the mongo and openAI clients."
]
},
{
Expand Down Expand Up @@ -224,8 +224,8 @@
"source": [
"## Stream, vectorize & store\n",
"\n",
"In this lab we'll use a subset of MovieLens dataset of 5000 movies. \n",
"We will stream the data out of blob storage, generate vectors on the overview of the json document using the function above, then store it in Azure Cosmos DB for MongoDB collection. "
"In this lab we'll use a subset of the MovieLens dataset. \n",
"We will stream the data out of blob storage, generate vectors on the *overview* of the json document using the function above, then store it in Azure Cosmos DB for MongoDB collection. "
]
},
{
Expand All @@ -246,7 +246,7 @@
" #generate embeddings\n",
" vectorArray = generate_embeddings(object['overview'])\n",
"\n",
" #add the document to the collection\n",
" #add a vectorArray field to the document that will contain the embeddings \n",
" object[cosmos_vector_property] = vectorArray\n",
"\n",
" #insert the document into the collection\n",
Expand All @@ -266,7 +266,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Configure Vector Search w/ LangChain"
"## Configure Vector Search w/ LangChain"
]
},
{
Expand All @@ -291,7 +291,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setup RAG and Semantic Caching with your LLM"
"## Setup RAG and Semantic Caching with your LLM"
]
},
{
Expand Down Expand Up @@ -330,11 +330,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Then let's prepare the chain. The `prepare_chain` function is responsible for setting up a retrieval chain. The function starts by creating a `retriever` object using the vectorstore.as_retriever method. This retriever is configured to use a similarity search with a score threshold of 0.2 and to return the top 5 most similar results (k=5).\n",
"In this section, we'll implement the RAG pattern using LangChain. In LangChain, a retriever is used to augment the prompt with contextual data. In this case, the already established vector store will be used as the `retriever`. This retriever is configured to use a similarity search with a score threshold of 0.2 and to return the top 5 most similar results (k=5).\n",
"\n",
"Next we have the `ConversationalRetrievalChain` object that is responsible for managing the retrieval of responsed in a conversational context. It is configured with the previously created `retriever` and is set to return the source documents of the retrieved responses. The `combine_docs_chain_kwargs` parameter is set to final prompt of the `ConversationalRetrievalChain`. We add the verbose flag to return the final prompt and see the retrieved documents that will be used for the LLM. \n",
"Next we have the `ConversationalRetrievalChain` object that is responsible for managing the retrieval of responses in a conversational context. It is configured with the previously created `retriever` and is set to return the source documents of the retrieved responses. The `combine_docs_chain_kwargs` parameter is set to final prompt of the `ConversationalRetrievalChain`. We add the verbose flag to return the final prompt and see the retrieved documents that will be used for the LLM. \n",
"\n",
"The next section is to set up the semantic cache. There we need a similarity threshold of 0.99 to match the question asked. "
"The last part of the chain is to set up the semantic cache. There we need a similarity threshold of 0.99 to match the question asked. "
]
},
{
Expand Down Expand Up @@ -393,7 +393,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test the chatbot with a question:"
"Let's test the chatbot with a question. This first time, the questions is not yet in cache, so it should take longer as it will send it to the LLM."
]
},
{
Expand All @@ -402,8 +402,10 @@
"metadata": {},
"outputs": [],
"source": [
"%%time \n",
"query = \"Tell me about films with Buzz Lightyear\"\n",
"response = chain.invoke({\"question\": query, 'chat_history': [] })\n",
"print(\"***********************************************************\")\n",
"print (response['answer'])\n"
]
},
Expand All @@ -420,11 +422,20 @@
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"query = \"Tell me something about films with Buzz Lightyear\"\n",
"response = chain.invoke({\"question\": query, 'chat_history': [] })\n",
"print(\"***********************************************************\")\n",
"print (response['answer'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now notice that in order to answer the question below it will use the context from the documents the chain retrieves from the vector store. "
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -433,6 +444,7 @@
"source": [
"query = \"Whose spaceship crashed on a desert planet\"\n",
"response = chain.invoke({\"question\": query, 'chat_history': [] })\n",
"print(\"***********************************************************\")\n",
"print (response['answer'])"
]
},
Expand Down
14 changes: 8 additions & 6 deletions labs/03-orchestration/06-Conversations/chat-conversation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"metadata": {},
"source": [
"# Working with memory \n",
"When implementing a long runnng chat or a chat that goes over multiple sessions you might want to persist the messages of a conversation in an external history store. This will give you the ability to load previous conversations in the model chat prompt to provide context, trim old messages to reduce the amount of distracting information and leverage summaries to keep really long running conversations on point.\n",
"When implementing a long running chat or a chat that goes over multiple sessions you might want to persist the messages of a conversation in an external history store. This will give you the ability to load previous conversations in the model chat prompt to provide context, trim old messages to reduce the amount of distracting information and leverage summaries to keep really long running conversations on point.\n",
"\n",
"Overview: <br>\n",
"We will be using Azure CosmosDB (with the Mongo API) to persist chat, use langchain to store, load and trim conversation flows."
"We will be using Azure CosmosDB for MongoDB (vCore) to persist chat, use langchain to store, load and trim conversation flows."
]
},
{
Expand Down Expand Up @@ -122,7 +122,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now go into the Data Explorer of CosmosDB to check the documents that have been created.\n",
"Now go into the Data Explorer of CosmosDB or Mongo Compass to check the documents that have been created.\n",
"There should be several documents each for a single message that will be connected by the SessionId:\n",
"\n",
"```json\n",
Expand Down Expand Up @@ -154,7 +154,7 @@
"outputs": [],
"source": [
"from langchain_openai import AzureChatOpenAI\n",
"\n",
"import os\n",
"chat = AzureChatOpenAI(\n",
" azure_deployment = os.getenv(\"AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME\")\n",
")\n",
Expand All @@ -178,11 +178,13 @@
"\n",
"chat_message_history.add_user_message(follow_up_question)\n",
"\n",
"chain.invoke(\n",
"response = chain.invoke(\n",
" {\n",
" \"messages\": chat_message_history.messages,\n",
" }\n",
")"
")\n",
"\n",
"print(response)\n"
]
},
{
Expand Down
10 changes: 7 additions & 3 deletions labs/03-orchestration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ In this lab, we'll walk through the basics of Tokens, how they relate to LLMs, a

In this lab, we'll walk through the basics of creating Vectors using Embeddings and why they are important when it comes to AI Orchestration.

## 03-Vectorstore
## 03-VectorStore

[Qdrant](03-Qdrant/qdrant.ipynb)
[Qdrant](https://github.com/Azure/intro-to-intelligent-apps/blob/main/labs/03-orchestration/03-VectorStore/qdrant.ipynb )

In this lab, we'll walk through using an open source vector store called Qdrant.

[Mongo](https://github.com/Azure/intro-to-intelligent-apps/blob/main/labs/03-orchestration/03-VectorStore/mongo.ipynb)

In this lab, we'll use Azure CosmosDB for MongoDB (vCore) as a vector store and as a semantic cache.

## 04-AI Search

[Azure AI Search + Semantic Kernel C#](04-ACS/acs-sk-csharp.ipynb)
Expand All @@ -40,4 +44,4 @@ In this lab, we'll walk through integrating external APIs into your chain.

[Conversations](06-Conversations/chat-conversation.ipynb)

In this lab, we'll walk through persisting your chat history in **Azure CosmosDB**.
In this lab, we'll walk through persisting your chat history in **Azure Cosmos DB for MongoDB (vCore)**.

0 comments on commit e7d0ac8

Please sign in to comment.