-
Notifications
You must be signed in to change notification settings - Fork 12
Add RAG postprocessing step; config.yml messages; chat-fastspi.py improvements; Chainlit 2.0 #63
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
Changes from all commits
fb2d535
9b9d159
fae8abd
4326344
e49c6ac
87ee1c7
5b7245f
7128922
33e5eca
0531283
57b98ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| $schema: "https://json-schema.org/draft/2020-12/schema" | ||
| type: object | ||
| properties: | ||
| messages: | ||
| type: object | ||
| additionalProperties: | ||
| type: object | ||
| properties: | ||
| message: | ||
| type: string | ||
| enabled: | ||
| type: boolean | ||
| recipients: | ||
| type: array | ||
| items: | ||
| type: string | ||
| oneOf: | ||
| - pattern: "@.+\\..+" | ||
| - enum: ["all", "logged_in", "guests"] | ||
| trigger: | ||
| type: object | ||
| properties: | ||
| event: | ||
| type: string | ||
| enum: ["on_chat_start", "on_chat_end", "on_chat_resume", "on_message"] | ||
| after_messages: | ||
| type: integer | ||
| start: | ||
| type: string | ||
| format: date-time | ||
| end: | ||
| type: string | ||
| format: date-time | ||
| freq_max: | ||
| type: string | ||
| pattern: "^[0-9]+[smhdw]$" | ||
| anyOf: | ||
| - required: ["event"] | ||
| - required: ["after_messages"] | ||
| required: ["message", "trigger"] | ||
| required: ["messages"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,3 +164,4 @@ cython_debug/ | |
| csv_files/ | ||
| embeddings/ | ||
| records/ | ||
| config.yml | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| import hashlib | ||
| import hmac | ||
| import os | ||
| from string import Template | ||
|
|
||
| import requests | ||
| from chainlit.utils import mount_chainlit | ||
| from dotenv import load_dotenv | ||
| from fastapi import FastAPI, HTTPException, Request, Response | ||
| from fastapi import FastAPI, Request, Response | ||
| from fastapi.responses import RedirectResponse | ||
|
|
||
| load_dotenv() | ||
|
|
@@ -17,6 +18,20 @@ | |
| CLOUDFLARE_SECRET_KEY = os.getenv("CLOUDFLARE_SECRET_KEY") | ||
| CLOUDFLARE_SITE_KEY = os.getenv("CLOUDFLARE_SITE_KEY") | ||
|
|
||
| ERROR_PAGE_TEMPLATE = Template( | ||
| f""" | ||
| <html> | ||
| <body> | ||
| <h1>$error_title</h1> | ||
| <p>If you believe this to be in error, please contact the maintainers to report an issue: help@reactome.org</p> | ||
| <form action="{CHAINLIT_URI}" method="get"> | ||
| <button type="submit">Try again</button> | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking for feedback on this message. Should we include an email or link?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now it could be help@reactome.org |
||
| </form> | ||
| </body> | ||
| </html> | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def make_signature(value: str) -> str: | ||
| if CLOUDFLARE_SECRET_KEY is None: | ||
|
|
@@ -56,6 +71,10 @@ async def verify_captcha_middleware(request: Request, call_next): | |
| response = await call_next(request) | ||
| return response | ||
|
|
||
| if request.url.scheme == "http": | ||
| url = request.url.replace(scheme="https") | ||
| return RedirectResponse(url=str(url)) | ||
|
|
||
| # Check if the user has completed the CAPTCHA verification | ||
| captcha_verified = request.cookies.get("captcha_verified") | ||
|
|
||
|
|
@@ -69,18 +88,14 @@ async def verify_captcha_middleware(request: Request, call_next): | |
|
|
||
| # Serve the CAPTCHA verification page (basic HTML form) | ||
| @app.get(f"{CHAINLIT_URI}/verify_captcha_page") | ||
| async def captcha_page(request: Request): | ||
|
|
||
| host = request.headers.get('X-Forwarded-Host', request.headers.get('Host')) | ||
| form_action = f"https://{host}{CHAINLIT_URI}/verify_captcha" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reverted this since the new HTTPS middleware redirect should handle this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true |
||
|
|
||
| async def captcha_page(): | ||
| html_content = f""" | ||
| <html> | ||
| <head> | ||
| <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script> | ||
| </head> | ||
| <body> | ||
| <form id="captcha-form" action="{form_action}" method="post"> | ||
| <form id="captcha-form" action="{CHAINLIT_URI}/verify_captcha" method="post"> | ||
| <div class="cf-turnstile" data-sitekey="{os.getenv('CLOUDFLARE_SITE_KEY')}" data-callback="onSubmit"></div> | ||
| </form> | ||
| <script> | ||
|
|
@@ -107,14 +122,28 @@ async def verify_captcha(request: Request): | |
| form_data = await request.form() | ||
| cf_turnstile_response = form_data.get("cf-turnstile-response") | ||
| if not isinstance(cf_turnstile_response, str): | ||
| raise HTTPException(status_code=400, detail="CAPTCHA response is invalid") | ||
| error_html = ERROR_PAGE_TEMPLATE.substitute( | ||
| error_title="CAPTCHA response is invalid", | ||
| ) | ||
| return Response(content=error_html, media_type="text/html", status_code=400) | ||
|
|
||
| client_ip: str | ||
| if request.client: | ||
| client_ip = request.client.host | ||
| elif "X-Forwarded-For" in request.headers: | ||
| client_ip = request.headers["X-Forwarded-For"].split(",")[0] | ||
| else: | ||
| error_html = ERROR_PAGE_TEMPLATE.substitute( | ||
| error_title="Could not determine client host", | ||
| ) | ||
| return Response(content=error_html, media_type="text/html", status_code=400) | ||
|
|
||
| # Verify the CAPTCHA with Cloudflare | ||
| url = "https://challenges.cloudflare.com/turnstile/v0/siteverify" | ||
| data = { | ||
| "secret": os.getenv("CLOUDFLARE_SECRET_KEY"), | ||
| "response": cf_turnstile_response, | ||
| "remoteip": request.client.host if request.client else "127.0.0.1", | ||
| "remoteip": client_ip, | ||
| } | ||
|
|
||
| # Perform request to Cloudflare Turnstile verification endpoint | ||
|
|
@@ -123,7 +152,10 @@ async def verify_captcha(request: Request): | |
|
|
||
| # If CAPTCHA validation fails, return an error | ||
| if not result.get("success"): | ||
| raise HTTPException(status_code=400, detail="CAPTCHA verification failed") | ||
| error_html = ERROR_PAGE_TEMPLATE.substitute( | ||
| error_title="CAPTCHA verification failed", | ||
| ) | ||
| return Response(content=error_html, media_type="text/html", status_code=400) | ||
|
|
||
| # Set a signed cookie to mark CAPTCHA as verified | ||
| cookie_value = create_secure_cookie(cf_turnstile_response) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this to
config.yml