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

Add Function to Preprocess Request Prompt & Address String Escape Symbol Issue #279

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion api/ask_astro/services/questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import asyncio
import re
import time
from logging import getLogger

Expand All @@ -27,6 +28,20 @@ async def _update_firestore_request(request: AskAstroRequest) -> None:
)


def _preprocess_request(request: AskAstroRequest) -> None:
if len(request.prompt) > 20000:
error_msg = "Question text is too long. Please try making a new thread and shortening your question."
request.response = error_msg
raise Exception(error_msg)
davidgxue marked this conversation as resolved.
Show resolved Hide resolved
if not request.prompt:
error_msg = "Question text cannot be empty. Please try again with a different question."
request.response = error_msg
raise Exception(error_msg)
davidgxue marked this conversation as resolved.
Show resolved Hide resolved
if len(request.messages) > 10:
request.messages = request.messages[-10:]
davidgxue marked this conversation as resolved.
Show resolved Hide resolved
request.prompt = re.sub(r"(?<!\\)\\(?!\\)", "", request.prompt)


@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, max=10))
async def answer_question(request: AskAstroRequest) -> None:
"""
Expand All @@ -43,6 +58,9 @@ async def answer_question(request: AskAstroRequest) -> None:
request.status = "in_progress"
await _update_firestore_request(request)

# Preprocess request
_preprocess_request(request=request)

# Run the question answering chain
with callbacks.collect_runs() as cb:
result = await asyncio.to_thread(
Expand Down Expand Up @@ -74,7 +92,9 @@ async def answer_question(request: AskAstroRequest) -> None:
except Exception as e:
# If there's an error, mark the request as errored and add it to the database
request.status = "error"
request.response = "Sorry, something went wrong. Please try again later."
if request.response == "":
request.response = "Sorry, something went wrong. Please try again later."

await _update_firestore_request(request)

# Propagate the error
Expand Down
Loading