Spaces:
Sleeping
Sleeping
Upload AI Search.py
Browse files- pages/AI Search.py +76 -0
pages/AI Search.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.agents import ConversationalChatAgent, AgentExecutor
|
| 2 |
+
from langchain.memory import ConversationBufferMemory
|
| 3 |
+
from langchain_community.callbacks import StreamlitCallbackHandler
|
| 4 |
+
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
| 5 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 6 |
+
from langchain_core.runnables import RunnableConfig
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
google_gemini_api_key = os.getenv("GOOGLE_API_KEY")
|
| 15 |
+
|
| 16 |
+
import streamlit as st
|
| 17 |
+
|
| 18 |
+
st.set_page_config(
|
| 19 |
+
page_title="J.A.RV.I.S.",
|
| 20 |
+
page_icon="",
|
| 21 |
+
layout="centered",
|
| 22 |
+
)
|
| 23 |
+
st.title(":violet[Chat] with AISearch", anchor=False)
|
| 24 |
+
|
| 25 |
+
if "steps" not in st.session_state:
|
| 26 |
+
st.session_state.steps = {}
|
| 27 |
+
|
| 28 |
+
msgs= StreamlitChatMessageHistory()
|
| 29 |
+
memory = ConversationBufferMemory(
|
| 30 |
+
chat_memory=msgs, return_messages=True, memory_key="chat_history", output_key="output"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Remove the sidebar button and add a button in the main content area
|
| 34 |
+
if st.button("Reset chat history", key="reset_chat_history"):
|
| 35 |
+
msgs.clear()
|
| 36 |
+
st.session_state.steps = {}
|
| 37 |
+
|
| 38 |
+
avatars = {"human": "user", "ai": "assistant"}
|
| 39 |
+
for idx, msg in enumerate(msgs.messages):
|
| 40 |
+
with st.chat_message(avatars[msg.type]):
|
| 41 |
+
# Render intermediate steps if any were saved
|
| 42 |
+
for step in st.session_state.steps.get(str(idx), []):
|
| 43 |
+
if step[0].tool == "_Exception":
|
| 44 |
+
continue
|
| 45 |
+
with st.status(f"**{step[0].tool}**: {step[0].tool_input}", state="complete"):
|
| 46 |
+
st.write(step[0].log)
|
| 47 |
+
st.write(step[1])
|
| 48 |
+
st.write(msg.content)
|
| 49 |
+
|
| 50 |
+
from datetime import datetime
|
| 51 |
+
|
| 52 |
+
if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
|
| 53 |
+
# Get the current date
|
| 54 |
+
current_date = datetime.now().strftime("%d-%m-%Y")
|
| 55 |
+
# Modify the prompt to include the current date for the model's search
|
| 56 |
+
prompt_with_date = f"{prompt} as of {current_date}"
|
| 57 |
+
# Display the original prompt in the chat without the date
|
| 58 |
+
st.chat_message("user").write(prompt)
|
| 59 |
+
|
| 60 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro", api_key=google_gemini_api_key, streaming=True, convert_system_message_to_human=True)
|
| 61 |
+
tools = [DuckDuckGoSearchRun(name="Search")]
|
| 62 |
+
chat_agent = ConversationalChatAgent.from_llm_and_tools(llm=llm, tools=tools)
|
| 63 |
+
executor = AgentExecutor.from_agent_and_tools(
|
| 64 |
+
agent=chat_agent,
|
| 65 |
+
tools=tools,
|
| 66 |
+
memory=memory,
|
| 67 |
+
return_intermediate_steps=True,
|
| 68 |
+
handle_parsing_errors=True,
|
| 69 |
+
)
|
| 70 |
+
with st.chat_message("assistant"):
|
| 71 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 72 |
+
cfg = RunnableConfig()
|
| 73 |
+
cfg["callbacks"] = [st_cb]
|
| 74 |
+
response = executor.invoke(prompt_with_date, cfg)
|
| 75 |
+
st.write(response["output"])
|
| 76 |
+
st.session_state.steps[str(len(msgs.messages) - 1)] = response["intermediate_steps"]
|