Jason Caro commited on
Commit
c3769bb
·
1 Parent(s): 47c3676

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -22
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import chainlit as cl
2
  from langchain.embeddings.openai import OpenAIEmbeddings
3
- from langchain.document_loaders.csv_loader import CSVLoader
4
  from langchain.embeddings import CacheBackedEmbeddings
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain.vectorstores import FAISS
@@ -12,23 +11,11 @@ from langchain.prompts.chat import (
12
  SystemMessagePromptTemplate,
13
  HumanMessagePromptTemplate,
14
  )
15
- import chainlit as cl
16
 
17
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
18
 
19
  system_template = """
20
- Use the following pieces of context to answer the user's question.
21
- Please respond as if you were Ken from the movie Barbie. Ken is a well-meaning but naive character who loves to Beach. He talks like a typical Californian Beach Bro, but he doesn't use the word "Dude" so much.
22
- If you don't know the answer, just say that you don't know, don't try to make up an answer.
23
- You can make inferences based on the context as long as it still faithfully represents the feedback.
24
-
25
- Example of your response should be:
26
-
27
- ```
28
- The answer is foo
29
- ```
30
-
31
- Begin!
32
  ----------------
33
  {context}"""
34
 
@@ -41,7 +28,7 @@ chain_type_kwargs = {"prompt": prompt}
41
 
42
  @cl.author_rename
43
  def rename(orig_author: str):
44
- rename_dict = {"RetrievalQA": "Consulting The Kens"}
45
  return rename_dict.get(orig_author, orig_author)
46
 
47
  @cl.on_chat_start
@@ -49,16 +36,21 @@ async def init():
49
  msg = cl.Message(content=f"Building Index...")
50
  await msg.send()
51
 
52
- # build FAISS index from csv
53
- loader = CSVLoader(file_path="./data/barbie.csv", source_column="Review_Url")
54
- data = loader.load()
55
- documents = text_splitter.transform_documents(data)
 
 
 
 
56
  store = LocalFileStore("./cache/")
57
  core_embeddings_model = OpenAIEmbeddings()
58
  embedder = CacheBackedEmbeddings.from_bytes_store(
59
  core_embeddings_model, store, namespace=core_embeddings_model.model
60
  )
61
- # make async docsearch
 
62
  docsearch = await cl.make_async(FAISS.from_documents)(documents, embedder)
63
 
64
  chain = RetrievalQA.from_chain_type(
@@ -66,7 +58,7 @@ async def init():
66
  chain_type="stuff",
67
  return_source_documents=True,
68
  retriever=docsearch.as_retriever(),
69
- chain_type_kwargs = {"prompt": prompt}
70
  )
71
 
72
  msg.content = f"Index built!"
@@ -74,7 +66,6 @@ async def init():
74
 
75
  cl.user_session.set("chain", chain)
76
 
77
-
78
  @cl.on_message
79
  async def main(message):
80
  chain = cl.user_session.get("chain")
 
1
  import chainlit as cl
2
  from langchain.embeddings.openai import OpenAIEmbeddings
 
3
  from langchain.embeddings import CacheBackedEmbeddings
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
5
  from langchain.vectorstores import FAISS
 
11
  SystemMessagePromptTemplate,
12
  HumanMessagePromptTemplate,
13
  )
 
14
 
15
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
16
 
17
  system_template = """
18
+ Use the following pieces of context to answer the user's question. If the question cannot be answered with the supplied context, simply answer "I cannot determine this based on the provided context."
 
 
 
 
 
 
 
 
 
 
 
19
  ----------------
20
  {context}"""
21
 
 
28
 
29
  @cl.author_rename
30
  def rename(orig_author: str):
31
+ rename_dict = {"RetrievalQA": "Consulting PageTurn"}
32
  return rename_dict.get(orig_author, orig_author)
33
 
34
  @cl.on_chat_start
 
36
  msg = cl.Message(content=f"Building Index...")
37
  await msg.send()
38
 
39
+ # Read text from a .txt file
40
+ with open('./data/aerodynamic_drag.txt', 'r') as f:
41
+ aerodynamic_drag_data = f.read()
42
+
43
+ # Split the text into smaller chunks
44
+ documents = text_splitter.transform_documents([aerodynamic_drag_data])
45
+
46
+ # Create a local file store for caching
47
  store = LocalFileStore("./cache/")
48
  core_embeddings_model = OpenAIEmbeddings()
49
  embedder = CacheBackedEmbeddings.from_bytes_store(
50
  core_embeddings_model, store, namespace=core_embeddings_model.model
51
  )
52
+
53
+ # Make async docsearch
54
  docsearch = await cl.make_async(FAISS.from_documents)(documents, embedder)
55
 
56
  chain = RetrievalQA.from_chain_type(
 
58
  chain_type="stuff",
59
  return_source_documents=True,
60
  retriever=docsearch.as_retriever(),
61
+ chain_type_kwargs={"prompt": prompt}
62
  )
63
 
64
  msg.content = f"Index built!"
 
66
 
67
  cl.user_session.set("chain", chain)
68
 
 
69
  @cl.on_message
70
  async def main(message):
71
  chain = cl.user_session.get("chain")