1
1
import os
2
2
from operator import itemgetter
3
3
4
- import streamlit as st
5
4
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
6
9
from langchain_core .output_parsers import StrOutputParser
7
10
from langchain_core .prompts import ChatPromptTemplate , MessagesPlaceholder
11
+ from langchain_core .runnables .history import RunnableWithMessageHistory
12
+ from langchain_mongodb import MongoDBAtlasVectorSearch , MongoDBChatMessageHistory
8
13
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
13
14
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
16
16
17
17
# System message for the chatbot
18
18
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
46
47
47
loaders = [
48
48
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" ),
50
50
]
51
51
docs = []
52
52
for loader in loaders :
53
53
for doc in loader .load ():
54
54
docs .append (doc )
55
55
md = MarkdownifyTransformer ()
56
- text_splitter = RecursiveCharacterTextSplitter (
57
- chunk_size = 1000 , chunk_overlap = 200 )
56
+ text_splitter = RecursiveCharacterTextSplitter (chunk_size = 1000 , chunk_overlap = 200 )
58
57
docs = loader .load ()
59
58
converted_docs = md .transform_documents (docs )
60
59
splits = text_splitter .split_documents (converted_docs )
61
60
vectorstore = MongoDBAtlasVectorSearch .from_documents (
62
- splits , embedding , collection = collection , index_name = "default" )
61
+ splits , embedding , collection = collection , index_name = "default"
62
+ )
63
63
vectorstore .create_vector_search_index (768 )
64
64
65
65
# Initialize retriever and chat model
66
66
retriever = vectorstore .as_retriever ()
67
67
chat = ChatOllama (model = MODEL )
68
68
69
69
# 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
+ )
75
77
76
78
# 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
+
82
90
83
91
# Function to get session history
84
92
def get_session_history () -> BaseChatMessageHistory :
@@ -87,7 +95,11 @@ def get_session_history() -> BaseChatMessageHistory:
87
95
88
96
# Initialize history chain
89
97
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
+ )
91
103
92
104
# Streamlit UI
93
105
st .title ("Chatbot" )
0 commit comments