Spaces:
Runtime error
Runtime error
Commit
•
5eeff8b
0
Parent(s):
Duplicate from ml-maker-space/AliceInWonderLandChainlit
Browse filesCo-authored-by: Chris Alexiuk <c-s-ale@users.noreply.huggingface.co>
- .env.example +1 -0
- .gitattributes +34 -0
- .gitignore +4 -0
- Dockerfile +11 -0
- README.md +10 -0
- app.py +102 -0
- chainlit.md +12 -0
- requirements.txt +7 -0
- vector_db/chroma-collections.parquet +3 -0
- vector_db/chroma-embeddings.parquet +3 -0
- vector_db/index/id_to_uuid_d0cf4a6f-27f8-489b-ae73-fac746c9128b.pkl +3 -0
- vector_db/index/index_d0cf4a6f-27f8-489b-ae73-fac746c9128b.bin +3 -0
- vector_db/index/index_metadata_d0cf4a6f-27f8-489b-ae73-fac746c9128b.pkl +3 -0
- vector_db/index/uuid_to_id_d0cf4a6f-27f8-489b-ae73-fac746c9128b.pkl +3 -0
.env.example
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OPENAI_API_KEY=
|
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
.vscode
|
3 |
+
.chroma
|
4 |
+
__pycache__
|
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
RUN useradd -m -u 1000 user
|
3 |
+
USER user
|
4 |
+
ENV HOME=/home/user \
|
5 |
+
PATH=/home/user/.local/bin:$PATH
|
6 |
+
WORKDIR $HOME/app
|
7 |
+
COPY --chown=user . $HOME/app
|
8 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
9 |
+
RUN pip install -r requirements.txt
|
10 |
+
COPY . .
|
11 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
README.md
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Through The Looking Glass
|
3 |
+
emoji: 💻
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: gray
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
license: openrail
|
9 |
+
duplicated_from: ml-maker-space/AliceInWonderLandChainlit
|
10 |
+
---
|
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from langchain.agents import initialize_agent, Tool
|
3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
+
from langchain.agents import AgentType
|
5 |
+
from langchain.tools import BaseTool
|
6 |
+
from langchain.llms import OpenAI
|
7 |
+
from langchain import SerpAPIWrapper, LLMChain
|
8 |
+
from langchain.chains import RetrievalQA
|
9 |
+
from langchain.chat_models import ChatOpenAI
|
10 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
11 |
+
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
|
12 |
+
from langchain.document_loaders import TextLoader, DirectoryLoader
|
13 |
+
from langchain.vectorstores import Chroma
|
14 |
+
import os
|
15 |
+
import arxiv
|
16 |
+
import chainlit as cl
|
17 |
+
from chainlit import user_session
|
18 |
+
|
19 |
+
@cl.langchain_factory(use_async=True)
|
20 |
+
async def init():
|
21 |
+
# Set the OpenAI Embeddings model
|
22 |
+
embeddings = embeddings = OpenAIEmbeddings()
|
23 |
+
|
24 |
+
# Set the persist directory
|
25 |
+
persist_directory = "vector_db"
|
26 |
+
|
27 |
+
# Load the persisted Chroma vector store
|
28 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
|
29 |
+
|
30 |
+
# Create a chain that uses the Chroma vector store
|
31 |
+
alice_qa = RetrievalQA.from_chain_type(
|
32 |
+
ChatOpenAI(
|
33 |
+
model_name="gpt-3.5-turbo-16k",
|
34 |
+
temperature=0,
|
35 |
+
),
|
36 |
+
chain_type="stuff",
|
37 |
+
retriever=vectordb.as_retriever(),
|
38 |
+
)
|
39 |
+
|
40 |
+
search = SerpAPIWrapper()
|
41 |
+
|
42 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
43 |
+
readonlymemory = ReadOnlySharedMemory(memory=memory)
|
44 |
+
|
45 |
+
tools = [
|
46 |
+
Tool(
|
47 |
+
name = "Alice in Wonderland QA System",
|
48 |
+
func=alice_qa.run,
|
49 |
+
description="useful for when you need to answer questions about Alice in Wonderland. Input should be a fully formed question."
|
50 |
+
),
|
51 |
+
Tool(
|
52 |
+
name = "Backup Alice Google Search",
|
53 |
+
func=search.run,
|
54 |
+
description="useful for when you need to answer questions about Alice in Wonderland but only when the Alice in Wonderland QA System couldn't answer the query. Input should be a fully formed question."
|
55 |
+
),
|
56 |
+
]
|
57 |
+
|
58 |
+
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
|
59 |
+
suffix = """Begin!"
|
60 |
+
|
61 |
+
{chat_history}
|
62 |
+
Question: {input}
|
63 |
+
{agent_scratchpad}"""
|
64 |
+
|
65 |
+
prompt = ZeroShotAgent.create_prompt(
|
66 |
+
tools,
|
67 |
+
prefix=prefix,
|
68 |
+
suffix=suffix,
|
69 |
+
input_variables=["input", "chat_history", "agent_scratchpad"]
|
70 |
+
)
|
71 |
+
|
72 |
+
llm_chain = LLMChain(
|
73 |
+
llm=ChatOpenAI(
|
74 |
+
model_name="gpt-3.5-turbo-16k",
|
75 |
+
temperature=0,
|
76 |
+
),
|
77 |
+
prompt=prompt
|
78 |
+
)
|
79 |
+
|
80 |
+
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
|
81 |
+
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)
|
82 |
+
|
83 |
+
# Let the user know that the system is ready
|
84 |
+
await cl.Message(
|
85 |
+
content=f"You can begin by asking any questions about Alice in Wonderland!"
|
86 |
+
).send()
|
87 |
+
|
88 |
+
return agent_chain
|
89 |
+
|
90 |
+
@cl.langchain_run
|
91 |
+
async def run(agent, input_str):
|
92 |
+
res = await cl.make_async(agent)(input_str, callbacks=[cl.LangchainCallbackHandler()])
|
93 |
+
print(res)
|
94 |
+
await cl.Message(content=res["output"]).send()
|
95 |
+
|
96 |
+
@cl.langchain_rename
|
97 |
+
def rename(original_llm_chain: str):
|
98 |
+
rename_dict = {
|
99 |
+
"LLMChain" : "The Mad Hatter 🤪🎩"
|
100 |
+
}
|
101 |
+
|
102 |
+
return rename_dict.get(original_llm_chain, original_llm_chain)
|
chainlit.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Down the LangChain Rabbit Hole 🐇⏰!
|
2 |
+
|
3 |
+
This is a companion application for the [How to Build ChatGPT for Your Data](https://lu.ma/r3q7ndj4) event put on by the Machine Learning Makerspace!
|
4 |
+
|
5 |
+
# Notebooks
|
6 |
+
|
7 |
+
- Introduction to LangChain notebook available here: [Down the LangChain Rabbit Hole 🐇⏰](https://colab.research.google.com/drive/1HjZdfhVx-cVu3r-Xk-dGL6B3ZU0wtfAO?usp=sharing)
|
8 |
+
- LangChain Agent notebook available here [Through the LangChain Looking Glass 🐇⏰](https://colab.research.google.com/drive/1Q-lm-apSJRYwoPvUZGiwYMezeky0yQXD?usp=sharing)
|
9 |
+
|
10 |
+
![image](https://i.imgur.com/L5aklsK.png)
|
11 |
+
|
12 |
+
Feel free to ask questions about the Alice in Wonderland series by [Lewis Carroll](https://en.wikipedia.org/wiki/Alice%27s_Adventures_in_Wonderland)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
arxiv==1.4.7
|
2 |
+
langchain
|
3 |
+
chainlit
|
4 |
+
openai
|
5 |
+
chromadb
|
6 |
+
tiktoken
|
7 |
+
google-search-results
|
vector_db/chroma-collections.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dacc35a2bc6103853543323e632f513acf9899848acf03b71e451d2745138842
|
3 |
+
size 557
|
vector_db/chroma-embeddings.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:494201e44259368c11b93d43f1a2673ebb28d32567a27e681206b8101a552545
|
3 |
+
size 5300049
|
vector_db/index/id_to_uuid_d0cf4a6f-27f8-489b-ae73-fac746c9128b.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:923ac0841a588099b308d49d7b5df76e2231555f2c10897d2d511c83a59fd731
|
3 |
+
size 20355
|
vector_db/index/index_d0cf4a6f-27f8-489b-ae73-fac746c9128b.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a097f4871ca53fe0ce45bdad0bf4199bde4c7f203066be5dda1665df38ce0bc5
|
3 |
+
size 3983392
|
vector_db/index/index_metadata_d0cf4a6f-27f8-489b-ae73-fac746c9128b.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e53255fbf25ed70518581988507ffc72bfe8a4f6699d2546c3fbc1a2070ce1df
|
3 |
+
size 105
|
vector_db/index/uuid_to_id_d0cf4a6f-27f8-489b-ae73-fac746c9128b.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b78d6a3e9574ccfa09550c45aa17804d1e0b483ea87b862e0529153a5700718a
|
3 |
+
size 23815
|