zeetay commited on
Commit
889ca34
·
0 Parent(s):

added base infrastructure and gitignored env vars

Browse files
Files changed (4) hide show
  1. ai_prompts.py +16 -0
  2. app.py +52 -0
  3. requirements.txt +5 -0
  4. utils.py +27 -0
ai_prompts.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # backend/ai_prompts.py
2
+ TEMPLATES = {
3
+ "Academic": (
4
+ "You are an academic assistant. Convert the following problem into academic-style pseudocode.\n\n"
5
+ "Problem:\n{user_input}\n\nDetail level: {detail}\n\nOutput: Markdown formatted pseudocode with clear variable definitions."
6
+ ),
7
+ "Developer-Friendly": (
8
+ "You are a senior software engineer. Produce developer-friendly pseudocode for:\n\n{user_input}\n\nDetail: {detail}\n\nReturn code-style pseudocode in Markdown."
9
+ ),
10
+ "English-Like": (
11
+ "Rewrite the problem as step-by-step plain English instructions suitable for beginners:\n\n{user_input}\n\nDetail: {detail}\n\nUse numbered steps."
12
+ ),
13
+ "Step-by-Step": (
14
+ "Create a step-by-step plan that a developer could implement. Problem:\n\n{user_input}\n\nDetail: {detail}"
15
+ ),
16
+ }
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # backend/app.py
2
+ from fastapi import FastAPI, HTTPException, Request
3
+ from pydantic import BaseModel, constr
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ import os
6
+ import time
7
+ import logging
8
+ import openai
9
+ from .ai_prompts import TEMPLATES
10
+ from .utils import call_openai_with_retries
11
+
12
+ logging.basicConfig(level=logging.INFO)
13
+ app = FastAPI(title="Pseudogen V1 API")
14
+
15
+ app.add_middleware(
16
+ CORSMiddleware,
17
+ allow_origins=["*"], # lock down in production
18
+ allow_credentials=True,
19
+ allow_methods=["*"],
20
+ allow_headers=["*"],
21
+ )
22
+
23
+ OPENAI_KEY = os.getenv("OPENAI_API_KEY")
24
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini") # set as env var
25
+
26
+ if not OPENAI_KEY:
27
+ logging.warning("OPENAI_API_KEY not found. Set OPENAI_API_KEY in environment.")
28
+
29
+ openai.api_key = OPENAI_KEY
30
+
31
+ class GenerateRequest(BaseModel):
32
+ problem_description: constr(min_length=1, max_length=4000)
33
+ style: constr(regex="^(Academic|Developer-Friendly|English-Like|Step-by-Step)$")
34
+ detail: constr(regex="^(Concise|Detailed)$")
35
+
36
+ @app.post("/generate-pseudocode")
37
+ async def generate(req: GenerateRequest):
38
+ if not OPENAI_KEY:
39
+ raise HTTPException(status_code=500, detail="Server misconfiguration: OPENAI_API_KEY missing")
40
+
41
+ template = TEMPLATES.get(req.style)
42
+ if template is None:
43
+ raise HTTPException(status_code=400, detail="Unknown style")
44
+
45
+ prompt = template.format(user_input=req.problem_description, detail=req.detail)
46
+ try:
47
+ response_text = call_openai_with_retries(prompt, model=OPENAI_MODEL)
48
+ except Exception as e:
49
+ logging.exception("OpenAI call failed")
50
+ raise HTTPException(status_code=502, detail=str(e))
51
+
52
+ return {"markdown": response_text}
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ openai
4
+ python-dotenv
5
+ pydantic
utils.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # backend/utils.py
2
+ import time
3
+ import openai
4
+ from requests.exceptions import RequestException
5
+
6
+ def call_openai_with_retries(prompt, model="gpt-4o-mini", max_retries=3, backoff=1.0):
7
+ last_err = None
8
+ for attempt in range(1, max_retries + 1):
9
+ try:
10
+ resp = openai.ChatCompletion.create(
11
+ model=model,
12
+ messages=[{"role": "user", "content": prompt}],
13
+ temperature=0.2,
14
+ max_tokens=1200,
15
+ )
16
+ # defensive checks:
17
+ if "choices" in resp and len(resp.choices) > 0:
18
+ text = resp.choices[0].message.get("content", "").strip()
19
+ if text:
20
+ return text
21
+ raise RuntimeError("Empty response from model")
22
+ except Exception as e:
23
+ last_err = e
24
+ if attempt < max_retries:
25
+ time.sleep(backoff * attempt)
26
+ else:
27
+ raise