Skip to content

Commit d0d87e6

Browse files
committed
Init structure fastapi project
1 parent 633a5f2 commit d0d87e6

39 files changed

+3519
-369
lines changed

.env.example

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# General settings
2+
PROJECT_NAME=
3+
API_V1_STR=
4+
SECRET_KEY=
5+
ACCESS_TOKEN_EXPIRE_MINUTES=
6+
7+
# CORS Origins
8+
BACKEND_CORS_ORIGINS=["http://localhost", "http://localhost:3000"]
9+
FRONTEND_HOST=http://localhost:3000
10+
11+
# Environment and logging
12+
ENVIRONMENT=local # Options: local, staging, production
13+
LOG_LEVEL=DEBUG # Options: DEBUG, INFO, ERROR, WARNING
14+
SENTRY_DSN= # Add your Sentry DSN if available
15+
16+
# Database configuration
17+
POSTGRES_SERVER=localhost
18+
POSTGRES_PORT=5432
19+
POSTGRES_USER=user
20+
POSTGRES_PASSWORD=password
21+
POSTGRES_DB=app_database
22+
23+
# LLM Configuration
24+
LLM_TEMPERATURE=0.5
25+
LLM_MAX_TOKENS=
26+
EMBEDDING_DIM=1024
27+
28+
# OpenAI Configuration
29+
LLM_OPENAI_MODEL=
30+
OPENAI_API_KEY=
31+
32+
# Optional Worker Configuration
33+
UVICORN_WORKER_COUNT=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ cython_debug/
160160
# and can be added to the global gitignore or merged into this file. For a more nuclear
161161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162162
#.idea/
163+
.python-version

data/.gitkeep

Whitespace-only changes.

main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import uvicorn
2+
from fastapi import FastAPI
3+
from fastapi.middleware.cors import CORSMiddleware
4+
5+
from qllm.api.routers import api_router
6+
7+
# from qllm.core.utils import mount_static_files
8+
from qllm.init_setting import init_openai
9+
10+
app = FastAPI()
11+
12+
init_openai()
13+
14+
origins = [
15+
"http://localhost",
16+
"http://localhost:8080",
17+
]
18+
19+
app.add_middleware(
20+
CORSMiddleware,
21+
allow_origins=origins,
22+
allow_credentials=True,
23+
allow_methods=["*"],
24+
allow_headers=["*"],
25+
)
26+
app.include_router(api_router, prefix="/api/v1")
27+
28+
# Mount the data files to serve the file viewer
29+
# mount_static_files("data", "/api/files/data")
30+
# Mount the output files from tools
31+
# mount_static_files("output", "/api/files/output")
32+
33+
34+
if __name__ == "__main__":
35+
uvicorn.run(app="main:app", host="127.0.0.1", port=5601, reload=True)

poetry.lock

Lines changed: 2475 additions & 366 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ authors = ["LongBaoCoder2 <[email protected]>"]
1313
readme = "README.md"
1414

1515
[tool.poetry.dependencies]
16-
python = "^3.9"
17-
pymilvus = "^2.4.9"
16+
python = ">=3.11,<3.13"
1817
qdrant-client = "^1.12.1"
19-
weaviate-client = "^4.9.4"
18+
pymilvus = "^2.4.9"
19+
mem0ai = "^0.1.30"
20+
openai = "^1.55.0"
21+
llama-index = "^0.12.1"
22+
llama-index-memory-mem0 = "^0.2.0"
23+
llama-index-vector-stores-qdrant = "^0.4.0"
24+
llama-index-readers-file = "^0.4.0"
25+
llama-index-embeddings-fastembed = "^0.3.0"
26+
llama-index-llms-openai = "^0.3.1"
27+
llama-index-agent-openai = "^0.4.0"
28+
llama-index-core = "^0.12.1"
29+
fastembed = "^0.4.2"
30+
pydantic-settings = "^2.6.1"
31+
psycopg2 = "^2.9.10"
2032

2133

2234
[tool.poetry.group.dev.dependencies]

qllm/alembic.ini

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
# Use forward slashes (/) also on windows to provide an os agnostic path
6+
script_location = alembic
7+
8+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
9+
# Uncomment the line below if you want the files to be prepended with date and time
10+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
11+
# for all available tokens
12+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13+
14+
# sys.path path, will be prepended to sys.path if present.
15+
# defaults to the current working directory.
16+
prepend_sys_path = .
17+
18+
# timezone to use when rendering the date within the migration file
19+
# as well as the filename.
20+
# If specified, requires the python>=3.9 or backports.zoneinfo library.
21+
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
22+
# string value is passed to ZoneInfo()
23+
# leave blank for localtime
24+
# timezone =
25+
26+
# max length of characters to apply to the "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to alembic/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
# version_path_separator = newline
53+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
54+
55+
# set to 'true' to search source files recursively
56+
# in each "version_locations" directory
57+
# new in Alembic version 1.10
58+
# recursive_version_locations = false
59+
60+
# the output encoding used when revision files
61+
# are written from script.py.mako
62+
# output_encoding = utf-8
63+
64+
sqlalchemy.url = postgresql://user:[email protected]:5432/llama_app_db
65+
66+
67+
[post_write_hooks]
68+
# post_write_hooks defines scripts or Python functions that are run
69+
# on newly generated revision scripts. See the documentation for further
70+
# detail and examples
71+
72+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
73+
# hooks = black
74+
# black.type = console_scripts
75+
# black.entrypoint = black
76+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
77+
78+
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
79+
# hooks = ruff
80+
# ruff.type = exec
81+
# ruff.executable = %(here)s/.venv/bin/ruff
82+
# ruff.options = --fix REVISION_SCRIPT_FILENAME
83+
84+
# Logging configuration
85+
[loggers]
86+
keys = root,sqlalchemy,alembic
87+
88+
[handlers]
89+
keys = console
90+
91+
[formatters]
92+
keys = generic
93+
94+
[logger_root]
95+
level = WARNING
96+
handlers = console
97+
qualname =
98+
99+
[logger_sqlalchemy]
100+
level = WARNING
101+
handlers =
102+
qualname = sqlalchemy.engine
103+
104+
[logger_alembic]
105+
level = INFO
106+
handlers =
107+
qualname = alembic
108+
109+
[handler_console]
110+
class = StreamHandler
111+
args = (sys.stderr,)
112+
level = NOTSET
113+
formatter = generic
114+
115+
[formatter_generic]
116+
format = %(levelname)-5.5s [%(name)s] %(message)s
117+
datefmt = %H:%M:%S

qllm/alembic/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

qllm/alembic/env.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from logging.config import fileConfig
2+
3+
from alembic import context
4+
from sqlalchemy import engine_from_config, pool
5+
6+
from qllm.core.config import settings
7+
from qllm.models.base import Base
8+
9+
# this is the Alembic Config object, which provides
10+
# access to the values within the .ini file in use.
11+
config = context.config
12+
# Interpret the config file for Python logging.
13+
# This line sets up loggers basically.
14+
if config.config_file_name is not None:
15+
fileConfig(config.config_file_name)
16+
17+
18+
# add your model's MetaData object here
19+
# for 'autogenerate' support
20+
21+
target_metadata = Base.metadata
22+
23+
# other values from the config, defined by the needs of env.py,
24+
# can be acquired:
25+
# my_important_option = config.get_main_option("my_important_option")
26+
# ... etc.
27+
db_url = config.get_main_option("sqlalchemy.url")
28+
if settings.SQLALCHEMY_DATABASE_URI is not None:
29+
db_url = settings.SQLALCHEMY_DATABASE_URI
30+
print(f"Using DATABASE_URL {db_url} from environment for migrations")
31+
config.set_main_option("sqlalchemy.url", db_url)
32+
33+
34+
def run_migrations_offline() -> None:
35+
"""Run migrations in 'offline' mode.
36+
37+
This configures the context with just a URL
38+
and not an Engine, though an Engine is acceptable
39+
here as well. By skipping the Engine creation
40+
we don't even need a DBAPI to be available.
41+
42+
Calls to context.execute() here emit the given string to the
43+
script output.
44+
45+
"""
46+
context.configure(
47+
url=config.get_main_option("sqlalchemy.url"),
48+
target_metadata=target_metadata,
49+
literal_binds=True,
50+
dialect_opts={"paramstyle": "named"},
51+
)
52+
53+
with context.begin_transaction():
54+
context.run_migrations()
55+
56+
57+
def run_migrations_online() -> None:
58+
"""Run migrations in 'online' mode.
59+
60+
In this scenario we need to create an Engine
61+
and associate a connection with the context.
62+
63+
"""
64+
connectable = engine_from_config(
65+
config.get_section(config.config_ini_section, {}),
66+
prefix="sqlalchemy.",
67+
poolclass=pool.NullPool,
68+
)
69+
70+
with connectable.connect() as connection:
71+
context.configure(connection=connection, target_metadata=target_metadata)
72+
73+
with context.begin_transaction():
74+
context.run_migrations()
75+
76+
77+
if context.is_offline_mode():
78+
run_migrations_offline()
79+
else:
80+
run_migrations_online()

qllm/alembic/script.py.mako

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
${imports if imports else ""}
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = ${repr(up_revision)}
16+
down_revision: Union[str, None] = ${repr(down_revision)}
17+
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
18+
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
19+
20+
21+
def upgrade() -> None:
22+
${upgrades if upgrades else "pass"}
23+
24+
25+
def downgrade() -> None:
26+
${downgrades if downgrades else "pass"}

0 commit comments

Comments
 (0)