Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use bokeh_fastapi through panel #503

Merged
merged 17 commits into from
Dec 11, 2024
Merged
8 changes: 0 additions & 8 deletions .github/actions/setup-env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ runs:
- name: Setup mambaforge and development environment
uses: conda-incubator/setup-miniconda@v3
with:
miniforge-variant: Mambaforge
miniforge-version: latest
activate-environment: ragna-deploy-dev

Expand Down Expand Up @@ -57,13 +56,6 @@ runs:
shell: bash -el {0}
run: playwright install

- name: Install dev dependencies
shell: bash -el {0}
run: |
pip install \
git+https://github.com/bokeh/bokeh-fastapi.git@main \
git+https://github.com/holoviz/panel@7377c9e99bef0d32cbc65e94e908e365211f4421

- name: Install ragna
shell: bash -el {0}
run: |
Expand Down
127 changes: 62 additions & 65 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ jobs:
matrix:
os:
- ubuntu-latest
# FIXME
# Building panel from source on Windows does not work through pip
# - windows-latest
- windows-latest
- macos-latest
python-version: ["3.10"]
include:
Expand Down Expand Up @@ -81,65 +79,64 @@ jobs:
uses: pmeier/[email protected]
with:
path: test-results.xml

pytest-ui:
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest
browser:
- chromium
- firefox
python-version:
- "3.10"
- "3.10"
- "3.12"
exclude:
- python-version: "3.11"
os: windows-latest
- python-version: "3.12"
os: windows-latest
- python-version: "3.11"
os: macos-latest
- python-version: "3.12"
os: macos-latest
include:
- browser: webkit
os: macos-latest
python-version: "3.10"

fail-fast: false

runs-on: ${{ matrix.os }}

defaults:
run:
shell: bash -el {0}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup environment
uses: ./.github/actions/setup-env
with:
python-version: ${{ matrix.python-version }}

- name: Run unit tests
id: tests
run: |
pytest tests/deploy/ui \
--browser ${{ matrix.browser }} \
--video=retain-on-failure

- name: Upload playwright video
if: failure()
uses: actions/upload-artifact@v4
with:
name:
playwright-${{ matrix.os }}-${{ matrix.python-version}}-${{ github.run_id }}
path: test-results
# pytest-ui:
# strategy:
# matrix:
# os:
# - ubuntu-latest
# - windows-latest
# - macos-latest
# browser:
# - chromium
# - firefox
# python-version:
# - "3.10"
# - "3.10"
# - "3.12"
# exclude:
# - python-version: "3.11"
# os: windows-latest
# - python-version: "3.12"
# os: windows-latest
# - python-version: "3.11"
# os: macos-latest
# - python-version: "3.12"
# os: macos-latest
# include:
# - browser: webkit
# os: macos-latest
# python-version: "3.10"
#
# fail-fast: false
#
# runs-on: ${{ matrix.os }}
#
# defaults:
# run:
# shell: bash -el {0}
#
# steps:
# - name: Checkout repository
# uses: actions/checkout@v4
# with:
# fetch-depth: 0
#
# - name: Setup environment
# uses: ./.github/actions/setup-env
# with:
# python-version: ${{ matrix.python-version }}
#
# - name: Run unit tests
# id: tests
# run: |
# pytest tests/deploy/ui \
# --browser ${{ matrix.browser }} \
# --video=retain-on-failure
#
# - name: Upload playwright video
# if: failure()
# uses: actions/upload-artifact@v4
# with:
# name:
# playwright-${{ matrix.os }}-${{ matrix.python-version}}-${{ github.run_id }}
# path: test-results
9 changes: 5 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ authors = [
readme = "README.md"
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
requires-python = ">=3.10"
dependencies = [
"aiofiles",
# Remove this and instead depend on panel[fastapi]
# after https://github.com/holoviz/panel/pull/7495 is released
"bokeh_fastapi==0.1.1",
"emoji",
"eval_type_backport; python_version<'3.10'",
"fastapi",
"httpx",
"packaging",
# FIXME: pin them to released versions
"bokeh-fastapi",
"panel",
"panel==1.5.4",
"pydantic>=2",
"pydantic-core",
"pydantic-settings>=2",
Expand Down
6 changes: 3 additions & 3 deletions ragna/core/_components.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import abc
import datetime
import enum
import functools
import inspect
import uuid
from datetime import datetime, timezone
from typing import (
AsyncIterable,
AsyncIterator,
Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(
role: MessageRole = MessageRole.SYSTEM,
sources: Optional[list[Source]] = None,
id: Optional[uuid.UUID] = None,
timestamp: Optional[datetime.datetime] = None,
timestamp: Optional[datetime] = None,
) -> None:
if isinstance(content, str):
self._content: str = content
Expand All @@ -200,7 +200,7 @@ def __init__(
self.id = id

if timestamp is None:
timestamp = datetime.datetime.utcnow()
timestamp = datetime.now(timezone.utc)
self.timestamp = timestamp

async def __aiter__(self) -> AsyncIterator[str]:
Expand Down
13 changes: 11 additions & 2 deletions ragna/deploy/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
import threading
import time
import webbrowser
from pathlib import Path
from typing import AsyncContextManager, AsyncIterator, Callable, Optional, cast

import httpx
import panel.io.fastapi
from fastapi import FastAPI, Request, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, Response
from fastapi.staticfiles import StaticFiles

import ragna
from ragna.core import RagnaException
Expand Down Expand Up @@ -79,8 +82,14 @@ def server_available() -> bool:
app.include_router(make_api_router(engine), prefix="/api")

if ui:
panel_app = make_ui_app(engine)
panel_app.serve_with_fastapi(app, endpoint="/ui")
ui_app = make_ui_app(engine)
panel.io.fastapi.add_applications({"/ui": ui_app.index_page}, app=app)
for dir in ["css", "imgs"]:
app.mount(
f"/{dir}",
StaticFiles(directory=str(Path(__file__).parent / "_ui" / dir)),
name=dir,
)
Comment on lines +87 to +92
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go into BokehFastapi under a static_dirs keyword similar to what pn.serve does.


@app.get("/", include_in_schema=False)
async def base_redirect() -> Response:
Expand Down
2 changes: 1 addition & 1 deletion ragna/deploy/_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _get_component_json_schema(
json_schema = component._protocol_model().model_json_schema()
# FIXME: there is likely a better way to exclude certain fields builtin in
# pydantic
for special_param in SpecialChatParams.model_fields:
for special_param in SpecialChatParams.__pydantic_fields__:
if (
"properties" in json_schema
and special_param in json_schema["properties"]
Expand Down
4 changes: 2 additions & 2 deletions ragna/deploy/_ui/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ async def answer(self, chat_id, prompt):
):
yield self.improve_message(message.model_dump(mode="json"))

async def get_components(self):
return self._engine.get_components().model_dump(mode="json")
def get_components(self):
return self._engine.get_components()

async def start_and_prepare(
self, name, documents, source_storage, assistant, params
Expand Down
60 changes: 0 additions & 60 deletions ragna/deploy/_ui/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from pathlib import Path
from typing import cast

import panel as pn
import param
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

from ragna.deploy._engine import Engine

Expand Down Expand Up @@ -83,61 +78,6 @@ def index_page(self):
def health_page(self):
return pn.pane.HTML("<h1>Ok</h1>")

def add_panel_app(self, server, panel_app_fn, endpoint):
# FIXME: this code will ultimately be distributed as part of panel
from functools import partial

import panel as pn
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh_fastapi import BokehFastAPI
from bokeh_fastapi.handler import WSHandler
from fastapi.responses import FileResponse
from panel.io.document import extra_socket_handlers
from panel.io.resources import COMPONENT_PATH
from panel.io.server import ComponentResourceHandler
from panel.io.state import set_curdoc

def dispatch_fastapi(conn, events=None, msg=None):
if msg is None:
msg = conn.protocol.create("PATCH-DOC", events)
return [conn._socket.send_message(msg)]

extra_socket_handlers[WSHandler] = dispatch_fastapi

def panel_app(doc):
doc.on_event("document_ready", partial(pn.state._schedule_on_load, doc))

with set_curdoc(doc):
panel_app = panel_app_fn()
panel_app.server_doc(doc)

handler = FunctionHandler(panel_app)
application = Application(handler)

BokehFastAPI({endpoint: application}, server=server)

@server.get(
f"/{COMPONENT_PATH.rstrip('/')}" + "/{path:path}", include_in_schema=False
)
def get_component_resource(path: str):
# ComponentResourceHandler.parse_url_path only ever accesses
# self._resource_attrs, which fortunately is a class attribute. Thus, we can
# get away with using the method without actually instantiating the class
self_ = cast(ComponentResourceHandler, ComponentResourceHandler)
resolved_path = ComponentResourceHandler.parse_url_path(self_, path)
return FileResponse(resolved_path)

def serve_with_fastapi(self, app: FastAPI, endpoint: str):
self.add_panel_app(app, self.index_page, endpoint)

for dir in ["css", "imgs"]:
app.mount(
f"/{dir}",
StaticFiles(directory=str(Path(__file__).parent / dir)),
name=dir,
)


def app(engine: Engine) -> App:
return App(engine)
6 changes: 1 addition & 5 deletions ragna/deploy/_ui/central_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,7 @@ def header(self):
)

chat_documents_pills = []
if (
self.current_chat is not None
and "metadata" in self.current_chat
and "documents" in self.current_chat
):
if self.current_chat is not None:
doc_names = [d["name"] for d in self.current_chat["documents"]]

# FIXME: Instead of setting a hard limit of 20 documents here, this should
Expand Down
5 changes: 5 additions & 0 deletions ragna/deploy/_ui/css/modal_configuration/fileinput.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:host(.file-input) .bk-input {
height: 80px;
border: var(--accent-color) dashed 2px;
border-radius: 10px;
}
Loading
Loading