diff --git a/backend/nlp.py b/backend/nlp.py index 73038ae4..8d92389e 100644 --- a/backend/nlp.py +++ b/backend/nlp.py @@ -7,6 +7,7 @@ from pydantic import BaseModel import spacy +import openai from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam from openai import AsyncOpenAI @@ -37,6 +38,23 @@ exit() +async def warmup_nlp(): + # Warm up the OpenAI client by making a dummy request + dummy_client = openai.AsyncOpenAI(base_url="https://localhost:8000/v1", api_key="", timeout=0.01, max_retries=0) + # make a dummy request to make sure everything is imported + try: + await dummy_client.chat.completions.create( + model=MODEL_NAME, + messages=[{"role": "user", "content": "Hello"}], + ) + except openai.APIConnectionError: + # We expect this error because we're connecting to a non-existent server + pass + + + # Warm up the SpaCy model by processing a sample text + nlp("Hello world. This is a test.").sents + prompts = { "example_sentences": """\ We're helping a writer draft a document. Please output three possible options for inspiring and fresh possible next sentences that would help the writer think about what they should write next. Guidelines: diff --git a/backend/server.py b/backend/server.py index 42cdf2de..bc47171a 100644 --- a/backend/server.py +++ b/backend/server.py @@ -3,6 +3,7 @@ import logging import os import zipfile +from contextlib import asynccontextmanager from datetime import datetime from pathlib import Path from typing import Annotated, Dict, List, Literal, Optional @@ -112,8 +113,12 @@ class ReflectionLog(Log): paragraph: str -# Initialize Server -app = FastAPI() +@asynccontextmanager +async def app_lifespan(app: FastAPI): + await nlp.warmup_nlp() + yield + +app = FastAPI(lifespan=app_lifespan) origins = ["*"] diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index c6ff0d46..5c4de560 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -7,33 +7,6 @@ export interface LogPayload { [key: string]: any; } -export async function pingServer(): Promise { - try { - // Ping the server, which returns the server's current timestamp - const url = `${SERVER_URL}/ping`; - const response = await fetch(url, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - } - }); - - if (!response.ok) { - // eslint-disable-next-line no-console - console.warn('Server ping failed:', response.status); - } - - const data = await response.json(); - // eslint-disable-next-line no-console - console.log('Server ping successful:', data); - } - catch (error) { - // eslint-disable-next-line no-console - console.warn('Server ping error:', error); - // Don't throw - we want to silently handle ping failures - } -} - export function log(payload: LogPayload) { const payloadWithTimestamp = { ...payload,