HanLee commited on
Commit
ce5d5d0
·
1 Parent(s): 87798fb

feat: 03_04

Browse files
Files changed (3) hide show
  1. README.md +7 -22
  2. app/app.py +13 -11
  3. app/prompt.py +48 -0
README.md CHANGED
@@ -2,32 +2,17 @@
2
  This is the repository for the LinkedIn Learning course `Hands-On AI: Building and Deploying LLM-Powered Apps`. The full course is available from [LinkedIn Learning][lil-course-url].
3
 
4
  _See the readme file in the main branch for updated instructions and information._
5
- ## Lab5: Putting it All Together
6
- In Lab 2, we created the basic scaffold of our Chat with PDF App. In Lab 3, we added PDF uploading and processing functionality. In Lab 4, we added the capability to indexing documents into a vector database. Now we have all the required pieces together, it's time for us to assemble our RAG (retrieval-augmented generation) system using Langchain.
7
 
 
8
 
9
- ## Exercises
10
-
11
- We will build on top of our existing chainlit app code in `app/app.py` in the `app` folder. As in our previous app, we added some template code and instructions in `app/app.py`
12
-
13
- 1. Please go through the exercises in `app/app.py`.
14
-
15
- 2. Please lanuch the application by running the following command on the Terminal:
16
 
17
- ```bash
18
- chainlit run app/app.py -w
19
- ```
20
-
21
- ## Solution
22
-
23
- Please see `app/app.py`.
24
-
25
- Alternatively, to launch the application, please run the following command on the Terminal:
26
 
27
- ```bash
28
- chainlit run app/app.py -w
29
- ```
30
 
31
  ## References
32
 
33
- - [Langchain RetrivalQA](https://python.langchain.com/docs/use_cases/web_scraping#research-automation)
 
2
  This is the repository for the LinkedIn Learning course `Hands-On AI: Building and Deploying LLM-Powered Apps`. The full course is available from [LinkedIn Learning][lil-course-url].
3
 
4
  _See the readme file in the main branch for updated instructions and information._
5
+ ## Lab6: Setup Prompting
6
+ Now our Chat with PDF application is up and running, but we run into a very slight problem: one of the key questions is not working despite that there's ample information in the PDF documents!!!
7
 
8
+ We can "fix" this is via prompt engineering. Prompt Engineering refers to methods for how to communicate with LLM to steer its behavior for desired outcomes without updating the model weights.
9
 
10
+ Before we can do that, we need to extract the prompt template out of the code.
 
 
 
 
 
 
11
 
12
+ ## Exercises
 
 
 
 
 
 
 
 
13
 
14
+ Please extract Langchain's prompt template out of the code base into an independent prompt.py in the app directory. Control (or Command for Mac) click will help you navigate this very quickly!
 
 
15
 
16
  ## References
17
 
18
+ - [Prompt Engineering vs Blind Prompting](https://mitchellh.com/writing/prompt-engineering-vs-blind-prompting)
app/app.py CHANGED
@@ -23,6 +23,13 @@ from langchain.vectorstores import Chroma
23
  from langchain.vectorstores.base import VectorStore
24
 
25
 
 
 
 
 
 
 
 
26
  def process_file(*, file: AskFileResponse) -> List[Document]:
27
  """Processes one PDF file from a Chainlit AskFileResponse object by first
28
  loading the PDF document and then chunk it into sub documents. Only
@@ -143,28 +150,23 @@ async def on_chat_start():
143
  msg.content = f"`{file.name}` loaded. You can now ask questions!"
144
  await msg.update()
145
 
146
- ##########################################################################
147
- # Exercise 1:
148
- # Now we have search engine setup, our Chat with PDF application can do
149
- # RAG architecture pattern. Please use the appropriate RetrievalQA Chain
150
- # from Langchain.
151
- #
152
- # Remember, we would want to set the model temperature to
153
- # 0 to ensure model outputs do not vary across runs, and we would want to
154
- # also return sources to our answers.
155
- ##########################################################################
156
  model = ChatOpenAI(
157
  model="gpt-3.5-turbo-16k-0613",
158
  temperature=0,
159
  streaming=True
160
  )
161
 
 
 
 
 
 
162
  chain = RetrievalQAWithSourcesChain.from_chain_type(
163
  llm=model,
164
  chain_type="stuff",
165
  retriever=search_engine.as_retriever(max_tokens_limit=4097),
 
166
  )
167
- ##########################################################################
168
 
169
  # We are saving the chain in user_session, so we do not have to rebuild
170
  # it every single time.
 
23
  from langchain.vectorstores.base import VectorStore
24
 
25
 
26
+ ##############################################################################
27
+ # Exercise 2:
28
+ # Please import the copied prompt scaffolds from prompt.py
29
+ ##############################################################################
30
+ from prompt import EXAMPLE_PROMPT, PROMPT
31
+
32
+
33
  def process_file(*, file: AskFileResponse) -> List[Document]:
34
  """Processes one PDF file from a Chainlit AskFileResponse object by first
35
  loading the PDF document and then chunk it into sub documents. Only
 
150
  msg.content = f"`{file.name}` loaded. You can now ask questions!"
151
  await msg.update()
152
 
 
 
 
 
 
 
 
 
 
 
153
  model = ChatOpenAI(
154
  model="gpt-3.5-turbo-16k-0613",
155
  temperature=0,
156
  streaming=True
157
  )
158
 
159
+ ##########################################################################
160
+ # Exercise 3:
161
+ # Please modify this chain's initiation with the proper kwargs to take in
162
+ # custom prompts.
163
+ ##########################################################################
164
  chain = RetrievalQAWithSourcesChain.from_chain_type(
165
  llm=model,
166
  chain_type="stuff",
167
  retriever=search_engine.as_retriever(max_tokens_limit=4097),
168
+ chain_type_kwargs={"prompt": PROMPT, "document_prompt": EXAMPLE_PROMPT},
169
  )
 
170
 
171
  # We are saving the chain in user_session, so we do not have to rebuild
172
  # it every single time.
app/prompt.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ##############################################################################
2
+ # Exercise 1:
3
+ # Please navigate and find the Langchain prompt templates used for
4
+ # RetrievalQAWithSources chain and how it is initialized.
5
+ ##############################################################################
6
+ from langchain.prompts import PromptTemplate
7
+
8
+ template = """Given the following extracted parts of a long document and a question, create a final answer with references ("SOURCES").
9
+ If you don't know the answer, just say that you don't know. Don't try to make up an answer.
10
+ ALWAYS return a "SOURCES" part in your answer.
11
+
12
+ QUESTION: Which state/country's law governs the interpretation of the contract?
13
+ =========
14
+ Content: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights.
15
+ Source: 28-pl
16
+ Content: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\n\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\n\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\n\n11.9 No Third-Party Beneficiaries.
17
+ Source: 30-pl
18
+ Content: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur,
19
+ Source: 4-pl
20
+ =========
21
+ FINAL ANSWER: This Agreement is governed by English law.
22
+ SOURCES: 28-pl
23
+
24
+ QUESTION: What did the president say about Michael Jackson?
25
+ =========
26
+ Content: Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \n\nGroups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.
27
+ Source: 0-pl
28
+ Content: And we won’t stop. \n\nWe have lost so much to COVID-19. Time with one another. And worst of all, so much loss of life. \n\nLet’s use this moment to reset. Let’s stop looking at COVID-19 as a partisan dividing line and see it for what it is: A God-awful disease. \n\nLet’s stop seeing each other as enemies, and start seeing each other for who we really are: Fellow Americans. \n\nWe can’t change how divided we’ve been. But we can change how we move forward—on COVID-19 and other issues we must face together. \n\nI recently visited the New York City Police Department days after the funerals of Officer Wilbert Mora and his partner, Officer Jason Rivera. \n\nThey were responding to a 9-1-1 call when a man shot and killed them with a stolen gun. \n\nOfficer Mora was 27 years old. \n\nOfficer Rivera was 22. \n\nBoth Dominican Americans who’d grown up on the same streets they later chose to patrol as police officers. \n\nI spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.
29
+ Source: 24-pl
30
+ Content: And a proud Ukrainian people, who have known 30 years of independence, have repeatedly shown that they will not tolerate anyone who tries to take their country backwards. \n\nTo all Americans, I will be honest with you, as I’ve always promised. A Russian dictator, invading a foreign country, has costs around the world. \n\nAnd I’m taking robust action to make sure the pain of our sanctions is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers. \n\nTonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world. \n\nAmerica will lead that effort, releasing 30 Million barrels from our own Strategic Petroleum Reserve. And we stand ready to do more if necessary, unified with our allies. \n\nThese steps will help blunt gas prices here at home. And I know the news about what’s happening can seem alarming. \n\nBut I want you to know that we are going to be okay.
31
+ Source: 5-pl
32
+ Content: More support for patients and families. \n\nTo get there, I call on Congress to fund ARPA-H, the Advanced Research Projects Agency for Health. \n\nIt’s based on DARPA—the Defense Department project that led to the Internet, GPS, and so much more. \n\nARPA-H will have a singular purpose—to drive breakthroughs in cancer, Alzheimer’s, diabetes, and more. \n\nA unity agenda for the nation. \n\nWe can do this. \n\nMy fellow Americans—tonight , we have gathered in a sacred space—the citadel of our democracy. \n\nIn this Capitol, generation after generation, Americans have debated great questions amid great strife, and have done great things. \n\nWe have fought for freedom, expanded liberty, defeated totalitarianism and terror. \n\nAnd built the strongest, freest, and most prosperous nation the world has ever known. \n\nNow is the hour. \n\nOur moment of responsibility. \n\nOur test of resolve and conscience, of history itself. \n\nIt is in this moment that our character is formed. Our purpose is found. Our future is forged. \n\nWell I know this nation.
33
+ Source: 34-pl
34
+ =========
35
+ FINAL ANSWER: The president did not mention Michael Jackson.
36
+ SOURCES:
37
+
38
+ QUESTION: {question}
39
+ =========
40
+ {summaries}
41
+ =========
42
+ FINAL ANSWER:"""
43
+ PROMPT = PromptTemplate(template=template, input_variables=["summaries", "question"])
44
+
45
+ EXAMPLE_PROMPT = PromptTemplate(
46
+ template="Content: {page_content}\nSource: {source}",
47
+ input_variables=["page_content", "source"],
48
+ )