mirror of
https://github.com/langchain-ai/streamlit-agent.git
synced 2026-07-01 09:25:05 -04:00
Update everything to langchain 0.1 (#45)
This commit is contained in:
Generated
+2021
-1835
File diff suppressed because it is too large
Load Diff
+7
-3
@@ -9,18 +9,22 @@ packages = [{include = "streamlit_agent"}]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.10,<4.0"
|
||||
langchain = {version = ">=0.0.252"}
|
||||
openai = "^0.27.8"
|
||||
langchain = {version = ">=0.1.0"}
|
||||
openai = ">=1.6.1"
|
||||
duckduckgo-search = "^3.8.3"
|
||||
pypdf = "^3.12.2"
|
||||
sentence-transformers = "^2.2.2"
|
||||
torch = ">=2.0.0, !=2.0.1"
|
||||
tabulate = "^0.9.0"
|
||||
streamlit-feedback = "^0.0.9"
|
||||
langchain-experimental = "^0.0.10"
|
||||
langchain-experimental = "^0.0.49"
|
||||
streamlit = ">=1.26"
|
||||
docarray = "^0.38.0"
|
||||
hnswlib = "^0.7.0"
|
||||
langchain-community = "^0.0.12"
|
||||
langchain-openai = "^0.0.2.post1"
|
||||
numexpr = "^2.8.8"
|
||||
langchainhub = "^0.1.14"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
black = "^23.3.0"
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from langchain.chains import LLMChain
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
||||
from langchain_openai import OpenAI
|
||||
import streamlit as st
|
||||
|
||||
st.set_page_config(page_title="StreamlitChatMessageHistory", page_icon="📖")
|
||||
@@ -49,8 +49,8 @@ for msg in msgs.messages:
|
||||
if prompt := st.chat_input():
|
||||
st.chat_message("human").write(prompt)
|
||||
# Note: new messages are saved to history automatically by Langchain during run
|
||||
response = llm_chain.run(prompt)
|
||||
st.chat_message("ai").write(response)
|
||||
response = llm_chain.invoke(prompt)
|
||||
st.chat_message("ai").write(response["text"])
|
||||
|
||||
# Draw the messages at the end, so newly generated ones show up immediately
|
||||
with view_messages:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from langchain.callbacks.base import BaseCallbackHandler
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.schema import ChatMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
import streamlit as st
|
||||
|
||||
|
||||
@@ -34,5 +34,5 @@ if prompt := st.chat_input():
|
||||
with st.chat_message("assistant"):
|
||||
stream_handler = StreamHandler(st.empty())
|
||||
llm = ChatOpenAI(openai_api_key=openai_api_key, streaming=True, callbacks=[stream_handler])
|
||||
response = llm(st.session_state.messages)
|
||||
response = llm.invoke(st.session_state.messages)
|
||||
st.session_state.messages.append(ChatMessage(role="assistant", content=response.content))
|
||||
|
||||
@@ -92,7 +92,7 @@ def playback_callbacks(
|
||||
# Return the agent's result
|
||||
for record in records:
|
||||
if record["callback_type"] == CallbackType.ON_AGENT_FINISH:
|
||||
return record["args"][0][0]["output"]
|
||||
return record["args"][0].return_values
|
||||
|
||||
return "[Missing Agent Result]"
|
||||
|
||||
@@ -107,16 +107,12 @@ class CapturingCallbackHandler(BaseCallbackHandler):
|
||||
with open(path, "wb") as file:
|
||||
pickle.dump(self._records, file)
|
||||
|
||||
def _append_record(
|
||||
self, type: str, args: tuple[Any, ...], kwargs: dict[str, Any]
|
||||
) -> None:
|
||||
def _append_record(self, type: str, args: tuple[Any, ...], kwargs: dict[str, Any]) -> None:
|
||||
time_now = time.time()
|
||||
time_delta = time_now - self._last_time if self._last_time is not None else 0
|
||||
self._last_time = time_now
|
||||
self._records.append(
|
||||
CallbackRecord(
|
||||
callback_type=type, args=args, kwargs=kwargs, time_delta=time_delta
|
||||
)
|
||||
CallbackRecord(callback_type=type, args=args, kwargs=kwargs, time_delta=time_delta)
|
||||
)
|
||||
|
||||
def on_llm_start(self, *args: Any, **kwargs: Any) -> None:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from langchain.agents import AgentType
|
||||
from langchain.agents import create_pandas_dataframe_agent
|
||||
from langchain_experimental.agents import create_pandas_dataframe_agent
|
||||
from langchain.callbacks import StreamlitCallbackHandler
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
import streamlit as st
|
||||
|
||||
@@ -2,14 +2,14 @@ from pathlib import Path
|
||||
|
||||
import streamlit as st
|
||||
|
||||
from langchain import SQLDatabase
|
||||
from langchain.agents import AgentType
|
||||
from langchain.agents import initialize_agent, Tool
|
||||
from langchain.callbacks import StreamlitCallbackHandler
|
||||
from langchain import hub
|
||||
from langchain.agents import AgentExecutor, Tool, create_react_agent
|
||||
from langchain.chains import LLMMathChain
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.utilities import DuckDuckGoSearchAPIWrapper
|
||||
from langchain_community.callbacks import StreamlitCallbackHandler
|
||||
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper, SQLDatabase
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_experimental.sql import SQLDatabaseChain
|
||||
from langchain_openai import OpenAI
|
||||
|
||||
from streamlit_agent.callbacks.capturing_callback_handler import playback_callbacks
|
||||
from streamlit_agent.clear_results import with_clear_container
|
||||
@@ -18,7 +18,7 @@ DB_PATH = (Path(__file__).parent / "Chinook.db").absolute()
|
||||
|
||||
SAVED_SESSIONS = {
|
||||
"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?": "leo.pickle",
|
||||
"What is the full name of the artist who recently released an album called "
|
||||
"What is the full name of the female artist who recently released an album called "
|
||||
"'The Storm Before the Calm' and are they in the FooBar database? If so, what albums of theirs "
|
||||
"are in the FooBar database?": "alanis.pickle",
|
||||
}
|
||||
@@ -66,7 +66,8 @@ tools = [
|
||||
]
|
||||
|
||||
# Initialize agent
|
||||
mrkl = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
|
||||
react_agent = create_react_agent(llm, tools, hub.pull("hwchase17/react"))
|
||||
mrkl = AgentExecutor(agent=react_agent, tools=tools)
|
||||
|
||||
with st.form(key="form"):
|
||||
if not enable_custom:
|
||||
@@ -87,6 +88,8 @@ if with_clear_container(submit_clicked):
|
||||
|
||||
answer_container = output_container.chat_message("assistant", avatar="🦜")
|
||||
st_callback = StreamlitCallbackHandler(answer_container)
|
||||
cfg = RunnableConfig()
|
||||
cfg["callbacks"] = [st_callback]
|
||||
|
||||
# If we've saved this question, play it back instead of actually running LangChain
|
||||
# (so that we don't exhaust our API calls unnecessarily)
|
||||
@@ -96,6 +99,6 @@ if with_clear_container(submit_clicked):
|
||||
print(f"Playing saved session: {session_path}")
|
||||
answer = playback_callbacks([st_callback], str(session_path), max_pause_time=2)
|
||||
else:
|
||||
answer = mrkl.run(user_input, callbacks=[st_callback])
|
||||
answer = mrkl.invoke({"input": user_input}, cfg)
|
||||
|
||||
answer_container.write(answer)
|
||||
answer_container.write(answer["output"])
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,9 +1,11 @@
|
||||
from langchain.agents import ConversationalChatAgent, AgentExecutor
|
||||
from langchain.callbacks import StreamlitCallbackHandler
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
||||
from langchain.tools import DuckDuckGoSearchRun
|
||||
from langchain_community.callbacks import StreamlitCallbackHandler
|
||||
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
||||
from langchain_community.tools import DuckDuckGoSearchRun
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
import streamlit as st
|
||||
|
||||
st.set_page_config(page_title="LangChain: Chat with search", page_icon="🦜")
|
||||
@@ -51,6 +53,8 @@ if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?")
|
||||
)
|
||||
with st.chat_message("assistant"):
|
||||
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
||||
response = executor(prompt, callbacks=[st_cb])
|
||||
cfg = RunnableConfig()
|
||||
cfg["callbacks"] = [st_cb]
|
||||
response = executor.invoke(prompt, cfg)
|
||||
st.write(response["output"])
|
||||
st.session_state.steps[str(len(msgs.messages) - 1)] = response["intermediate_steps"]
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from langchain.llms import OpenAI
|
||||
from langchain.callbacks import LangChainTracer
|
||||
from langchain.chains import ConversationChain
|
||||
from langchain.memory import ConversationBufferMemory
|
||||
from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
|
||||
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_core.tracers import LangChainTracer
|
||||
from langchain_core.tracers.run_collector import RunCollectorCallbackHandler
|
||||
from langchain_openai import OpenAI
|
||||
from langsmith import Client
|
||||
import streamlit as st
|
||||
from streamlit_feedback import streamlit_feedback
|
||||
import time
|
||||
|
||||
st.set_page_config(page_title="LangChain: Simple feedback", page_icon="🦜")
|
||||
st.title("🦜 LangChain: Simple feedback")
|
||||
@@ -25,6 +28,9 @@ if not langchain_api_key or not openai_api_key:
|
||||
langchain_endpoint = "https://api.smith.langchain.com"
|
||||
client = Client(api_url=langchain_endpoint, api_key=langchain_api_key)
|
||||
ls_tracer = LangChainTracer(project_name=project, client=client)
|
||||
run_collector = RunCollectorCallbackHandler()
|
||||
cfg = RunnableConfig()
|
||||
cfg["callbacks"] = [ls_tracer, run_collector]
|
||||
|
||||
msgs = StreamlitChatMessageHistory()
|
||||
memory = ConversationBufferMemory(chat_memory=msgs)
|
||||
@@ -43,13 +49,14 @@ for msg in msgs.messages:
|
||||
if input := st.chat_input(placeholder="Tell me a joke about a shark?"):
|
||||
st.chat_message("user").write(input)
|
||||
with st.chat_message("assistant"):
|
||||
response = llm_chain(input, callbacks=[ls_tracer], include_run_info=True)
|
||||
response = llm_chain.invoke(input, cfg)
|
||||
st.write(response["response"])
|
||||
st.session_state.last_run = response["__run"].run_id
|
||||
st.session_state.last_run = run_collector.traced_runs[0].id
|
||||
|
||||
|
||||
@st.cache_data(ttl="2h", show_spinner=False)
|
||||
def get_run_url(run_id):
|
||||
time.sleep(1)
|
||||
return client.read_run(run_id).url
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user