Spaces:
Sleeping
Sleeping
Commit
Β·
8dab0c7
1
Parent(s):
dcd0193
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from langchain.agents import AgentType
|
| 5 |
+
from langchain.agents import initialize_agent
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
+
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
| 9 |
+
from langchain.schema import SystemMessage
|
| 10 |
+
from langchain.prompts import MessagesPlaceholder
|
| 11 |
+
|
| 12 |
+
from app.tools import StoriesTool
|
| 13 |
+
from app.tools import CommentsTool
|
| 14 |
+
from app.tools import ContentTool
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
async def generate_response(question):
|
| 18 |
+
result = await open_ai_agent.arun(question)
|
| 19 |
+
return result
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
st.set_page_config(page_title="NewsNerd HackerBot π€π°")
|
| 23 |
+
st.title("NewsNerd HackerBot π€π°")
|
| 24 |
+
stop = False
|
| 25 |
+
|
| 26 |
+
with st.sidebar:
|
| 27 |
+
if 'OPENAI_API_KEY' in st.secrets:
|
| 28 |
+
st.success("OPENAI_API_KEY already provided!", icon='β
')
|
| 29 |
+
openai_api_key = st.secrets['OPENAI_API_KEY']
|
| 30 |
+
else:
|
| 31 |
+
openai_api_key = st.text_input('Enter your OPENAI_API_KEY: ', type='password')
|
| 32 |
+
if not openai_api_key:
|
| 33 |
+
st.warning('Please, enter your OPENAI_API_KEY', icon='β οΈ')
|
| 34 |
+
stop = True
|
| 35 |
+
else:
|
| 36 |
+
st.success('Ask Hacker News whatever you want!', icon='π')
|
| 37 |
+
|
| 38 |
+
st.markdown("""
|
| 39 |
+
# **Greetings, Digital Explorer!**
|
| 40 |
+
|
| 41 |
+
Are you fatigued from navigating the expansive digital realm in search of your daily tech tales
|
| 42 |
+
and hacker happenings? Fear not, for your cyber-savvy companion has descended upon the scene β
|
| 43 |
+
behold the extraordinary **NewsNerd HackerBot**!
|
| 44 |
+
""")
|
| 45 |
+
|
| 46 |
+
if stop:
|
| 47 |
+
st.stop()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
tools = [StoriesTool(), CommentsTool(), ContentTool()]
|
| 51 |
+
msgs = StreamlitChatMessageHistory(key="langchain_messages")
|
| 52 |
+
memory = ConversationBufferMemory(chat_memory=msgs, return_messages=True)
|
| 53 |
+
system_message = SystemMessage(content="""
|
| 54 |
+
You are the Singularity Incarnation of Hacker News.
|
| 55 |
+
The human will ask you for information about Hacker News.
|
| 56 |
+
If you can't find any information about the question asked
|
| 57 |
+
or the result is incomplete, apologise to the human and ask him if
|
| 58 |
+
you can help him with something else.
|
| 59 |
+
If the human asks you to show him stories, do it using a markdown table.
|
| 60 |
+
The markdown table has the following format:
|
| 61 |
+
|
| 62 |
+
story_id | title | url | score
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
if len(msgs.messages) == 0:
|
| 66 |
+
msgs.add_ai_message("Greetings, human, I am the Incarnation of Hacker News. How can I help you?")
|
| 67 |
+
|
| 68 |
+
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613", openai_api_key=openai_api_key)
|
| 69 |
+
agent_kwargs = {
|
| 70 |
+
"system_message": system_message,
|
| 71 |
+
"extra_prompt_messages": [MessagesPlaceholder(variable_name="history")]
|
| 72 |
+
}
|
| 73 |
+
open_ai_agent = initialize_agent(tools,
|
| 74 |
+
llm,
|
| 75 |
+
agent=AgentType.OPENAI_FUNCTIONS,
|
| 76 |
+
agent_kwargs=agent_kwargs,
|
| 77 |
+
verbose=True,
|
| 78 |
+
memory=memory
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
for msg in msgs.messages:
|
| 82 |
+
st.chat_message(msg.type).write(msg.content)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if prompt := st.chat_input(disabled=not openai_api_key):
|
| 86 |
+
st.chat_message("human").write(prompt)
|
| 87 |
+
with st.spinner("Thinking ..."):
|
| 88 |
+
response = asyncio.run(generate_response(prompt))
|
| 89 |
+
st.chat_message("ai").write(response)
|