Corin1998 commited on
Commit
4ae243e
·
verified ·
1 Parent(s): dea650b

Upload 4 files

Browse files
Files changed (3) hide show
  1. API_KEY=dev +2 -1
  2. main.py +4 -9
  3. openai_integration.py +8 -3
API_KEY=dev CHANGED
@@ -1,5 +1,6 @@
1
  API_KEY=dev
2
- OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx
 
3
  OAI_CHAT_MODEL=gpt-4o-mini
4
  OAI_EMB_MODEL=text-embedding-3-small
5
  DATABASE_URL=sqlite:///./app.db
 
1
  API_KEY=dev
2
+ OPENAI_API_KEY=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
3
+
4
  OAI_CHAT_MODEL=gpt-4o-mini
5
  OAI_EMB_MODEL=text-embedding-3-small
6
  DATABASE_URL=sqlite:///./app.db
main.py CHANGED
@@ -10,12 +10,6 @@ Features (MVP)
10
  - Invoices: create (or convert from quote), mark paid, compute totals
11
  - Simple API-key auth via X-API-Key header (set API_KEY env var; defaults to "dev")
12
  - Autogenerated docs at /docs
13
-
14
- Run locally:
15
- python -m venv .venv && source .venv/bin/activate
16
- pip install -r requirements.txt
17
- export API_KEY=dev
18
- uvicorn main:app --reload
19
  """
20
 
21
  from __future__ import annotations
@@ -28,9 +22,6 @@ from fastapi.middleware.cors import CORSMiddleware
28
  from pydantic import BaseModel
29
  from sqlmodel import Field as SQLField, Session, SQLModel, create_engine, select, Relationship
30
 
31
- from openai_integration import router as ai_router
32
- app.include_router(ai_router, prefix="/ai", tags=["ai"])
33
-
34
  # --------------------------
35
  # Auth
36
  # --------------------------
@@ -224,3 +215,7 @@ def add_invoice_item(invoice_id: int, payload: InvoiceItem, session: Session = D
224
  @app.get("/")
225
  def root():
226
  return {"ok": True, "app": "mini-invoice-saas", "docs": "/docs"}
 
 
 
 
 
10
  - Invoices: create (or convert from quote), mark paid, compute totals
11
  - Simple API-key auth via X-API-Key header (set API_KEY env var; defaults to "dev")
12
  - Autogenerated docs at /docs
 
 
 
 
 
 
13
  """
14
 
15
  from __future__ import annotations
 
22
  from pydantic import BaseModel
23
  from sqlmodel import Field as SQLField, Session, SQLModel, create_engine, select, Relationship
24
 
 
 
 
25
  # --------------------------
26
  # Auth
27
  # --------------------------
 
215
  @app.get("/")
216
  def root():
217
  return {"ok": True, "app": "mini-invoice-saas", "docs": "/docs"}
218
+
219
+ # -------- include ChatGPT router (最後に!) --------
220
+ from openai_integration import router as ai_router
221
+ app.include_router(ai_router, prefix="/ai", tags=["ai"])
openai_integration.py CHANGED
@@ -20,17 +20,22 @@ except Exception:
20
 
21
  from openai import OpenAI
22
 
 
 
 
 
 
 
23
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
24
  if not OPENAI_API_KEY:
25
  raise RuntimeError("Set OPENAI_API_KEY before importing openai_integration")
26
 
27
  client = OpenAI(api_key=OPENAI_API_KEY)
28
 
29
- OAI_CHAT_MODEL = os.getenv("OAI_CHAT_MODEL", "gpt-4o-mini") # 軽量モデル推奨
30
- OAI_EMB_MODEL = os.getenv("OAI_EMB_MODEL", "text-embedding-3-small")
31
 
32
  router = APIRouter()
33
-
34
  # -------- Schemas --------
35
  class LineItem(BaseModel):
36
  description: str
 
20
 
21
  from openai import OpenAI
22
 
23
+ API_KEY = os.getenv("API_KEY", "dev")
24
+ async def require_api_key(x_api_key: str | None = Header(default=None)):
25
+ if x_api_key != API_KEY:
26
+ raise HTTPException(status_code=401, detail="Invalid or missing X-API-Key")
27
+
28
+ # --- OpenAI クライアント ---
29
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
30
  if not OPENAI_API_KEY:
31
  raise RuntimeError("Set OPENAI_API_KEY before importing openai_integration")
32
 
33
  client = OpenAI(api_key=OPENAI_API_KEY)
34
 
35
+ OAI_CHAT_MODEL = os.getenv("OAI_CHAT_MODEL", "gpt-4o-mini")
36
+ OAI_EMB_MODEL = os.getenv("OAI_EMB_MODEL", "text-embedding-3-small")
37
 
38
  router = APIRouter()
 
39
  # -------- Schemas --------
40
  class LineItem(BaseModel):
41
  description: str