Update app.py
Browse files
app.py
CHANGED
|
@@ -17,44 +17,13 @@ from langchain.agents import Tool, ZeroShotAgent, AgentExecutor
|
|
| 17 |
from langchain.agents.agent_toolkits import create_retriever_tool, create_conversational_retrieval_agent
|
| 18 |
from langchain import LLMChain
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
system_template = """
|
| 23 |
-
Use the following pieces of context to answer the user's question.
|
| 24 |
-
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.
|
| 25 |
-
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 26 |
-
You can make inferences based on the context as long as it still faithfully represents the feedback.
|
| 27 |
-
|
| 28 |
-
Example of your response should be:
|
| 29 |
-
|
| 30 |
-
```
|
| 31 |
-
The answer is foo
|
| 32 |
-
```
|
| 33 |
-
|
| 34 |
-
Begin!
|
| 35 |
-
----------------
|
| 36 |
-
{context}"""
|
| 37 |
-
|
| 38 |
-
messages = [
|
| 39 |
-
SystemMessagePromptTemplate.from_template(system_template),
|
| 40 |
-
HumanMessagePromptTemplate.from_template("{question}"),
|
| 41 |
-
]
|
| 42 |
-
prompt = ChatPromptTemplate(messages=messages)
|
| 43 |
-
chain_type_kwargs = {"prompt": prompt}
|
| 44 |
-
|
| 45 |
-
# @cl.author_rename
|
| 46 |
-
# def rename(orig_author: str):
|
| 47 |
-
# rename_dict = {"RetrievalQA": "Consulting The Kens"}
|
| 48 |
-
# return rename_dict.get(orig_author, orig_author)
|
| 49 |
|
| 50 |
@cl.on_chat_start
|
| 51 |
async def init():
|
| 52 |
msg = cl.Message(content=f"Building Index...")
|
| 53 |
await msg.send()
|
| 54 |
|
| 55 |
-
### start building retrievers, stores and agents
|
| 56 |
-
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature = 0)
|
| 57 |
-
|
| 58 |
barbie_wikipedia_docs = WikipediaLoader(query="Barbie (film)", load_max_docs=1, doc_content_chars_max=1_000_000).load()
|
| 59 |
barbie_csv_docs = CSVLoader(file_path="./barbie_data/barbie.csv", source_column="Review_Url").load()
|
| 60 |
oppenheimer_wikipedia_docs = WikipediaLoader(query="Oppenheimer (film)", load_max_docs=1, doc_content_chars_max=1_000_000).load()
|
|
@@ -106,8 +75,12 @@ async def init():
|
|
| 106 |
opp_wikipedia_faiss_retriever = opp_wikipedia_faiss_store.as_retriever(search_kwargs={"k": 1})
|
| 107 |
|
| 108 |
# set up ensemble retriever
|
| 109 |
-
barbie_ensemble_retriever =
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# #### Retrieval Agent
|
| 113 |
barbie_wikipedia_retrieval_tool = create_retriever_tool(
|
|
|
|
| 17 |
from langchain.agents.agent_toolkits import create_retriever_tool, create_conversational_retrieval_agent
|
| 18 |
from langchain import LLMChain
|
| 19 |
|
| 20 |
+
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature = 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@cl.on_chat_start
|
| 23 |
async def init():
|
| 24 |
msg = cl.Message(content=f"Building Index...")
|
| 25 |
await msg.send()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
barbie_wikipedia_docs = WikipediaLoader(query="Barbie (film)", load_max_docs=1, doc_content_chars_max=1_000_000).load()
|
| 28 |
barbie_csv_docs = CSVLoader(file_path="./barbie_data/barbie.csv", source_column="Review_Url").load()
|
| 29 |
oppenheimer_wikipedia_docs = WikipediaLoader(query="Oppenheimer (film)", load_max_docs=1, doc_content_chars_max=1_000_000).load()
|
|
|
|
| 75 |
opp_wikipedia_faiss_retriever = opp_wikipedia_faiss_store.as_retriever(search_kwargs={"k": 1})
|
| 76 |
|
| 77 |
# set up ensemble retriever
|
| 78 |
+
barbie_ensemble_retriever = await cl.make_async(EnsembleRetriever)(
|
| 79 |
+
retrievers=[barbie_wikipedia_bm25_retriever, barbie_wikipedia_faiss_retriever],
|
| 80 |
+
weights=[0.25, 0.75])
|
| 81 |
+
opp_ensemble_retriever = await cl.make_async(EnsembleRetriever(
|
| 82 |
+
retrievers=[opp_wikipedia_bm25_retriever, opp_wikipedia_faiss_retriever],
|
| 83 |
+
weights=[0.25, 0.75])
|
| 84 |
|
| 85 |
# #### Retrieval Agent
|
| 86 |
barbie_wikipedia_retrieval_tool = create_retriever_tool(
|