Skip to content

Commit e8759c5

Browse files
committed
Restructure to a packaged app
1 parent 7087963 commit e8759c5

File tree

7 files changed

+133
-93
lines changed

7 files changed

+133
-93
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/examples/langgraph-chatbot-demo/pyproject.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ opentelemetry-instrumentation-vertexai = { git = "https://github.com/aabmass/ope
2828
opentelemetry-exporter-gcp-logging = { git = "https://github.com/DylanRussell/opentelemetry-operations-python.git", subdirectory = "opentelemetry-exporter-gcp-logging", branch = "logging_exporter" }
2929

3030
[dependency-groups]
31-
dev = [
32-
"ipython>=8.18.1",
33-
"ruff>=0.9.2",
34-
]
31+
dev = ["ipython>=8.18.1", "ruff>=0.9.2"]
32+
33+
[build-system]
34+
requires = ["hatchling"]
35+
build-backend = "hatchling.build"

instrumentation-genai/opentelemetry-instrumentation-vertexai/examples/langgraph-chatbot-demo/run_streamlit.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
# /// script
2+
# requires-python = ">=3.13"
3+
# dependencies = [
4+
# "langgraph-chatbot-demo",
5+
# ]
6+
#
7+
# [tool.uv.sources]
8+
# langgraph-chatbot-demo = { path = "." }
9+
# ///
10+
111
import os
12+
import importlib.util
213
import subprocess
314

415
import google.auth
@@ -27,13 +38,18 @@ def setenv_default(k: str, v: str) -> None:
2738
f"authorization=Bearer {creds.token},x-goog-user-project={project_id}",
2839
)
2940

30-
print(os.environ)
41+
langchain_app_spec = importlib.util.find_spec(
42+
"langgraph_chatbot_demo.langchain_history"
43+
)
44+
if not (langchain_app_spec and langchain_app_spec.origin):
45+
raise Exception("Could not find langchain_history.py")
46+
3147
subprocess.run(
3248
[
3349
"opentelemetry-instrument",
3450
"streamlit",
3551
"run",
36-
"langchain_history.py",
52+
langchain_app_spec.origin,
3753
],
3854
check=True,
3955
)

instrumentation-genai/opentelemetry-instrumentation-vertexai/examples/langgraph-chatbot-demo/src/langgraph_chatbot_demo/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Adapated from https://github.com/streamlit/llm-examples/blob/main/pages/2_Chat_with_search.py"""
2+
3+
from os import environ
4+
5+
import streamlit as st
6+
from langchain.agents import AgentType, initialize_agent
7+
from langchain.callbacks import StreamlitCallbackHandler
8+
from langchain_community.tools import DuckDuckGoSearchRun
9+
from langchain_google_vertexai import ChatVertexAI
10+
11+
st.title("🔎 LangChain - Chat with search")
12+
13+
"""
14+
In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
15+
Try more LangChain 🤝 Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
16+
"""
17+
18+
if "messages" not in st.session_state:
19+
st.session_state["messages"] = [
20+
{
21+
"role": "assistant",
22+
"content": "Hi, I'm a chatbot who can search the web. How can I help you?",
23+
}
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(
30+
placeholder="Who won the Women's U.S. Open in 2018?"
31+
):
32+
st.session_state.messages.append({"role": "user", "content": prompt})
33+
st.chat_message("user").write(prompt)
34+
35+
llm = ChatVertexAI(
36+
model="gemini-1.5-flash",
37+
project=environ.get("GOOGLE_CLOUD_PROJECT", None),
38+
)
39+
search = DuckDuckGoSearchRun(name="Search")
40+
search_agent = initialize_agent(
41+
[search],
42+
llm,
43+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
44+
handle_parsing_errors=True,
45+
)
46+
with st.chat_message("assistant"):
47+
st_cb = StreamlitCallbackHandler(
48+
st.container(), expand_new_thoughts=False
49+
)
50+
response = search_agent.run(
51+
st.session_state.messages, callbacks=[st_cb]
52+
)
53+
st.session_state.messages.append(
54+
{"role": "assistant", "content": response}
55+
)
56+
st.write(response)

instrumentation-genai/opentelemetry-instrumentation-vertexai/examples/langgraph-chatbot-demo/uv.lock

Lines changed: 54 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)