Tuana commited on
Commit
7a4f3b9
·
1 Parent(s): 5b37c89
Files changed (3) hide show
  1. app.py +11 -8
  2. utils/haystack.py +3 -3
  3. utils/ui.py +18 -4
app.py CHANGED
@@ -3,28 +3,32 @@ from json import JSONDecodeError
3
  import logging
4
 
5
  import streamlit as st
 
6
  from utils.haystack import run_agent, start_haystack
7
  from utils.ui import reset_results, set_initial_state, sidebar
8
- from utils.config import TWITTER_BEARER_TOKEN, SERPER_KEY, OPENAI_API_KEY
9
 
10
  set_initial_state()
11
 
12
  sidebar()
13
 
14
  st.write("# 👩 What would they have tweeted about this?")
 
 
 
 
 
15
 
16
- # if st.session_state.get("OPENAI_API_KEY"):
17
- if "last_k_tweets" not in st.session_state:
18
- st.session_state["last_k_tweets"] = st.slider("How many tweets should we retrieve?", value=10, step=10, max_value=100, min_value=10, on_change = reset_results)
19
 
20
- agent = start_haystack(openai_key=OPENAI_API_KEY, twitter_bearer=TWITTER_BEARER_TOKEN, serper_key=SERPER_KEY, last_k_tweets=st.session_state["last_k_tweets"])
21
  # st.session_state["api_keys_configured"] = True
22
 
23
  # Search bar
24
  question = st.text_input("Example: If the twitter account tuanacelik were to write a tweet in their style about climate change, what would it be?", on_change=reset_results)
25
  run_pressed = st.button("Generate tweet")
26
  # else:
27
- # st.write("Please provide your OpenAI Key to start using the application")
28
  # st.write("If you are using a smaller screen, open the sidebar from the top left to provide your OpenAI Key 🙌")
29
 
30
  # if st.session_state.get("api_keys_configured"):
@@ -46,5 +50,4 @@ if run_query and question:
46
 
47
  if st.session_state.result:
48
  result = st.session_state.result
49
- st.write(result["answers"][0].answer)
50
-
 
3
  import logging
4
 
5
  import streamlit as st
6
+
7
  from utils.haystack import run_agent, start_haystack
8
  from utils.ui import reset_results, set_initial_state, sidebar
9
+ from utils.config import TWITTER_BEARER_TOKEN, OPENAI_API_KEY, SERPER_KEY
10
 
11
  set_initial_state()
12
 
13
  sidebar()
14
 
15
  st.write("# 👩 What would they have tweeted about this?")
16
+ st.write("## About this App")
17
+ st.write("This app uses an [Agent](https://docs.haystack.deepset.ai/docs/agent) (using GPT-4) with a `WebSearch` and `TwitterRetriever` tool")
18
+ st.write("The `TwitterRetriever` is set to retrieve the latest 15 tweets by the given twitter username to construct an understanding of their style of tweeting")
19
+ st.write("### Instructions")
20
+ st.write("For best results, please provide a Twitter username as it appears on twietter. E.g.: dog_feelings 🐶")
21
 
22
+ # if st.session_state.get("OPENAI_API_KEY") and st.session_state.get("SERPER_KEY"):
 
 
23
 
24
+ agent = start_haystack(openai_key=OPENAI_API_KEY, twitter_bearer=TWITTER_BEARER_TOKEN, serper_key=SERPER_KEY)
25
  # st.session_state["api_keys_configured"] = True
26
 
27
  # Search bar
28
  question = st.text_input("Example: If the twitter account tuanacelik were to write a tweet in their style about climate change, what would it be?", on_change=reset_results)
29
  run_pressed = st.button("Generate tweet")
30
  # else:
31
+ # st.write("Please provide your OpenAI and SerperDev Keys to start using the application")
32
  # st.write("If you are using a smaller screen, open the sidebar from the top left to provide your OpenAI Key 🙌")
33
 
34
  # if st.session_state.get("api_keys_configured"):
 
50
 
51
  if st.session_state.result:
52
  result = st.session_state.result
53
+ st.write(result["answers"][0].answer)
 
utils/haystack.py CHANGED
@@ -6,7 +6,7 @@ from haystack.nodes import PromptNode, PromptTemplate, WebRetriever
6
  from haystack.pipelines import WebQAPipeline
7
 
8
 
9
- def start_haystack(openai_key, twitter_bearer, serper_key, last_k_tweets):
10
  prompt_node = PromptNode(
11
  "text-davinci-003",
12
  default_prompt_template=PromptTemplate(prompt="./prompts/lfqa.yaml"),
@@ -17,9 +17,9 @@ def start_haystack(openai_key, twitter_bearer, serper_key, last_k_tweets):
17
  web_retriever = WebRetriever(api_key=serper_key, top_search_results=2, mode="preprocessed_documents")
18
  web_pipeline = WebQAPipeline(retriever=web_retriever, prompt_node=prompt_node)
19
 
20
- twitter_retriver = TwitterRetriever(bearer_token=twitter_bearer, last_k_tweets=last_k_tweets)
21
 
22
- pn = PromptNode(model_name_or_path="gpt-4", api_key=openai_key, stop_words=["Observation:"])
23
  agent = Agent(prompt_node=pn, prompt_template="./prompts/twitter_agent.yaml")
24
 
25
  tweet_retriver_tool = Tool(name="TwitterRetriever", pipeline_or_node=twitter_retriver,
 
6
  from haystack.pipelines import WebQAPipeline
7
 
8
 
9
+ def start_haystack(openai_key, twitter_bearer, serper_key):
10
  prompt_node = PromptNode(
11
  "text-davinci-003",
12
  default_prompt_template=PromptTemplate(prompt="./prompts/lfqa.yaml"),
 
17
  web_retriever = WebRetriever(api_key=serper_key, top_search_results=2, mode="preprocessed_documents")
18
  web_pipeline = WebQAPipeline(retriever=web_retriever, prompt_node=prompt_node)
19
 
20
+ twitter_retriver = TwitterRetriever(bearer_token=twitter_bearer, last_k_tweets=15)
21
 
22
+ pn = PromptNode(model_name_or_path="gpt-4", api_key=openai_key, stop_words=["Observation:"], max_length=400)
23
  agent = Agent(prompt_node=pn, prompt_template="./prompts/twitter_agent.yaml")
24
 
25
  tweet_retriver_tool = Tool(name="TwitterRetriever", pipeline_or_node=twitter_retriver,
utils/ui.py CHANGED
@@ -16,6 +16,9 @@ def reset_results(*args):
16
  # def set_openai_api_key(api_key: str):
17
  # st.session_state["OPENAI_API_KEY"] = api_key
18
 
 
 
 
19
  def sidebar():
20
  with st.sidebar:
21
  image = Image.open('logo/haystack-logo-colored.png')
@@ -27,12 +30,12 @@ def sidebar():
27
 
28
  st.markdown(
29
  "## How to use\n"
30
- # "1. Enter your [OpenAI API key](https://platform.openai.com/account/api-keys) below\n"
31
  "1. Enter a query that includes a twitter username and be descriptive about wanting a tweet as a result.\n"
32
  "2. Enjoy 🤗\n"
33
  )
34
 
35
- # api_key_input = st.text_input(
36
  # "OpenAI API Key",
37
  # type="password",
38
  # placeholder="Paste your OpenAI API key here (sk-...)",
@@ -40,8 +43,19 @@ def sidebar():
40
  # value=st.session_state.get("OPENAI_API_KEY", ""),
41
  # )
42
 
43
- # if api_key_input:
44
- # set_openai_api_key(api_key_input)
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  st.markdown("---")
47
  st.markdown(
 
16
  # def set_openai_api_key(api_key: str):
17
  # st.session_state["OPENAI_API_KEY"] = api_key
18
 
19
+ # def set_serper_dev_key(api_key: str):
20
+ # st.session_state["SERPER_KEY"] = api_key
21
+
22
  def sidebar():
23
  with st.sidebar:
24
  image = Image.open('logo/haystack-logo-colored.png')
 
30
 
31
  st.markdown(
32
  "## How to use\n"
33
+ # "1. Enter your [OpenAI API](https://platform.openai.com/account/api-keys) and [SerperDev API](https://serper.dev/) keys below\n"
34
  "1. Enter a query that includes a twitter username and be descriptive about wanting a tweet as a result.\n"
35
  "2. Enjoy 🤗\n"
36
  )
37
 
38
+ # openai_api_key_input = st.text_input(
39
  # "OpenAI API Key",
40
  # type="password",
41
  # placeholder="Paste your OpenAI API key here (sk-...)",
 
43
  # value=st.session_state.get("OPENAI_API_KEY", ""),
44
  # )
45
 
46
+ # serper_api_key_input = st.text_input(
47
+ # "SerperDev API Key",
48
+ # type="password",
49
+ # placeholder="Paste your SerperDev API key here (sk-...)",
50
+ # help="You can get your API key from https://serper.dev.",
51
+ # value=st.session_state.get("SERPER_KEY", ""),
52
+ # )
53
+
54
+ # if openai_api_key_input:
55
+ # set_openai_api_key(openai_api_key_input)
56
+
57
+ # if serper_api_key_input:
58
+ # set_serper_dev_key(serper_api_key_input)
59
 
60
  st.markdown("---")
61
  st.markdown(