File size: 4,346 Bytes
a557e27 a4d579b c83187d a4d579b c83187d a4d579b c83187d a4d579b e1a76a5 81258cc cfdc9de 17aca0b 81258cc 7f4ceb4 a4d579b c83187d a4d579b dd11e70 f187dab 81258cc a4d579b 18648f2 81258cc a4d579b 956f515 a4d579b 68fccfb c83187d a4d579b 6cf109f a4d579b c28a6ea a4d579b 68fccfb a4d579b a557e27 ae7bc30 a4d579b ae7bc30 a557e27 a4d579b a557e27 a4d579b a557e27 a4d579b a557e27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import os
import streamlit as st
from langchain.embeddings import HuggingFaceInstructEmbeddings, HuggingFaceEmbeddings
from langchain.vectorstores.faiss import FAISS
from huggingface_hub import snapshot_download
from langchain.callbacks import StreamlitCallbackHandler
from langchain.agents import OpenAIFunctionsAgent, AgentExecutor
from langchain.agents.agent_toolkits import create_retriever_tool
from langchain.agents.openai_functions_agent.agent_token_buffer_memory import (
AgentTokenBufferMemory,
)
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, AIMessage, HumanMessage
from langchain.prompts import MessagesPlaceholder
from langsmith import Client
client = Client()
st.set_page_config(
page_title="Investor Education ChatChain",
page_icon="π",
layout="wide",
initial_sidebar_state="collapsed",
)
#Load API Key
api_key = os.environ["OPENAI_API_KEY"]
#### sidebar section 1 ####
site_options = {'US': 'vanguard_embeddings_US',
'AUS': 'vanguard-embeddings'}
site_options_list = list(site_options.keys())
site_radio = st.radio(
"Which Vanguard website location would you want to chat to?",
('US', 'AUS'))
@st.cache_data
def load_vectorstore(site):
'''load embeddings and vectorstore'''
emb = HuggingFaceEmbeddings(model_name="all-mpnet-base-v2")
vectorstore = FAISS.load_local(site_options[site], emb,allow_dangerous_deserialization=True)
return vectorstore.as_retriever(search_kwargs={"k": 4})
tool = create_retriever_tool(
load_vectorstore(site_radio),
"search_vaguard_website",
"Searches and returns documents regarding the Vanguard website across US and AUS locations. The websites provide investment related information to the user")
tools = [tool]
llm = ChatOpenAI(temperature=0, streaming=True, model="gpt-4o")
message = SystemMessage(
content=(
"You are a helpful chatbot who is tasked with answering questions about investments using informationn that has been scraped from a website to answer the users question accurately."
"Do not use any information not provided in the website context."
"Unless otherwise explicitly stated, it is probably fair to assume that questions are about the CFA program and materials. "
"If there is any ambiguity, you probably assume they are about that."
)
)
prompt = OpenAIFunctionsAgent.create_prompt(
system_message=message,
extra_prompt_messages=[MessagesPlaceholder(variable_name="history")],
)
agent = OpenAIFunctionsAgent(llm=llm, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
return_intermediate_steps=True,
)
memory = AgentTokenBufferMemory(llm=llm)
starter_message = "Ask me anything about information on the Vanguard US/AUS websites!"
if "messages" not in st.session_state or st.sidebar.button("Clear message history"):
st.session_state["messages"] = [AIMessage(content=starter_message)]
def send_feedback(run_id, score):
client.create_feedback(run_id, "user_score", score=score)
for msg in st.session_state.messages:
if isinstance(msg, AIMessage):
st.chat_message("assistant").write(msg.content)
elif isinstance(msg, HumanMessage):
st.chat_message("user").write(msg.content)
memory.chat_memory.add_message(msg)
if prompt := st.chat_input(placeholder=starter_message):
st.chat_message("user").write(prompt)
with st.chat_message("assistant"):
st_callback = StreamlitCallbackHandler(st.container())
response = agent_executor(
{"input": prompt, "history": st.session_state.messages},
callbacks=[st_callback],
include_run_info=True,
)
st.session_state.messages.append(AIMessage(content=response["output"]))
st.write(response["output"])
memory.save_context({"input": prompt}, response)
st.session_state["messages"] = memory.buffer
run_id = response["__run"].run_id
col_blank, col_text, col1, col2 = st.columns([10, 2, 1, 1])
with col_text:
st.text("Feedback:")
# with col1:
# st.button("π", on_click=send_feedback, args=(run_id, 1))
# with col2:
# st.button("π", on_click=send_feedback, args=(run_id, 0)
|