-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
55 additions
and
43 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import os | ||
from pathlib import Path | ||
from dotenv import load_dotenv | ||
from openai import OpenAI | ||
|
||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
ENV_FILE = BASE_DIR / ".env" | ||
|
||
load_dotenv(ENV_FILE) | ||
|
||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,11 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
from fastapi import FastAPI, HTTPException | ||
from pydantic import BaseModel | ||
from dotenv import load_dotenv | ||
from openai import OpenAI | ||
|
||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
ENV_FILE = BASE_DIR / ".env" | ||
|
||
load_dotenv(ENV_FILE) | ||
|
||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | ||
from fastapi import FastAPI | ||
from app.routes import ping, generate | ||
|
||
app = FastAPI( | ||
title="Deening API", | ||
description="Best Recipe Service powered by AI.", | ||
version="0.1.0" | ||
) | ||
|
||
class Prompt(BaseModel): | ||
prompt: str | ||
|
||
class PingResponse(BaseModel): | ||
message: str | ||
|
||
class GeneratedResponse(BaseModel): | ||
generated_text: str | ||
|
||
class ErrorResponse(BaseModel): | ||
error: str | ||
|
||
@app.get("/ping", response_model=PingResponse) | ||
async def ping(): | ||
return {"message": "pong"} | ||
|
||
@app.post("/generate", response_model=GeneratedResponse, responses={400: {"model": ErrorResponse}}) | ||
async def generate(prompt: Prompt): | ||
try: | ||
response = client.chat.completions.create( | ||
model="chatgpt-4o-latest", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": prompt.prompt} | ||
] | ||
) | ||
return {"generated_text": response.choices[0].message.content} | ||
except Exception as e: | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
app.include_router(ping.router) | ||
app.include_router(generate.router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import ping, generate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from fastapi import APIRouter, HTTPException | ||
from pydantic import BaseModel | ||
from app.config import client | ||
|
||
router = APIRouter() | ||
|
||
class Prompt(BaseModel): | ||
prompt: str | ||
|
||
class GeneratedResponse(BaseModel): | ||
generated_text: str | ||
|
||
class ErrorResponse(BaseModel): | ||
error: str | ||
|
||
@router.post("/generate", response_model=GeneratedResponse, responses={400: {"model": ErrorResponse}}) | ||
async def generate(prompt: Prompt): | ||
try: | ||
response = client.chat.completions.create( | ||
model="chatgpt-4o-latest", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": prompt.prompt} | ||
] | ||
) | ||
return {"generated_text": response.choices[0].message.content} | ||
except Exception as e: | ||
raise HTTPException(status_code=400, detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from fastapi import APIRouter | ||
from pydantic import BaseModel | ||
|
||
router = APIRouter() | ||
|
||
class PingResponse(BaseModel): | ||
message: str | ||
|
||
@router.get("/ping", response_model=PingResponse) | ||
async def ping(): | ||
return {"message": "pong"} |