DavidFernandes commited on
Commit
8ed39b7
·
verified ·
1 Parent(s): 458ca5a

Delete pages/AI Search.py

Browse files
Files changed (1) hide show
  1. pages/AI Search.py +0 -77
pages/AI Search.py DELETED
@@ -1,77 +0,0 @@
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 langchain_openai import ChatOpenAI
8
- from dotenv import load_dotenv
9
- import os
10
-
11
- load_dotenv()
12
-
13
- openai_api_key = os.getenv("OPENAI_API_KEY")
14
-
15
- import streamlit as st
16
-
17
- st.title("Chat with search")
18
-
19
-
20
- if "steps" not in st.session_state:
21
- st.session_state.steps = {}
22
-
23
- msgs = StreamlitChatMessageHistory()
24
- memory = ConversationBufferMemory(
25
- chat_memory=msgs, return_messages=True, memory_key="chat_history", output_key="output"
26
- )
27
-
28
- # Remove the sidebar button and add a button in the main content area
29
- if st.button("Reset chat history", key="reset_chat_history"):
30
- msgs.clear()
31
- msgs.add_ai_message("How can I help you?")
32
- st.session_state.steps = {}
33
-
34
- avatars = {"human": "user", "ai": "assistant"}
35
- for idx, msg in enumerate(msgs.messages):
36
- with st.chat_message(avatars[msg.type]):
37
- # Render intermediate steps if any were saved
38
- for step in st.session_state.steps.get(str(idx), []):
39
- if step[0].tool == "_Exception":
40
- continue
41
- with st.status(f"**{step[0].tool}**: {step[0].tool_input}", state="complete"):
42
- st.write(step[0].log)
43
- st.write(step[1])
44
- st.write(msg.content)
45
-
46
- if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
47
- st.chat_message("user").write(prompt)
48
-
49
- llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
50
- tools = [DuckDuckGoSearchRun(name="Search")]
51
- chat_agent = ConversationalChatAgent.from_llm_and_tools(llm=llm, tools=tools)
52
- executor = AgentExecutor.from_agent_and_tools(
53
- agent=chat_agent,
54
- tools=tools,
55
- memory=memory,
56
- return_intermediate_steps=True,
57
- handle_parsing_errors=True,
58
- )
59
- with st.chat_message("assistant"):
60
- st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
61
- cfg = RunnableConfig()
62
- cfg["callbacks"] = [st_cb]
63
- response = executor.invoke(prompt, cfg)
64
- st.write(response["output"])
65
- st.session_state.steps[str(len(msgs.messages) - 1)] = response["intermediate_steps"]
66
-
67
- # Add custom CSS to position the button next to the input field
68
- st.markdown("""
69
- <style>
70
- .stButton > button {
71
- margin-top: 10px;
72
- }
73
- .stButton > button[data-baseweb="button"] {
74
- margin-left: 10px;
75
- }
76
- </style>
77
- """, unsafe_allow_html=True)