Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions backend/nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ["*"]

Expand Down
27 changes: 0 additions & 27 deletions frontend/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@ export interface LogPayload {
[key: string]: any;
}

export async function pingServer(): Promise<void> {
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,
Expand Down
Loading