-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
229 lines (204 loc) · 7.29 KB
/
main.py
File metadata and controls
229 lines (204 loc) · 7.29 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from fastapi import FastAPI, HTTPException, Depends, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from typing import Optional
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Text
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
import openai
import requests
import jwt
import os
from dotenv import load_dotenv
from cachetools import TTLCache, cached
from marshmallow import Schema, fields
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_DATABASE = os.getenv("POSTGRES_DATABASE")
openai.api_key = OPENAI_API_KEY
SQLALCHEMY_DATABASE_URL = f"postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:{POSTGRES_PORT}/{POSTGRES_DATABASE}"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
app = FastAPI()
origins = [
"http://localhost:8000",
"http://localhost:3000",
"http://127.0.0.1:8000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Caching Configuration
CACHE_TTL = 300 # Cache expiration time in seconds
cache = TTLCache(maxsize=100, ttl=CACHE_TTL)
# Authentication Configuration
SECRET_KEY = os.getenv("SECRET_KEY") # Replace with a strong, secure key
ALGORITHM = "HS256"
# Database Model
class RequestLog(Base):
__tablename__ = "request_logs"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(String)
timestamp = Column(DateTime)
request_data = Column(Text)
response_data = Column(Text)
# Database Session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Authentication Functions
def create_access_token(data: dict):
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def verify_token(token: str, credentials_exception):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except jwt.exceptions.InvalidTokenError:
raise credentials_exception
async def get_current_user(token: str = Depends(verify_token)):
user_id = token.get("sub")
if not user_id:
raise HTTPException(status_code=401, detail="Invalid authentication credentials")
return user_id
# API Endpoints
@app.post("/api/v1/completions")
async def create_completion(
request_data: CompletionRequest,
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)
):
try:
response = await create_completion_service(
prompt=request_data.prompt,
model=request_data.model,
max_tokens=request_data.max_tokens,
temperature=request_data.temperature
)
# Log the request
log_request(db, current_user, request_data, response)
return JSONResponse(content=response, status_code=200)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.post("/api/v1/translations")
async def translate_text(
request_data: TranslationRequest,
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)
):
try:
response = await translate_text_service(
text=request_data.text,
target_language=request_data.target_language
)
# Log the request
log_request(db, current_user, request_data, response)
return JSONResponse(content=response, status_code=200)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.post("/api/v1/summaries")
async def summarize_text(
request_data: SummarizationRequest,
db: Session = Depends(get_db),
current_user: str = Depends(get_current_user)
):
try:
response = await summarize_text_service(
text=request_data.text,
max_tokens=request_data.max_tokens
)
# Log the request
log_request(db, current_user, request_data, response)
return JSONResponse(content=response, status_code=200)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
# Service Functions
@cached(cache)
async def create_completion_service(
prompt: str,
model: str = "text-davinci-003",
max_tokens: int = 100,
temperature: float = 0.7
):
try:
response = openai.Completion.create(
engine=model,
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature
)
return response.choices[0].text
except openai.error.APIError as e:
raise HTTPException(status_code=500, detail=f"OpenAI API Error: {e}")
@cached(cache)
async def translate_text_service(
text: str,
target_language: str
):
try:
response = openai.Translation.create(
model="gpt-3.5-turbo",
from_language="auto",
to_language=target_language,
text=text
)
return response.text
except openai.error.APIError as e:
raise HTTPException(status_code=500, detail=f"OpenAI API Error: {e}")
@cached(cache)
async def summarize_text_service(
text: str,
max_tokens: int = 100
):
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Summarize this text: {text}",
max_tokens=max_tokens,
temperature=0.7
)
return response.choices[0].text
except openai.error.APIError as e:
raise HTTPException(status_code=500, detail=f"OpenAI API Error: {e}")
# Database Logging
def log_request(db: Session, user_id: str, request_data: dict, response_data: str):
db_request = RequestLog(
user_id=user_id,
timestamp=datetime.utcnow(),
request_data=json.dumps(request_data),
response_data=response_data
)
db.add(db_request)
db.commit()
# Data Models for Request Validation
class CompletionRequest(BaseModel):
prompt: str = Field(..., description="The text prompt to generate text from.")
model: Optional[str] = Field("text-davinci-003", description="The OpenAI model to use.")
max_tokens: Optional[int] = Field(100, description="The maximum number of tokens to generate.")
temperature: Optional[float] = Field(0.7, description="The temperature parameter controls the creativity of the response.")
class TranslationRequest(BaseModel):
text: str = Field(..., description="The text to translate.")
target_language: str = Field(..., description="The target language code (e.g., 'fr' for French).")
class SummarizationRequest(BaseModel):
text: str = Field(..., description="The text to summarize.")
max_tokens: Optional[int] = Field(100, description="The maximum number of tokens in the summary.")
# Run the Application
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)