-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
53 lines (46 loc) · 2.28 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from fastapi import APIRouter, HTTPException, Depends, status
from pydantic import BaseModel
from src.openai_service import openai_service
from loguru import logger
from typing import Optional
api_router = APIRouter()
class PromptRequest(BaseModel):
prompt: str
model: Optional[str] = "text-davinci-003"
max_tokens: Optional[int] = 150
temperature: Optional[float] = 0.7
@api_router.post("/generate", status_code=status.HTTP_200_OK)
async def generate_text(request_body: PromptRequest):
try:
response = await openai_service.generate_text(request_body.prompt, request_body.model, request_body.max_tokens, request_body.temperature)
return {"text": response}
except HTTPException as e:
logger.exception(f"HTTPException during text generation: {e}")
raise e
except Exception as e:
logger.exception(f"Unexpected error during text generation: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error")
@api_router.post("/translate", status_code=status.HTTP_200_OK)
async def translate_text(request_body: PromptRequest):
try:
response = await openai_service.translate_text(request_body.prompt, request_body.model, request_body.max_tokens, request_body.temperature)
return {"translation": response}
except HTTPException as e:
logger.exception(f"HTTPException during text translation: {e}")
raise e
except Exception as e:
logger.exception(f"Unexpected error during text translation: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error")
@api_router.post("/summarize", status_code=status.HTTP_200_OK)
async def summarize_text(request_body: PromptRequest):
try:
response = await openai_service.summarize_text(request_body.prompt, request_body.model, request_body.max_tokens, request_body.temperature)
return {"summary": response}
except HTTPException as e:
logger.exception(f"HTTPException during text summarization: {e}")
raise e
except Exception as e:
logger.exception(f"Unexpected error during text summarization: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error")
```