Skip to content

Commit fbe5e9c

Browse files
author
ajosh0504
committed
Pre-commit checks are green
1 parent 5e8e992 commit fbe5e9c

File tree

4 files changed

+6959
-6946
lines changed

4 files changed

+6959
-6946
lines changed

apps/local-bot/app.py

+34-22
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import os
22
from operator import itemgetter
33

4-
import streamlit as st
54
import ollama
5+
import streamlit as st
6+
from langchain_community.document_loaders import WebBaseLoader
7+
from langchain_community.document_transformers import MarkdownifyTransformer
8+
from langchain_core.chat_history import BaseChatMessageHistory
69
from langchain_core.output_parsers import StrOutputParser
710
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
11+
from langchain_core.runnables.history import RunnableWithMessageHistory
12+
from langchain_mongodb import MongoDBAtlasVectorSearch, MongoDBChatMessageHistory
813
from langchain_ollama import ChatOllama, OllamaEmbeddings
9-
from pymongo import MongoClient
10-
from langchain_mongodb import MongoDBChatMessageHistory, MongoDBAtlasVectorSearch
11-
from langchain_community.document_loaders import WebBaseLoader
12-
from langchain_community.document_transformers import MarkdownifyTransformer
1314
from langchain_text_splitters import RecursiveCharacterTextSplitter
14-
from langchain_core.runnables.history import RunnableWithMessageHistory
15-
from langchain_core.chat_history import BaseChatMessageHistory
15+
from pymongo import MongoClient
1616

1717
# System message for the chatbot
1818
SYSTEM_MESSAGE = """You're a helpful assistant. Answer all questions to the best of your ability. If you don't know the answer let the user know to find help on the internet.
@@ -46,39 +46,47 @@
4646

4747
loaders = [
4848
WebBaseLoader("https://en.wikipedia.org/wiki/AT%26T"),
49-
WebBaseLoader("https://en.wikipedia.org/wiki/Bank_of_America")
49+
WebBaseLoader("https://en.wikipedia.org/wiki/Bank_of_America"),
5050
]
5151
docs = []
5252
for loader in loaders:
5353
for doc in loader.load():
5454
docs.append(doc)
5555
md = MarkdownifyTransformer()
56-
text_splitter = RecursiveCharacterTextSplitter(
57-
chunk_size=1000, chunk_overlap=200)
56+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
5857
docs = loader.load()
5958
converted_docs = md.transform_documents(docs)
6059
splits = text_splitter.split_documents(converted_docs)
6160
vectorstore = MongoDBAtlasVectorSearch.from_documents(
62-
splits, embedding, collection=collection, index_name="default")
61+
splits, embedding, collection=collection, index_name="default"
62+
)
6363
vectorstore.create_vector_search_index(768)
6464

6565
# Initialize retriever and chat model
6666
retriever = vectorstore.as_retriever()
6767
chat = ChatOllama(model=MODEL)
6868

6969
# Define prompt template
70-
prompt_template = ChatPromptTemplate.from_messages([
71-
("system", SYSTEM_MESSAGE),
72-
MessagesPlaceholder("history"),
73-
("human", "{input}"),
74-
])
70+
prompt_template = ChatPromptTemplate.from_messages(
71+
[
72+
("system", SYSTEM_MESSAGE),
73+
MessagesPlaceholder("history"),
74+
("human", "{input}"),
75+
]
76+
)
7577

7678
# Define the chain of operations
77-
chain = {
78-
"context": itemgetter("input") | retriever,
79-
"input": itemgetter("input"),
80-
"history": itemgetter("history")
81-
} | prompt_template | chat | StrOutputParser()
79+
chain = (
80+
{
81+
"context": itemgetter("input") | retriever,
82+
"input": itemgetter("input"),
83+
"history": itemgetter("history"),
84+
}
85+
| prompt_template
86+
| chat
87+
| StrOutputParser()
88+
)
89+
8290

8391
# Function to get session history
8492
def get_session_history() -> BaseChatMessageHistory:
@@ -87,7 +95,11 @@ def get_session_history() -> BaseChatMessageHistory:
8795

8896
# Initialize history chain
8997
history_chain = RunnableWithMessageHistory(
90-
chain, get_session_history, input_messages_key="input", history_messages_key="history")
98+
chain,
99+
get_session_history,
100+
input_messages_key="input",
101+
history_messages_key="history",
102+
)
91103

92104
# Streamlit UI
93105
st.title("Chatbot")

apps/local-bot/compose.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ services:
1818
- MONGODB_INITDB_ROOT_PASSWORD=root
1919
ports:
2020
- 27017:27017
21-

apps/local-bot/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ langchain_ollama
55
pymongo
66
langchain_mongodb
77
langchain_community
8-
markdownify
8+
markdownify

0 commit comments

Comments
 (0)