Spaces:
Sleeping
Sleeping
Commit
ยท
f86b5db
1
Parent(s):
97e1dbe
import os
Browse files- LC_chat_w_search.py +0 -44
- LC_non-streaming.py +0 -24
- LC_streaming.py +0 -36
- app.py +2 -0
- app_hello_world.py +0 -5
- app_pword.py +0 -64
- lc_streaming_nopassword.py +0 -33
LC_chat_w_search.py
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
# LC_chat_w_search.py
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from langchain.callbacks import StreamlitCallbackHandler
|
| 5 |
-
from langchain.chat_models import ChatOpenAI
|
| 6 |
-
from langchain.tools import DuckDuckGoSearchRun
|
| 7 |
-
|
| 8 |
-
with st.sidebar:
|
| 9 |
-
openai_api_key = st.text_input("OpenAI API Key", key="langchain_search_api_key_openai", type="password")
|
| 10 |
-
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
|
| 11 |
-
"[View the source code](https://github.com/streamlit/llm-examples/blob/main/pages/2_Chat_with_search.py)"
|
| 12 |
-
"[](https://codespaces.new/streamlit/llm-examples?quickstart=1)"
|
| 13 |
-
|
| 14 |
-
st.title("๐ LangChain - Chat with search")
|
| 15 |
-
|
| 16 |
-
"""
|
| 17 |
-
In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
|
| 18 |
-
Try more LangChain ๐ค Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
|
| 19 |
-
"""
|
| 20 |
-
|
| 21 |
-
if "messages" not in st.session_state:
|
| 22 |
-
st.session_state["messages"] = [
|
| 23 |
-
{"role": "assistant", "content": "Hi, I'm a chatbot who can search the web. How can I help you?"}
|
| 24 |
-
]
|
| 25 |
-
|
| 26 |
-
for msg in st.session_state.messages:
|
| 27 |
-
st.chat_message(msg["role"]).write(msg["content"])
|
| 28 |
-
|
| 29 |
-
if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
|
| 30 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 31 |
-
st.chat_message("user").write(prompt)
|
| 32 |
-
|
| 33 |
-
if not openai_api_key:
|
| 34 |
-
st.info("Please add your OpenAI API key to continue.")
|
| 35 |
-
st.stop()
|
| 36 |
-
|
| 37 |
-
llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, streaming=True)
|
| 38 |
-
search = DuckDuckGoSearchRun(name="Search")
|
| 39 |
-
search_agent = initialize_agent([search], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
|
| 40 |
-
with st.chat_message("assistant"):
|
| 41 |
-
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 42 |
-
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 43 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 44 |
-
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LC_non-streaming.py
DELETED
|
@@ -1,24 +0,0 @@
|
|
| 1 |
-
# working
|
| 2 |
-
# Non-streaming chat.
|
| 3 |
-
# langchain llm, NOT CHAT_LLM
|
| 4 |
-
|
| 5 |
-
import streamlit as st
|
| 6 |
-
from langchain.llms import OpenAI
|
| 7 |
-
import os
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
st.title('๐ฆ๐ Dev App')
|
| 11 |
-
|
| 12 |
-
openai_api_key = os.environ["OPENAI_API_KEY"] #st.sidebar.text_input('OpenAI API Key')
|
| 13 |
-
|
| 14 |
-
def generate_response(input_text):
|
| 15 |
-
llm = OpenAI(temperature=0.0, openai_api_key=openai_api_key)
|
| 16 |
-
st.info(llm(input_text))
|
| 17 |
-
|
| 18 |
-
with st.form('my_form'):
|
| 19 |
-
text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
|
| 20 |
-
submitted = st.form_submit_button('Submit')
|
| 21 |
-
if not openai_api_key.startswith('sk-'):
|
| 22 |
-
st.warning('Please enter your OpenAI API key!', icon='โ ')
|
| 23 |
-
if submitted and openai_api_key.startswith('sk-'):
|
| 24 |
-
generate_response(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LC_streaming.py
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
# LC_Streaming.py
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from langchain.callbacks.base import BaseCallbackHandler
|
| 5 |
-
from langchain.chat_models import ChatOpenAI
|
| 6 |
-
from langchain.schema import HumanMessage
|
| 7 |
-
import streamlit as st
|
| 8 |
-
|
| 9 |
-
class StreamHandler(BaseCallbackHandler):
|
| 10 |
-
def __init__(self, container, initial_text="", display_method='markdown'):
|
| 11 |
-
self.container = container
|
| 12 |
-
self.text = initial_text
|
| 13 |
-
self.display_method = display_method
|
| 14 |
-
|
| 15 |
-
def on_llm_new_token(self, token: str, **kwargs) -> None:
|
| 16 |
-
self.text += token + "/"
|
| 17 |
-
display_function = getattr(self.container, self.display_method, None)
|
| 18 |
-
if display_function is not None:
|
| 19 |
-
display_function(self.text)
|
| 20 |
-
else:
|
| 21 |
-
raise ValueError(f"Invalid display_method: {self.display_method}")
|
| 22 |
-
|
| 23 |
-
query = st.text_input("input your query", value="Tell me a joke")
|
| 24 |
-
ask_button = st.button("ask")
|
| 25 |
-
|
| 26 |
-
st.markdown("### streaming box")
|
| 27 |
-
chat_box = st.empty()
|
| 28 |
-
stream_handler = StreamHandler(chat_box, display_method='write')
|
| 29 |
-
chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
|
| 30 |
-
|
| 31 |
-
st.markdown("### together box")
|
| 32 |
-
|
| 33 |
-
if query and ask_button:
|
| 34 |
-
response = chat([HumanMessage(content=query)])
|
| 35 |
-
llm_response = response.content
|
| 36 |
-
st.markdown(llm_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -6,6 +6,8 @@ from langchain.callbacks.base import BaseCallbackHandler
|
|
| 6 |
from langchain.chat_models import ChatOpenAI
|
| 7 |
from langchain.schema import HumanMessage
|
| 8 |
import streamlit as st
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class StreamHandler(BaseCallbackHandler):
|
| 11 |
def __init__(self, container, initial_text="", display_method='markdown'):
|
|
|
|
| 6 |
from langchain.chat_models import ChatOpenAI
|
| 7 |
from langchain.schema import HumanMessage
|
| 8 |
import streamlit as st
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
|
| 12 |
class StreamHandler(BaseCallbackHandler):
|
| 13 |
def __init__(self, container, initial_text="", display_method='markdown'):
|
app_hello_world.py
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
|
| 3 |
-
x = st.slider('Select a value')
|
| 4 |
-
st.write(x, 'squared is', x * x)
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app_pword.py
DELETED
|
@@ -1,64 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
from langchain.callbacks.base import BaseCallbackHandler
|
| 3 |
-
from langchain.chat_models import ChatOpenAI
|
| 4 |
-
from langchain.schema import HumanMessage
|
| 5 |
-
import streamlit as st
|
| 6 |
-
|
| 7 |
-
class StreamHandler(BaseCallbackHandler):
|
| 8 |
-
def __init__(self, container, initial_text="", display_method='markdown'):
|
| 9 |
-
self.container = container
|
| 10 |
-
self.text = initial_text
|
| 11 |
-
self.display_method = display_method
|
| 12 |
-
|
| 13 |
-
def on_llm_new_token(self, token: str, **kwargs) -> None:
|
| 14 |
-
self.text += token # + "/"
|
| 15 |
-
display_function = getattr(self.container, self.display_method, None)
|
| 16 |
-
if display_function is not None:
|
| 17 |
-
display_function(self.text)
|
| 18 |
-
else:
|
| 19 |
-
raise ValueError(f"Invalid display_method: {self.display_method}")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
import streamlit as st
|
| 23 |
-
|
| 24 |
-
def check_password():
|
| 25 |
-
"""Returns `True` if the user had the correct password."""
|
| 26 |
-
|
| 27 |
-
def password_entered():
|
| 28 |
-
"""Checks whether a password entered by the user is correct."""
|
| 29 |
-
if st.session_state["password"] == os.environ["USER_PWORD"]:
|
| 30 |
-
st.session_state["password_correct"] = True
|
| 31 |
-
del st.session_state["password"] # don't store password
|
| 32 |
-
else:
|
| 33 |
-
st.session_state["password_correct"] = False
|
| 34 |
-
|
| 35 |
-
if "password_correct" not in st.session_state:
|
| 36 |
-
# First run, show input for password.
|
| 37 |
-
st.text_input(
|
| 38 |
-
"Password", type="password", on_change=password_entered, key="password"
|
| 39 |
-
)
|
| 40 |
-
return False
|
| 41 |
-
elif not st.session_state["password_correct"]:
|
| 42 |
-
# Password not correct, show input + error.
|
| 43 |
-
st.text_input(
|
| 44 |
-
"Password", type="password", on_change=password_entered, key="password"
|
| 45 |
-
)
|
| 46 |
-
st.error("๐ Password incorrect")
|
| 47 |
-
return False
|
| 48 |
-
else:
|
| 49 |
-
# Password correct.
|
| 50 |
-
return True
|
| 51 |
-
|
| 52 |
-
if check_password():
|
| 53 |
-
query = st.text_input("Input your research question", value="How do biases in AI student evaluations compare to documented biases in human evaluations?")
|
| 54 |
-
ask_button = st.button("ask")
|
| 55 |
-
|
| 56 |
-
st.markdown("### GPT-3.5 response")
|
| 57 |
-
chat_box = st.empty()
|
| 58 |
-
stream_handler = StreamHandler(chat_box, display_method='write')
|
| 59 |
-
chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
|
| 60 |
-
|
| 61 |
-
#st.markdown("### together box")
|
| 62 |
-
|
| 63 |
-
if query and ask_button:
|
| 64 |
-
response = chat([HumanMessage(content=query)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lc_streaming_nopassword.py
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 1 |
-
from langchain.callbacks.base import BaseCallbackHandler
|
| 2 |
-
from langchain.chat_models import ChatOpenAI
|
| 3 |
-
from langchain.schema import HumanMessage
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
class StreamHandler(BaseCallbackHandler):
|
| 7 |
-
def __init__(self, container, initial_text="", display_method='markdown'):
|
| 8 |
-
self.container = container
|
| 9 |
-
self.text = initial_text
|
| 10 |
-
self.display_method = display_method
|
| 11 |
-
|
| 12 |
-
def on_llm_new_token(self, token: str, **kwargs) -> None:
|
| 13 |
-
self.text += token # + "/"
|
| 14 |
-
display_function = getattr(self.container, self.display_method, None)
|
| 15 |
-
if display_function is not None:
|
| 16 |
-
display_function(self.text)
|
| 17 |
-
else:
|
| 18 |
-
raise ValueError(f"Invalid display_method: {self.display_method}")
|
| 19 |
-
|
| 20 |
-
query = st.text_input("Input your research question", value="How do biases in AI student evaluations compare to documented biases in human evaluations?")
|
| 21 |
-
ask_button = st.button("ask")
|
| 22 |
-
|
| 23 |
-
st.markdown("### GPT-3.5 response")
|
| 24 |
-
chat_box = st.empty()
|
| 25 |
-
stream_handler = StreamHandler(chat_box, display_method='write')
|
| 26 |
-
chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
|
| 27 |
-
|
| 28 |
-
#st.markdown("### together box")
|
| 29 |
-
|
| 30 |
-
if query and ask_button:
|
| 31 |
-
response = chat([HumanMessage(content=query)])
|
| 32 |
-
# llm_response = response.content
|
| 33 |
-
# st.markdown(llm_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|