Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,97 +14,6 @@ from langchain.prompts.chat import (
|
|
| 14 |
)
|
| 15 |
import chainlit as cl
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 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 |
-
|
| 35 |
-
messages = [
|
| 36 |
-
SystemMessagePromptTemplate.from_template(system_template),
|
| 37 |
-
HumanMessagePromptTemplate.from_template("{question}"),
|
| 38 |
-
]
|
| 39 |
-
prompt = ChatPromptTemplate(messages=messages)
|
| 40 |
-
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
|
| 48 |
-
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(
|
| 65 |
-
ChatOpenAI(model="gpt-4", temperature=0, streaming=True),
|
| 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!"
|
| 73 |
-
await msg.send()
|
| 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")
|
| 81 |
-
cb = cl.AsyncLangchainCallbackHandler(
|
| 82 |
-
stream_final_answer=False, answer_prefix_tokens=["FINAL", "ANSWER"]
|
| 83 |
-
)
|
| 84 |
-
cb.answer_reached = True
|
| 85 |
-
res = await chain.acall(message, callbacks=[cb], )
|
| 86 |
-
|
| 87 |
-
answer = res["result"]
|
| 88 |
-
source_elements = []
|
| 89 |
-
visited_sources = set()
|
| 90 |
-
|
| 91 |
-
# Get the documents from the user session
|
| 92 |
-
docs = res["source_documents"]
|
| 93 |
-
metadatas = [doc.metadata for doc in docs]
|
| 94 |
-
all_sources = [m["source"] for m in metadatas]
|
| 95 |
-
|
| 96 |
-
for source in all_sources:
|
| 97 |
-
if source in visited_sources:
|
| 98 |
-
continue
|
| 99 |
-
visited_sources.add(source)
|
| 100 |
-
# Create the text element referenced in the message
|
| 101 |
-
source_elements.append(
|
| 102 |
-
cl.Text(content="https://www.imdb.com" + source, name="Review URL")
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
if source_elements:
|
| 106 |
-
answer += f"\nSources: {', '.join([e.content.decode('utf-8') for e in source_elements])}"
|
| 107 |
-
else:
|
| 108 |
-
answer += "\nNo sources found"
|
| 109 |
-
|
| 110 |
-
await cl.Message(content=answer, elements=source_elements).send()
|
|
|
|
| 14 |
)
|
| 15 |
import chainlit as cl
|
| 16 |
|
| 17 |
+
import build_langchain_vector_store
|
| 18 |
|
| 19 |
+
build_langchain_vector_store.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|