Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
llm-bootcamp-projects
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zaid
llm-bootcamp-projects
Commits
020fc973
Commit
020fc973
authored
Oct 12, 2023
by
Hyder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
delete-pages
parent
8bce82eb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
207 additions
and
0 deletions
+207
-0
1_Basic_Chatbot.py
1_Basic_Chatbot.py
+44
-0
2_Chatbot_Agent.py
2_Chatbot_Agent.py
+63
-0
3_Chat_with_your_Data.py
3_Chat_with_your_Data.py
+100
-0
No files found.
1_Basic_Chatbot.py
0 → 100644
View file @
020fc973
import
utils
import
streamlit
as
st
from
streaming
import
StreamHandler
from
langchain.llms
import
OpenAI
from
langchain.chains
import
ConversationChain
st
.
set_page_config
(
page_title
=
"Chatbot"
,
page_icon
=
"💬"
)
st
.
header
(
'Basic Chatbot'
)
st
.
write
(
'Allows users to interact with the OpenAI LLMs'
)
class
Basic
:
def
__init__
(
self
):
utils
.
configure_openai_api_key
()
self
.
openai_model
=
"gpt-3.5-turbo"
def
setup_chain
(
self
):
llm
=
OpenAI
(
model_name
=
self
.
openai_model
,
temperature
=
0
,
streaming
=
True
)
chain
=
ConversationChain
(
llm
=
llm
,
verbose
=
True
)
return
chain
@utils.enable_chat_history
def
main
(
self
):
chain
=
self
.
setup_chain
()
user_query
=
st
.
chat_input
(
placeholder
=
"Ask me anything!"
)
if
user_query
:
utils
.
display_msg
(
user_query
,
'user'
)
with
st
.
chat_message
(
"assistant"
):
try
:
st_cb
=
StreamHandler
(
st
.
empty
())
response
=
chain
.
run
(
user_query
,
callbacks
=
[
st_cb
])
st
.
session_state
.
messages
.
append
(
{
"role"
:
"assistant"
,
"content"
:
response
})
except
Exception
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
obj
=
Basic
()
obj
.
main
()
2_Chatbot_Agent.py
0 → 100644
View file @
020fc973
import
utils
import
streamlit
as
st
from
langchain.agents
import
AgentType
from
langchain.chat_models
import
ChatOpenAI
from
langchain.tools
import
DuckDuckGoSearchRun
from
langchain.agents
import
initialize_agent
,
Tool
from
langchain.callbacks
import
StreamlitCallbackHandler
st
.
set_page_config
(
page_title
=
"ChatWeb"
,
page_icon
=
"🌐"
)
st
.
header
(
'Chatbot with Web Browser Access'
)
st
.
write
(
'Equipped with internet agent, enables users to ask questions about recent events'
)
class
ChatbotTools
:
def
__init__
(
self
):
utils
.
configure_openai_api_key
()
self
.
openai_model
=
"gpt-3.5-turbo"
def
setup_agent
(
self
):
# Define tool
ddg_search
=
DuckDuckGoSearchRun
()
tools
=
[
Tool
(
name
=
"DuckDuckGoSearch"
,
func
=
ddg_search
.
run
,
description
=
"Useful for when you need to answer questions about current events. You should ask targeted questions"
,
)
]
# Setup LLM and Agent
llm
=
ChatOpenAI
(
model_name
=
self
.
openai_model
,
streaming
=
True
)
agent
=
initialize_agent
(
tools
=
tools
,
llm
=
llm
,
agent
=
AgentType
.
ZERO_SHOT_REACT_DESCRIPTION
,
handle_parsing_errors
=
True
,
verbose
=
True
)
return
agent
@utils.enable_chat_history
def
main
(
self
):
agent
=
self
.
setup_agent
()
user_query
=
st
.
chat_input
(
placeholder
=
"Ask me anything!"
)
if
user_query
:
utils
.
display_msg
(
user_query
,
'user'
)
with
st
.
chat_message
(
"assistant"
):
try
:
st_cb
=
StreamlitCallbackHandler
(
st
.
container
())
response
=
agent
.
run
(
user_query
,
callbacks
=
[
st_cb
])
st
.
session_state
.
messages
.
append
(
{
"role"
:
"assistant"
,
"content"
:
response
})
st
.
write
(
response
)
except
Exception
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
obj
=
ChatbotTools
()
obj
.
main
()
3_Chat_with_your_Data.py
0 → 100644
View file @
020fc973
import
os
import
utils
import
streamlit
as
st
from
streaming
import
StreamHandler
from
langchain.chat_models
import
ChatOpenAI
from
langchain.document_loaders
import
PyPDFLoader
from
langchain.memory
import
ConversationBufferMemory
from
langchain.chains
import
ConversationalRetrievalChain
from
langchain.embeddings
import
OpenAIEmbeddings
from
langchain.vectorstores
import
FAISS
from
langchain.text_splitter
import
RecursiveCharacterTextSplitter
st
.
set_page_config
(
page_title
=
"ChatPDF"
,
page_icon
=
"📄"
)
st
.
header
(
'Chat with your Documents'
)
st
.
write
(
'Has access to custom documents and can respond to user queries by referring to the content within those documents'
)
class
CustomDataChatbot
:
def
__init__
(
self
):
utils
.
configure_openai_api_key
()
self
.
openai_model
=
"gpt-3.5-turbo"
def
save_file
(
self
,
file
):
folder
=
'tmp'
if
not
os
.
path
.
exists
(
folder
):
os
.
makedirs
(
folder
)
file_path
=
f
'./{folder}/{file.name}'
with
open
(
file_path
,
'wb'
)
as
f
:
f
.
write
(
file
.
getvalue
())
return
file_path
@st.spinner
(
'Analyzing documents..'
)
def
setup_qa_chain
(
self
,
uploaded_files
):
# Load documents
docs
=
[]
for
file
in
uploaded_files
:
file_path
=
self
.
save_file
(
file
)
loader
=
PyPDFLoader
(
file_path
)
docs
.
extend
(
loader
.
load
())
# Split documents
text_splitter
=
RecursiveCharacterTextSplitter
(
chunk_size
=
1500
,
chunk_overlap
=
200
)
splits
=
text_splitter
.
split_documents
(
docs
)
# Create embeddings and store in vectordb
embeddings
=
OpenAIEmbeddings
()
vectordb
=
FAISS
.
from_documents
(
splits
,
embeddings
)
# Define retriever
retriever
=
vectordb
.
as_retriever
()
# Setup memory for contextual conversation
memory
=
ConversationBufferMemory
(
memory_key
=
'chat_history'
,
return_messages
=
True
)
# Setup LLM and QA chain
llm
=
ChatOpenAI
(
model_name
=
self
.
openai_model
,
temperature
=
0
,
streaming
=
True
)
qa_chain
=
ConversationalRetrievalChain
.
from_llm
(
llm
,
retriever
=
retriever
,
memory
=
memory
,
verbose
=
True
)
return
qa_chain
@utils.enable_chat_history
def
main
(
self
):
# User Inputs
uploaded_files
=
st
.
sidebar
.
file_uploader
(
label
=
'Upload PDF files'
,
type
=
[
'pdf'
],
accept_multiple_files
=
True
)
if
not
uploaded_files
:
st
.
error
(
"Please upload PDF documents to continue!"
)
st
.
stop
()
user_query
=
st
.
chat_input
(
placeholder
=
"Ask me anything!"
)
if
uploaded_files
and
user_query
:
qa_chain
=
self
.
setup_qa_chain
(
uploaded_files
)
utils
.
display_msg
(
user_query
,
'user'
)
with
st
.
chat_message
(
"assistant"
):
try
:
st_cb
=
StreamHandler
(
st
.
empty
())
response
=
qa_chain
.
run
(
user_query
,
callbacks
=
[
st_cb
])
st
.
session_state
.
messages
.
append
(
{
"role"
:
"assistant"
,
"content"
:
response
})
except
Exception
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
obj
=
CustomDataChatbot
()
obj
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment