watanabe3tipapa commited on
Commit
9bd5920
1 Parent(s): e0817ee

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +129 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from langchain.chat_models import ChatOpenAI
4
+
5
+ from langchain.agents import initialize_agent
6
+ from langchain.callbacks import StreamlitCallbackHandler
7
+ from langchain.chat_models import ChatOpenAI
8
+ from langchain.schema import SystemMessage
9
+
10
+ from tools.search_ddg import get_search_ddg_tool
11
+ from tools.fetch_page import get_fetch_page_tool
12
+
13
+ CUSTOM_SYSTEM_PROMPT = """
14
+ You are an assistant that conducts online research based on user requests. Using available tools, please explain the researched information.
15
+ Please don't answer based solely on what you already know. Always perform a search before providing a response.
16
+
17
+ In special cases, such as when the user specifies a page to read, there's no need to search.
18
+ Please read the provided page and answer the user's question accordingly.
19
+
20
+ If you find that there's not much information just by looking at the search results page, consider these two options and try them out.
21
+ Users usually don't ask extremely unusual questions, so you'll likely find an answer:
22
+
23
+ - Try clicking on the links of the search results to access and read the content of each page.
24
+ - Change your search query and perform a new search.
25
+
26
+ Users are extremely busy and not as free as you are.
27
+ Therefore, to save the user's effort, please provide direct answers.
28
+
29
+ BAD ANSWER EXAMPLE
30
+ - Please refer to these pages.
31
+ - You can write code referring these pages.
32
+ - Following page will be helpful.
33
+
34
+ GOOD ANSWER EXAMPLE
35
+ - This is sample code: -- sample code here --
36
+ - The answer of you question is -- answer here --
37
+
38
+ Please make sure to list the URLs of the pages you referenced at the end of your answer. (This will allow users to verify your response.)
39
+
40
+ Please make sure to answer in the language used by the user. If the user asks in Japanese, please answer in Japanese. If the user asks in Spanish, please answer in Spanish.
41
+ But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
42
+ """
43
+
44
+
45
+ def init_page():
46
+ st.set_page_config(
47
+ page_title="Web SGE Agent",
48
+ page_icon="🤗"
49
+ )
50
+ st.header("Web SGE Agent 🤗")
51
+ st.sidebar.title("Options")
52
+ st.session_state["openai_api_key"] = st.sidebar.text_input(
53
+ "OpenAI API Key", type="password")
54
+ st.session_state["langsmith_api_key"] = st.sidebar.text_input(
55
+ "LangSmith API Key (optional)", type="password")
56
+
57
+
58
+ def init_messages():
59
+ clear_button = st.sidebar.button("Clear Conversation", key="clear")
60
+ if clear_button or "messages" not in st.session_state:
61
+ st.session_state.messages = [
62
+ {"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
63
+ ]
64
+ st.session_state.costs = []
65
+
66
+
67
+ def select_model():
68
+ model = st.sidebar.radio("Choose a model:", ("GPT-4", "GPT-3.5-16k", "GPT-3.5 (not recommended)"))
69
+ if model == "GPT-4":
70
+ st.session_state.model_name = "gpt-4"
71
+ elif model == "GPT-3.5-16k":
72
+ st.session_state.model_name = "gpt-3.5-turbo-16k"
73
+ elif model == "GPT-3.5 (not recommended)":
74
+ st.session_state.model_name = "gpt-3.5-turbo"
75
+ else:
76
+ raise NotImplementedError
77
+
78
+ return ChatOpenAI(
79
+ temperature=0,
80
+ openai_api_key=st.session_state["openai_api_key"],
81
+ model_name=st.session_state.model_name,
82
+ streaming=True
83
+ )
84
+
85
+
86
+ def main():
87
+ init_page()
88
+ init_messages()
89
+ tools = [get_search_ddg_tool(), get_fetch_page_tool()]
90
+
91
+ """
92
+ This is a sample Web Browsing Agent app that uses LangChain's `OpenAIFunctionsAgent` and Streamlit's `StreamlitCallbackHandler`. Please refer to the code for more details at https://github.com/watanabe3tipapa/web_sge_agent.
93
+ """
94
+
95
+ for msg in st.session_state.messages:
96
+ st.chat_message(msg["role"]).write(msg["content"])
97
+
98
+ if not st.session_state["openai_api_key"]:
99
+ st.info("Please add your OpenAI API key to continue.")
100
+ st.stop()
101
+ else:
102
+ llm = select_model()
103
+
104
+ if st.session_state["langsmith_api_key"]:
105
+ os.environ['LANGCHAIN_TRACING_V2'] = 'true'
106
+ os.environ['LANGCHAIN_ENDPOINT'] = "https://api.smith.langchain.com"
107
+ os.environ['LANGCHAIN_API_KEY'] = st.session_state["langsmith_api_key"]
108
+
109
+ if prompt := st.chat_input(placeholder="Who won the 2023 FIFA Women's World Cup?"):
110
+ st.session_state.messages.append({"role": "user", "content": prompt})
111
+ st.chat_message("user").write(prompt)
112
+
113
+ search_agent = initialize_agent(
114
+ agent='openai-functions',
115
+ tools=tools,
116
+ llm=llm,
117
+ max_iteration=5,
118
+ agent_kwargs={
119
+ "system_message": SystemMessage(content=CUSTOM_SYSTEM_PROMPT)
120
+ }
121
+ )
122
+ with st.chat_message("assistant"):
123
+ st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
124
+ response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
125
+ st.session_state.messages.append({"role": "assistant", "content": response})
126
+ st.write(response)
127
+
128
+ if __name__ == '__main__':
129
+ main()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ openai==0.28.0
2
+ tiktoken==0.4.0
3
+ langchain==0.0.279
4
+ html2text==2020.1.16
5
+ beautifulsoup4==4.12.2
6
+ duckduckgo-search==3.8.5
7
+ readability-lxml==0.8.1