bstraehle commited on
Commit
a4da0c1
1 Parent(s): 5614ed2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -19
app.py CHANGED
@@ -16,16 +16,16 @@ _ = load_dotenv(find_dotenv())
16
 
17
  #openai.api_key = os.environ["OPENAI_API_KEY"]
18
 
19
- template = """Answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer as concise as
20
- possible. Always say "🚀 Thanks for using the app - Bernd Straehle." at the end of the answer.
21
- Question: {question} Helpful Answer: """
22
 
23
- rag_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up
24
- an answer. Keep the answer as concise as possible. Always say "🔥 Thanks for using the app - Bernd Straehle." at the end of the answer.
25
- {context} Question: {question} Helpful Answer: """
26
 
27
- CHAIN_PROMPT = PromptTemplate(input_variables = ["question"], template = template)
28
- RAG_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"], template = rag_template)
 
 
29
 
30
  CHROMA_DIR = "docs/chroma"
31
  YOUTUBE_DIR = "docs/youtube"
@@ -35,21 +35,31 @@ YOUTUBE_URL = "https://www.youtube.com/watch?v=--khbXchTeE"
35
  MODEL_NAME = "gpt-4"
36
 
37
  def invoke(openai_api_key, use_rag, prompt):
38
- llm = ChatOpenAI(model_name = MODEL_NAME, openai_api_key = openai_api_key, temperature = 0)
 
 
39
  if (use_rag):
40
  if (os.path.isdir(CHROMA_DIR)):
41
- vector_db = Chroma(persist_directory = CHROMA_DIR, embedding_function = OpenAIEmbeddings())
 
42
  else:
43
- loader = GenericLoader(YoutubeAudioLoader([YOUTUBE_URL], YOUTUBE_DIR), OpenAIWhisperParser())
 
44
  docs = loader.load()
45
- text_splitter = RecursiveCharacterTextSplitter(chunk_size = 1500, chunk_overlap = 150)
 
46
  splits = text_splitter.split_documents(docs)
47
- vector_db = Chroma.from_documents(documents = splits, embedding = OpenAIEmbeddings(), persist_directory = CHROMA_DIR)
48
- rag_chain = RetrievalQA.from_chain_type(llm, retriever = vector_db.as_retriever(search_kwargs = {"k": 3}), return_source_documents = True, chain_type_kwargs = {"prompt": RAG_CHAIN_PROMPT})
 
 
 
 
 
49
  result = rag_chain({"query": prompt})
50
  result = result["result"]
51
  else:
52
- chain = LLMChain(llm = llm, prompt = CHAIN_PROMPT)
53
  result = chain.run({"question": prompt})
54
  return result
55
 
@@ -59,8 +69,8 @@ description = """<strong>Overview:</strong> The app demonstrates how to use a La
59
  <strong>Instructions:</strong> Enter an OpenAI API key and perform LLM use cases on a <a href='https://www.youtube.com/watch?v=--khbXchTeE'>short video about GPT-4</a>
60
  (semantic search, sentiment analysis, summarization, translation, etc.)
61
  <ul style="list-style-type:square;">
62
- <li>Set "Use RAG" to "False" and submit prompt "what is gpt-4". The LLM <strong>without</strong> RAG does not know the answer.</li>
63
- <li>Set "Use RAG" to "True" and submit prompt "what is gpt-4". The LLM <strong>with</strong> RAG knows the answer.</li>
64
  <li>Experiment with different prompts, for example "what is gpt-4, answer in german" or "write a poem about gpt-4".</li>
65
  </ul>
66
  In a production system, processing external data would be done in a batch process. An idea for a production system would be to perform LLM use cases on the
@@ -72,8 +82,8 @@ description = """<strong>Overview:</strong> The app demonstrates how to use a La
72
 
73
  gr.close_all()
74
  demo = gr.Interface(fn=invoke,
75
- inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1), gr.Radio([True, False], label="Use RAG", value = False), gr.Textbox(label = "Prompt", value = "what is gpt-4", lines = 1)],
76
  outputs = [gr.Textbox(label = "Completion", lines = 1)],
77
  title = "Generative AI - LLM & RAG",
78
  description = description)
79
- demo.queue().launch()
 
16
 
17
  #openai.api_key = os.environ["OPENAI_API_KEY"]
18
 
19
+ template = """If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer as concise as possible. Always say
20
+ "🔥 Thanks for using the app - Bernd Straehle." at the end of the answer. """
 
21
 
22
+ llm_template = "Answer the question at the end. " + template + "Question: {question} Helpful Answer: "
23
+ rag_template = "Use the following pieces of context to answer the question at the end. " + template + "{context} Question: {question} Helpful Answer: "
 
24
 
25
+ LLM_CHAIN_PROMPT = PromptTemplate(input_variables = ["question"],
26
+ template = llm_template)
27
+ RAG_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"],
28
+ template = rag_template)
29
 
30
  CHROMA_DIR = "docs/chroma"
31
  YOUTUBE_DIR = "docs/youtube"
 
35
  MODEL_NAME = "gpt-4"
36
 
37
  def invoke(openai_api_key, use_rag, prompt):
38
+ llm = ChatOpenAI(model_name = MODEL_NAME,
39
+ openai_api_key = openai_api_key,
40
+ temperature = 0)
41
  if (use_rag):
42
  if (os.path.isdir(CHROMA_DIR)):
43
+ vector_db = Chroma(embedding_function = OpenAIEmbeddings(),
44
+ persist_directory = CHROMA_DIR)
45
  else:
46
+ loader = GenericLoader(YoutubeAudioLoader([YOUTUBE_URL], YOUTUBE_DIR),
47
+ OpenAIWhisperParser())
48
  docs = loader.load()
49
+ text_splitter = RecursiveCharacterTextSplitter(chunk_overlap = 150,
50
+ chunk_size = 1500)
51
  splits = text_splitter.split_documents(docs)
52
+ vector_db = Chroma.from_documents(documents = splits,
53
+ embedding = OpenAIEmbeddings(),
54
+ persist_directory = CHROMA_DIR)
55
+ rag_chain = RetrievalQA.from_chain_type(llm,
56
+ chain_type_kwargs = {"prompt": RAG_CHAIN_PROMPT},
57
+ retriever = vector_db.as_retriever(search_kwargs = {"k": 3}),
58
+ return_source_documents = True)
59
  result = rag_chain({"query": prompt})
60
  result = result["result"]
61
  else:
62
+ chain = LLMChain(llm = llm, prompt = LLM_CHAIN_PROMPT)
63
  result = chain.run({"question": prompt})
64
  return result
65
 
 
69
  <strong>Instructions:</strong> Enter an OpenAI API key and perform LLM use cases on a <a href='https://www.youtube.com/watch?v=--khbXchTeE'>short video about GPT-4</a>
70
  (semantic search, sentiment analysis, summarization, translation, etc.)
71
  <ul style="list-style-type:square;">
72
+ <li>Set "Retrieval Augmented Generation" to "False" and submit prompt "what is gpt-4". The LLM <strong>without</strong> RAG does not know the answer.</li>
73
+ <li>Set "Retrieval Augmented Generation" to "True" and submit prompt "what is gpt-4". The LLM <strong>with</strong> RAG knows the answer.</li>
74
  <li>Experiment with different prompts, for example "what is gpt-4, answer in german" or "write a poem about gpt-4".</li>
75
  </ul>
76
  In a production system, processing external data would be done in a batch process. An idea for a production system would be to perform LLM use cases on the
 
82
 
83
  gr.close_all()
84
  demo = gr.Interface(fn=invoke,
85
+ inputs = [gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1), gr.Radio([True, False], label="Retrieval Augmented Generation", value = False), gr.Textbox(label = "Prompt", value = "what is gpt-4", lines = 1)],
86
  outputs = [gr.Textbox(label = "Completion", lines = 1)],
87
  title = "Generative AI - LLM & RAG",
88
  description = description)
89
+ demo.launch()