Refreshed notebooks, particularly with new Week 1

This commit is contained in:
Edward Donner
2024-11-13 15:46:22 +00:00
parent 6ba1875cd3
commit 21c7a8155c
34 changed files with 2331 additions and 410 deletions

View File

@@ -199,11 +199,7 @@
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}]\n",
" for user_message, assistant_message in history:\n",
" messages.append({\"role\": \"user\", \"content\": user_message})\n",
" messages.append({\"role\": \"assistant\", \"content\": assistant_message})\n",
" \n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history\n",
" message = add_context(message)\n",
" messages.append({\"role\": \"user\", \"content\": message})\n",
"\n",
@@ -232,7 +228,7 @@
"metadata": {},
"outputs": [],
"source": [
"view = gr.ChatInterface(chat).launch()"
"view = gr.ChatInterface(chat, type=\"messages\").launch()"
]
},
{

View File

@@ -101,6 +101,23 @@
" documents.append(doc)"
]
},
{
"cell_type": "markdown",
"id": "f065d4b1-80b7-4e15-abd4-60a83e752ea8",
"metadata": {},
"source": [
"# Please note:\n",
"\n",
"In the next cell, we split the text into chunks.\n",
"\n",
"2 students let me know that the next cell crashed their computer. \n",
"They were able to fix it by changing the chunk_size from 1,000 to 2,000 and the chunk_overlap from 200 to 400. \n",
"This shouldn't be required; but if it happens to you, please make that change! \n",
"(Note that LangChain may give a warning about a chunk being larger than 1,000 - this can be safely ignored).\n",
"\n",
"_With much thanks to Steven W and Nir P for this valuable contribution._"
]
},
{
"cell_type": "code",
"execution_count": null,

View File

@@ -313,7 +313,7 @@
"# the retriever is an abstraction over the VectorStore that will be used during RAG\n",
"retriever = vectorstore.as_retriever()\n",
"\n",
"# putting it together: set up the conversation chain with the GPT 3.5 LLM, the vector store and memory\n",
"# putting it together: set up the conversation chain with the GPT 4o-mini LLM, the vector store and memory\n",
"conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)"
]
},
@@ -329,6 +329,20 @@
"print(result[\"answer\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6eb99fb-33ec-4025-ab92-b634ede03647",
"metadata": {},
"outputs": [],
"source": [
"# set up a new conversation memory for the chat\n",
"memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)\n",
"\n",
"# putting it together: set up the conversation chain with the GPT 4o-mini LLM, the vector store and memory\n",
"conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)"
]
},
{
"cell_type": "markdown",
"id": "bbbcb659-13ce-47ab-8a5e-01b930494964",
@@ -346,7 +360,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Wrapping that in a function\n",
"# Wrapping in a function - note that history isn't used, as the memory is in the conversation_chain\n",
"\n",
"def chat(message, history):\n",
" result = conversation_chain.invoke({\"question\": message})\n",
@@ -362,7 +376,7 @@
"source": [
"# And in Gradio:\n",
"\n",
"view = gr.ChatInterface(chat).launch()"
"view = gr.ChatInterface(chat, type=\"messages\").launch(inbrowser=True)"
]
},
{

View File

@@ -313,6 +313,20 @@
"print(result[\"answer\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b5a9013-d5d4-4e25-9e7c-cdbb4f33e319",
"metadata": {},
"outputs": [],
"source": [
"# set up a new conversation memory for the chat\n",
"memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)\n",
"\n",
"# putting it together: set up the conversation chain with the GPT 4o-mini LLM, the vector store and memory\n",
"conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)"
]
},
{
"cell_type": "markdown",
"id": "bbbcb659-13ce-47ab-8a5e-01b930494964",
@@ -346,7 +360,7 @@
"source": [
"# And in Gradio:\n",
"\n",
"view = gr.ChatInterface(chat).launch()"
"view = gr.ChatInterface(chat, type=\"messages\").launch(inbrowser=True)"
]
},
{