-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirements.txt
122 lines (106 loc) · 4.47 KB
/
requirements.txt
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
# Core Python Dependencies
import fastapi # 0.115.2: Modern API framework, type safety, ASGI, and automatic documentation
import uvicorn # 0.32.0: ASGI server for running FastAPI applications
from pydantic import BaseModel # 2.9.2: Data validation and type checking for API models
from sqlalchemy import create_engine # 2.0.36: Object-relational mapper for database interactions
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker # Manage database sessions efficiently
import openai # 1.52.0: Interact with OpenAI's API for query processing
import requests # 2.32.3: Make HTTP requests to external APIs, including OpenAI
import json # Built-in library for working with JSON data
from python_dotenv import load_dotenv # 1.0.1: Load environment variables from a `.env` file
import pyjwt # 2.9.0: Generate and verify JSON Web Tokens (JWT) for authentication
from bcrypt import hashpw, gensalt # 4.2.0: Securely hash passwords using bcrypt
import psycopg2_binary # 2.9.10: PostgreSQL database connector for Python
import redis # 5.1.1: Redis library for caching frequently accessed data
# Testing Dependencies
import pytest # 8.3.3: Test framework for writing unit and integration tests
from pytest_cov import # 5.0.0: Coverage reporting for pytest
# Code Quality and Style
import flake8 # 7.1.1: Static analysis tool for code quality
import mypy # 1.12.0: Static type checker for Python code
import black # 24.10.0: Code formatter for Python
from pre_commit import # 4.0.1: Pre-commit hook management tool
import coverage # 7.6.3: Code coverage measurement tool
# Load Environment Variables
load_dotenv()
# Database Configuration
DATABASE_URL = os.getenv("DATABASE_URL")
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# OpenAI API Key
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY
# JWT Secret Key
JWT_SECRET = os.getenv("JWT_SECRET")
# Redis Connection
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
REDIS_PORT = int(os.getenv("REDIS_PORT", 6379))
redis_client = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
# Define Data Models
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
password = Column(String)
class Query(Base):
__tablename__ = "queries"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
query_text = Column(String)
model = Column(String)
parameters = Column(JSON)
response = Column(String)
timestamp = Column(DateTime)
# Main Functions and Methods:
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def create_user(db: Session, username: str, password: str):
hashed_password = hashpw(password.encode(), gensalt())
new_user = User(username=username, password=hashed_password)
db.add(new_user)
db.commit()
db.refresh(new_user)
return new_user
def authenticate_user(db: Session, username: str, password: str):
user = db.query(User).filter(User.username == username).first()
if user and hashpw(password.encode(), user.password.encode()) == user.password.encode():
return user
return None
def generate_response(query_text: str, model: str, parameters: dict):
response = openai.Completion.create(
engine=model,
prompt=query_text,
temperature=parameters.get("temperature", 0.7),
max_tokens=parameters.get("max_tokens", 256),
top_p=parameters.get("top_p"),
frequency_penalty=parameters.get("frequency_penalty"),
presence_penalty=parameters.get("presence_penalty")
)
return response.choices[0].text
def store_query_and_response(db: Session, user_id: int, query_text: str, model: str, parameters: dict, response: str):
new_query = Query(
user_id=user_id,
query_text=query_text,
model=model,
parameters=parameters,
response=response,
timestamp=datetime.utcnow()
)
db.add(new_query)
db.commit()
db.refresh(new_query)
return new_query
def get_response_by_id(db: Session, query_id: int):
query = db.query(Query).filter(Query.id == query_id).first()
if query:
return query.response
return None
def get_user_queries(db: Session, user_id: int):
queries = db.query(Query).filter(Query.user_id == user_id).all()
return queries