mafzaal commited on
Commit
04abf37
·
1 Parent(s): a297ec7

Add initial implementation of chat application with environment variable support and vector storage integration

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. README.md +22 -0
  3. app.py +110 -0
  4. chainlit.md +6 -0
  5. pyproject.toml +3 -0
.gitignore CHANGED
@@ -1,5 +1,6 @@
1
  data/
2
  db/
 
3
 
4
  # Byte-compiled / optimized / DLL files
5
  __pycache__/
 
1
  data/
2
  db/
3
+ .chainlit/
4
 
5
  # Byte-compiled / optimized / DLL files
6
  __pycache__/
README.md CHANGED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Welcome to TheDataGuy Chat! 👋
2
+
3
+ This is a Q&A chatbot powered by TheDataGuy blog posts. Ask questions about topics covered in the blog, such as:
4
+
5
+ - RAGAS and RAG evaluation
6
+ - Building research agents
7
+ - Metric-driven development
8
+ - Data science best practices
9
+
10
+ ## How it works
11
+
12
+ Under the hood, this application uses:
13
+
14
+ 1. **Snowflake Arctic Embeddings**: To convert text into vector representations
15
+ 2. **Qdrant Vector Database**: To store and search for similar content
16
+ 3. **GPT-4o-mini**: To generate helpful responses based on retrieved content
17
+ 4. **LangChain**: For building the RAG workflow
18
+ 5. **Chainlit**: For the chat interface
19
+
20
+ ## Sources
21
+
22
+ All answers are generated based on content from [TheDataGuy blog](https://thedataguy.pro/blog/). Sources are shown for each response so you can read more about the topic.
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import getpass
3
+ from pathlib import Path
4
+ from operator import itemgetter
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+
10
+ import chainlit as cl
11
+ from langchain.prompts import ChatPromptTemplate
12
+ from langchain.schema.runnable import RunnablePassthrough
13
+ from langchain_openai.chat_models import ChatOpenAI
14
+ from langchain_huggingface import HuggingFaceEmbeddings
15
+ from langchain_qdrant import QdrantVectorStore
16
+ from qdrant_client import QdrantClient
17
+ from qdrant_client.http.models import Distance, VectorParams
18
+
19
+ # Get vector storage path from .env file with fallback
20
+ storage_path = Path(os.environ.get("VECTOR_STORAGE_PATH", "./db/vectorstore_v3"))
21
+ #qclient = QdrantClient(storage_path)
22
+
23
+ # Load embedding model from environment variable with fallback
24
+ embedding_model = os.environ.get("EMBEDDING_MODEL", "Snowflake/snowflake-arctic-embed-l")
25
+ huggingface_embeddings = HuggingFaceEmbeddings(model_name=embedding_model)
26
+
27
+ # Set up Qdrant vectorstore from existing collection
28
+ collection_name = os.environ.get("QDRANT_COLLECTION", "thedataguy_documents")
29
+
30
+ vector_store = QdrantVectorStore.from_existing_collection(
31
+ #client=qclient,
32
+ path=storage_path,
33
+ collection_name=collection_name,
34
+ embedding=huggingface_embeddings,
35
+ )
36
+
37
+
38
+ # Create a retriever
39
+ retriever = vector_store.as_retriever()
40
+
41
+ # Set up ChatOpenAI with environment variables
42
+ llm_model = os.environ.get("LLM_MODEL", "gpt-4o-mini")
43
+ temperature = float(os.environ.get("TEMPERATURE", "0"))
44
+ llm = ChatOpenAI(model=llm_model, temperature=temperature)
45
+
46
+ # Create RAG prompt template
47
+ rag_prompt_template = """\
48
+ You are a helpful assistant that answers questions based on the context provided.
49
+ Generate a concise answer to the question in markdown format and include a list of relevant links to the context.
50
+ Use links from context to help user to navigate to to find more information.
51
+ You have access to the following information:
52
+
53
+ Context:
54
+ {context}
55
+
56
+ Question:
57
+ {question}
58
+
59
+ If context is unrelated to question, say "I don't know".
60
+ """
61
+
62
+ rag_prompt = ChatPromptTemplate.from_template(rag_prompt_template)
63
+
64
+ # Create chain
65
+ retrieval_augmented_qa_chain = (
66
+ {"context": itemgetter("question") | retriever, "question": itemgetter("question")}
67
+ | RunnablePassthrough.assign(context=itemgetter("context"))
68
+ | {"response": rag_prompt | llm, "context": itemgetter("context")}
69
+ )
70
+
71
+
72
+
73
+ @cl.on_chat_start
74
+ async def setup_chain():
75
+ # Check if API key is already set
76
+ api_key = os.environ.get("OPENAI_API_KEY")
77
+ if not api_key:
78
+ # In a real app, you'd want to handle this more gracefully
79
+ api_key = await cl.AskUserMessage(
80
+ content="Please enter your OpenAI API Key:",
81
+ timeout=60,
82
+ raise_on_timeout=True
83
+ ).send()
84
+ os.environ["OPENAI_API_KEY"] = api_key.content
85
+
86
+ # Set a loading message
87
+ msg = cl.Message(content="Let's talk about [TheDataGuy](https://thedataguy.pro)'s blog posts, how can I help you?", author="System")
88
+ await msg.send()
89
+
90
+ # Store the chain in user session
91
+ cl.user_session.set("chain", retrieval_augmented_qa_chain)
92
+
93
+
94
+
95
+ @cl.on_message
96
+ async def on_message(message: cl.Message):
97
+ # Get chain from user session
98
+ chain = cl.user_session.get("chain")
99
+
100
+ print( message.content)
101
+ # Call the chain with the user message
102
+ response = chain.invoke({"question": message.content})
103
+
104
+
105
+ # Send the response with sources
106
+ await cl.Message(
107
+ content=response["response"].content,
108
+
109
+ ).send()
110
+
chainlit.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # Let's Talk
2
+
3
+ `Let's Talk` is chat app based on contents from [TheDataGuy](https://thedataguy.pro)'s blog posts.
4
+
5
+ More information at [Let's Talk](https://github.com/mafzaal/lets-talk)
6
+
pyproject.toml CHANGED
@@ -5,14 +5,17 @@ description = "Add your description here"
5
  readme = "README.md"
6
  requires-python = ">=3.13"
7
  dependencies = [
 
8
  "ipykernel>=6.29.5",
9
  "langchain>=0.3.25",
10
  "langchain-community>=0.3.23",
11
  "langchain-core>=0.3.59",
12
  "langchain-huggingface>=0.2.0",
13
  "langchain-openai>=0.3.16",
 
14
  "langchain-text-splitters>=0.3.8",
15
  "pandas>=2.2.3",
 
16
  "qdrant-client>=1.14.2",
17
  "unstructured[md]>=0.17.2",
18
  ]
 
5
  readme = "README.md"
6
  requires-python = ">=3.13"
7
  dependencies = [
8
+ "chainlit>=2.5.5",
9
  "ipykernel>=6.29.5",
10
  "langchain>=0.3.25",
11
  "langchain-community>=0.3.23",
12
  "langchain-core>=0.3.59",
13
  "langchain-huggingface>=0.2.0",
14
  "langchain-openai>=0.3.16",
15
+ "langchain-qdrant>=0.2.0",
16
  "langchain-text-splitters>=0.3.8",
17
  "pandas>=2.2.3",
18
+ "python-dotenv>=1.1.0",
19
  "qdrant-client>=1.14.2",
20
  "unstructured[md]>=0.17.2",
21
  ]