Skip to content

Commit

Permalink
Clean llm-app, and make streamlit ui more visible (#6528)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 32d78c9faaa6482f3615bef0a98d8a93629aebdb
  • Loading branch information
szymondudycz authored and Manul from Pathway committed May 31, 2024
1 parent 0543cbe commit 52b6b5a
Show file tree
Hide file tree
Showing 42 changed files with 741 additions and 146 deletions.
7 changes: 0 additions & 7 deletions .dockerignore

This file was deleted.

27 changes: 0 additions & 27 deletions Dockerfile

This file was deleted.

14 changes: 0 additions & 14 deletions docker-compose.yml

This file was deleted.

Empty file removed examples/README.md
Empty file.
1 change: 0 additions & 1 deletion examples/data/documents_extra.jsonl

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file removed examples/pipelines/__init__.py
Empty file.
8 changes: 7 additions & 1 deletion examples/pipelines/alert/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Set your env variables in the .env file placed in this directory or in the root
OPENAI_API_KEY=sk-...
SLACK_ALERT_CHANNEL_ID= # If unset, alerts will be printed to the terminal
SLACK_ALERT_TOKEN=
PATHWAY_DATA_DIR= # If unset, defaults to ../../data/magic-cola/live/
PATHWAY_DATA_DIR= # If unset, defaults to ./data/live/
PATHWAY_PERSISTENT_STORAGE= # Set this variable if you want to use caching
```

Expand Down Expand Up @@ -65,3 +65,9 @@ curl --data '{
"query": "When does the magic cola campaign start? Alert me if the start date changes."
}' http://localhost:8080/ | jq
```

or use the Streamlit UI. Run:
```bash
streamlit run ui/server.py --server.port 8501 --server.address 0.0.0.0
```
and then you can access the UI at `0.0.0.0:8501`.
2 changes: 1 addition & 1 deletion examples/pipelines/alert/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def decision_to_bool(decision: str) -> bool:

def run(
*,
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "../../data/magic-cola/live/"),
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "./data/live/"),
api_key: str = os.environ.get("OPENAI_API_KEY", ""),
host: str = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "0.0.0.0"),
port: int = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", "8080")),
Expand Down
File renamed without changes.
8 changes: 7 additions & 1 deletion examples/pipelines/contextful/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Set your env variables in the .env file placed in this directory or in the root

```bash
OPENAI_API_KEY=sk-...
PATHWAY_DATA_DIR= # If unset, defaults to ../../data/pathway-docs/
PATHWAY_DATA_DIR= # If unset, defaults to ./data/
PATHWAY_PERSISTENT_STORAGE= # Set this variable if you want to use caching
```

Expand Down Expand Up @@ -44,3 +44,9 @@ curl --data '{
"query": "How to connect to Kafka in Pathway?"
}' http://localhost:8080/ | jq
```

or use the Streamlit UI. Run:
```bash
streamlit run ui/server.py --server.port 8501 --server.address 0.0.0.0
```
and then you can access the UI at `0.0.0.0:8501`.
2 changes: 1 addition & 1 deletion examples/pipelines/contextful/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class QueryInputSchema(pw.Schema):

def run(
*,
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "../../data/pathway-docs/"),
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "./data/"),
api_key: str = os.environ.get("OPENAI_API_KEY", ""),
host: str = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "0.0.0.0"),
port: int = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", "8080")),
Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions examples/pipelines/contextful/ui/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

import requests
import streamlit as st
from dotenv import load_dotenv

with st.sidebar:
st.markdown(
"[View the source code on GitHub](https://github.com/pathwaycom/llm-app)"
)

# Load environment variables
load_dotenv()
api_host = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "127.0.0.1")
api_port = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", 8080))


# Streamlit UI elements
st.title("LLM App")


# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])


# React to user input
if prompt := st.chat_input("How can I help you today?"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)

# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})

url = f"http://{api_host}:{api_port}/"
data = {"query": prompt, "user": "user"}

response = requests.post(url, json=data)

if response.status_code == 200:
response = response.json()
with st.chat_message("assistant"):
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.error(
f"Failed to send data to Discounts API. Status code: {response.status_code}"
)
8 changes: 7 additions & 1 deletion examples/pipelines/contextful_geometric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Set your env variables in the .env file placed in this directory or in the root

```bash
OPENAI_API_KEY=sk-...
PATHWAY_DATA_DIR= # If unset, defaults to ../../data/pathway-docs/
PATHWAY_DATA_DIR= # If unset, defaults to ./data/
PATHWAY_PERSISTENT_STORAGE= # Set this variable if you want to use caching
```

Expand Down Expand Up @@ -49,3 +49,9 @@ curl --data '{
"query": "How to connect to Kafka in Pathway?"
}' http://localhost:8080/ | jq
```

or use the Streamlit UI. Run:
```bash
streamlit run ui/server.py --server.port 8501 --server.address 0.0.0.0
```
and then you can access the UI at `0.0.0.0:8501`.
2 changes: 1 addition & 1 deletion examples/pipelines/contextful_geometric/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class QueryInputSchema(pw.Schema):

def run(
*,
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "./examples/data/pathway-docs/"),
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "./data/"),
api_key: str = os.environ.get("OPENAI_API_KEY", ""),
host: str = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "0.0.0.0"),
port: int = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", "8080")),
Expand Down
415 changes: 415 additions & 0 deletions examples/pipelines/contextful_geometric/data/pathway-docs.jsonl

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions examples/pipelines/contextful_geometric/ui/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

import requests
import streamlit as st
from dotenv import load_dotenv

with st.sidebar:
st.markdown(
"[View the source code on GitHub](https://github.com/pathwaycom/llm-app)"
)

# Load environment variables
load_dotenv()
api_host = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "127.0.0.1")
api_port = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", 8080))


# Streamlit UI elements
st.title("LLM App")


# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])


# React to user input
if prompt := st.chat_input("How can I help you today?"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)

# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})

url = f"http://{api_host}:{api_port}/"
data = {"query": prompt, "user": "user"}

response = requests.post(url, json=data)

if response.status_code == 200:
response = response.json()
with st.chat_message("assistant"):
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.error(
f"Failed to send data to Discounts API. Status code: {response.status_code}"
)
11 changes: 4 additions & 7 deletions examples/pipelines/contextful_parsing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Set your env variables in the .env file placed in this directory or in the root

```bash
OPENAI_API_KEY=sk-...
PATHWAY_DATA_DIR= # If unset, defaults to ../../data/finance/
PATHWAY_DATA_DIR= # If unset, defaults to ./data/
PATHWAY_PERSISTENT_STORAGE= # Set this variable if you want to use caching
```

Expand Down Expand Up @@ -42,11 +42,8 @@ curl --data '{
}' http://localhost:8080/ | jq
```

Or start streamlit UI:

First go to `ui` directory with `cd ui/`
and run:

or use the Streamlit UI. Run:
```bash
streamlit run server.py
streamlit run ui/server.py --server.port 8501 --server.address 0.0.0.0
```
and then you can access the UI at `0.0.0.0:8501`.
13 changes: 2 additions & 11 deletions examples/pipelines/contextful_parsing/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@
for documents in the corpus. A prompt is built from the relevant documentations pages
and sent to the OpenAI GPT-4 chat service for processing.
Usage:
In the root of this repository run:
`poetry run ./run_examples.py unstructured`
or, if all dependencies are managed manually rather than using poetry
`python examples/pipelines/unstructured/app.py`
You can also run this example directly in the environment with llm_app installed.
In another terminal, navigate to `examples/pipelines/unstructured/ui` and run
`streamlit run server.py`. You can interact with the app at `localhost:8501`
Please check the README.md in this directory for how-to-run instructions.
"""

import os
Expand All @@ -45,7 +36,7 @@ class QueryInputSchema(pw.Schema):

def run(
*,
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "../../data/finance/"),
data_dir: str = os.environ.get("PATHWAY_DATA_DIR", "./data/"),
api_key: str = os.environ.get("OPENAI_API_KEY", ""),
host: str = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "0.0.0.0"),
port: int = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", "8080")),
Expand Down
6 changes: 6 additions & 0 deletions examples/pipelines/contextful_s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ curl --data '{
"query": "How to connect to Kafka in Pathway?"
}' http://localhost:8080/ | jq
```

or use the Streamlit UI. Run:
```bash
streamlit run ui/server.py --server.port 8501 --server.address 0.0.0.0
```
and then you can access the UI at `0.0.0.0:8501`.
11 changes: 1 addition & 10 deletions examples/pipelines/contextful_s3/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@
for documents in the corpus. A prompt is built from the relevant documentations pages
and sent to the OpenAI chat service for processing.
Usage:
In the root of this repository run:
`poetry run ./run_examples.py contextful-s3`
or, if all dependencies are managed manually rather than using poetry
`python examples/pipelines/contextful_s3/app.py`
You can also run this example directly in the environment with llm_app installed.
To call the REST API:
curl --data '{"user": "user", "query": "How to connect to Kafka in Pathway?"}' http://localhost:8080/ | jq
Please check the README.md in this directory for how-to-run instructions.
"""

import os
Expand Down
54 changes: 54 additions & 0 deletions examples/pipelines/contextful_s3/ui/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

import requests
import streamlit as st
from dotenv import load_dotenv

with st.sidebar:
st.markdown(
"[View the source code on GitHub](https://github.com/pathwaycom/llm-app)"
)

# Load environment variables
load_dotenv()
api_host = os.environ.get("PATHWAY_REST_CONNECTOR_HOST", "127.0.0.1")
api_port = int(os.environ.get("PATHWAY_REST_CONNECTOR_PORT", 8080))


# Streamlit UI elements
st.title("LLM App")


# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])


# React to user input
if prompt := st.chat_input("How can I help you today?"):
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)

# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})

url = f"http://{api_host}:{api_port}/"
data = {"query": prompt, "user": "user"}

response = requests.post(url, json=data)

if response.status_code == 200:
response = response.json()
with st.chat_message("assistant"):
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
else:
st.error(
f"Failed to send data to Discounts API. Status code: {response.status_code}"
)
6 changes: 6 additions & 0 deletions examples/pipelines/contextless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ curl --data '{
"query": "How to connect to Kafka in Pathway?"
}' http://localhost:8080/ | jq
```

or use the Streamlit UI. Run:
```bash
streamlit run ui/server.py --server.port 8501 --server.address 0.0.0.0
```
and then you can access the UI at `0.0.0.0:8501`.
Loading

0 comments on commit 52b6b5a

Please sign in to comment.