Skip to content

Commit a5f0f3e

Browse files
committed
Merge branch 'langchain-template'
2 parents 2aed480 + 304110a commit a5f0f3e

34 files changed

+1541
-5
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,6 @@ sonarqube/logs
136136

137137
backend
138138

139-
.terraform
139+
.terraform
140+
141+
my_app

create_fastapi_project/helpers/install.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ def install_dependencies(root: str, dependencies: list[str], dev: bool = False):
6969
if process.returncode != 0:
7070
raise Exception(f"Error installing dependencies: {stderr.decode('utf-8')}")
7171

72-
print(stdout.decode("utf-8"))
72+
decoded_stdout = stdout.decode("utf-8", errors="replace")
73+
print(decoded_stdout)
7374

7475

7576
def add_configuration_to_pyproject(
@@ -87,7 +88,7 @@ def add_configuration_to_pyproject(
8788
"readme": "README.md",
8889
"packages": [{"include": "app"}],
8990
"dependencies": {
90-
"python": ">3.9,<3.12"
91+
"python": ">=3.10,<3.12"
9192
}
9293
},
9394
"black": {

create_fastapi_project/main.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ def create_project():
3636
validate=ProjectNameValidator,
3737
default="backend",
3838
).ask()
39+
basic = ITemplate.basic.value
40+
langchain_basic = ITemplate.langchain_basic.value
41+
full = ITemplate.full.value
3942
template_type: str = questionary.select(
40-
"Choose a template", choices=[ITemplate.basic, questionary.Choice(ITemplate.full, disabled=disabled_message)]
43+
"Choose a template", choices=[
44+
basic,
45+
langchain_basic,
46+
questionary.Choice(full, disabled=disabled_message)
47+
]
4148
).ask()
42-
if template_type != ITemplate.basic:
49+
if template_type == ITemplate.full:
4350
questionary.select(
4451
"Choose the authentication service",
4552
choices=[

create_fastapi_project/templates/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class ITemplate(str, Enum):
88
basic = "basic"
9+
langchain_basic = "langchain_basic"
910
full = "full"
1011

1112
def install_template(root: str, template: ITemplate, app_name: str):
@@ -42,6 +43,15 @@ def install_template(root: str, template: ITemplate, app_name: str):
4243

4344
if has_pyproject:
4445
dependencies = ["fastapi[all]", "fastapi-pagination[sqlalchemy]@^0.12.7", "asyncer@^0.0.2", "httpx@^0.24.1"]
46+
if template == ITemplate.langchain_basic:
47+
langchain_dependencies = [
48+
"langchain@^0.0.265",
49+
"openai@^0.27.8",
50+
"adaptive-cards-py@^0.0.7",
51+
"google-search-results@^2.4.2"
52+
]
53+
dependencies[0] = "fastapi[all]@^0.99.1"
54+
dependencies.extend(langchain_dependencies)
4555
dev_dependencies = ["pytest@^5.2", "mypy@^1.5.0", "ruff@^0.0.284", "black@^23.7.0"]
4656
print("- Installing main packages. This might take a couple of minutes.")
4757
install_dependencies(poetry_path, dependencies)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROJECT_NAME=
2+
OPENAI_API_KEY=
3+
UNSPLASH_API_KEY=
4+
SERP_API_KEY=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11
2+
ENV PYTHONUNBUFFERED=1
3+
WORKDIR /code
4+
# Install Poetry
5+
RUN apt clean && apt update && apt install curl -y
6+
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python && \
7+
cd /usr/local/bin && \
8+
ln -s /opt/poetry/bin/poetry && \
9+
poetry config virtualenvs.create false
10+
11+
# Copy poetry.lock* in case it doesn't exist in the repo
12+
COPY app/pyproject.toml app/poetry.lock* /code/
13+
14+
# Allow installing dev dependencies to run tests
15+
ARG INSTALL_DEV=false
16+
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"
17+
18+
ENV PYTHONPATH=/code
19+
EXPOSE 8000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/make
2+
3+
include .env
4+
5+
help:
6+
@echo "make"
7+
@echo " install"
8+
@echo " Install all packages of poetry project locally."
9+
@echo " run-app"
10+
@echo " Run app locally without docker."
11+
@echo " run-dev-build"
12+
@echo " Run development docker compose and force build containers."
13+
@echo " run-dev"
14+
@echo " Run development docker compose."
15+
@echo " stop-dev"
16+
@echo " Stop development docker compose."
17+
@echo " run-prod"
18+
@echo " Run production docker compose."
19+
@echo " stop-prod"
20+
@echo " Run production docker compose."
21+
@echo " formatter"
22+
@echo " Apply black formatting to code."
23+
@echo " mypy"
24+
@echo " Check typing."
25+
@echo " lint"
26+
@echo " Lint code with ruff, and check if black formatter should be applied."
27+
@echo " lint-watch"
28+
@echo " Lint code with ruff in watch mode."
29+
@echo " lint-fix"
30+
@echo " Lint code with ruff and try to fix."
31+
32+
install:
33+
cd app && poetry install && cd ..
34+
35+
run-app:
36+
cd app && poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 && cd ..
37+
38+
39+
run-dev-build:
40+
docker compose -f docker-compose-dev.yml up --build
41+
42+
run-dev:
43+
docker compose -f docker-compose-dev.yml up
44+
45+
stop-dev:
46+
docker compose -f docker-compose-dev.yml down
47+
48+
run-prod:
49+
docker compose up --build
50+
51+
stop-prod:
52+
docker compose down
53+
54+
formatter:
55+
cd app && \
56+
poetry run black app
57+
58+
mypy:
59+
cd app && \
60+
poetry run mypy .
61+
62+
lint:
63+
cd app && \
64+
poetry run ruff app && poetry run black --check app
65+
66+
lint-watch:
67+
cd app && \
68+
poetry run ruff app --watch
69+
70+
lint-fix:
71+
cd app && \
72+
poetry run ruff app --fix
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# FastAPI Project with `create-fastapi-project`
2+
3+
This is a FastAPI project initialized using [`create-fastapi-project`](https://github.com/allient/create-fastapi-project), designed to provide a quick start for building APIs with [FastAPI](https://fastapi.tiangolo.com/).
4+
5+
## Required API Keys
6+
7+
### OpenAI
8+
9+
1. Create an account on [OpenAI](https://platform.openai.com/).
10+
2. Get your API key from [API Keys - OpenAI](https://platform.openai.com/account/api-keys).
11+
3. Set your API key as an environment variable named `OPENAI_API_KEY`.
12+
13+
## Other API Keys (Optional if you want to use the template's custom tools)
14+
15+
### Unsplash (Image Search)
16+
17+
1. Create an account on [Unsplash](https://unsplash.com/developers).
18+
2. Create an app on [Unsplash Developers](https://unsplash.com/oauth/applications).
19+
3. Get your Access Key from [Your Applications - Unsplash Developers](https://unsplash.com/oauth/applications).
20+
4. Set your Access Key as an environment variable named `UNSPLASH_API_KEY`.
21+
22+
### SerpApi (Search Engine Results Page API)
23+
24+
1. Create an account on [SerpApi](https://serpapi.com/).
25+
2. Get your API key from [API Key - SerpApi](https://serpapi.com/manage-api-key).
26+
3. Set your API key as an environment variable named `SERP_API_KEY`.
27+
28+
## Getting Started
29+
30+
The commands in this documentation can be customized on the **Makefile**. It can be started with and without docker.
31+
32+
- Run the server (Recommended using docker):
33+
34+
```bash
35+
# Run locally with docker in dev mode and force build
36+
make run-dev-build
37+
# or
38+
# Run locally with docker in dev mode
39+
make run-dev
40+
# or
41+
# Run locally with docker in prod mode (Autoreload disabled)
42+
make run-prod
43+
```
44+
45+
- Run the server without docker:
46+
47+
First, make sure you have all packages installed:
48+
49+
```bash
50+
make install
51+
```
52+
53+
```bash
54+
make run-app
55+
```
56+
57+
Open [http://localhost:8000/docs](http://localhost:8000/docs) with your browser to see the result.
58+
59+
You can start editing the server by modifying `app/main.py`.
60+
61+
## Demo Langchain Tools
62+
63+
This template includes some tools to help you get started with your project:
64+
65+
- Search pokemon by name or id
66+
- Search weather by city name
67+
- Search images by keyword
68+
- Search videos by keyword
69+
70+
And also includes a agent that uses the tools to answer your questions.
71+
You can access the agent by opening [http://localhost:8000/chat](http://localhost:8000/chat) with your browser.
72+
73+
## Learn More
74+
75+
To learn more about Fastapi, take a look at the following resources:
76+
77+
- [Fastapi Documentation](https://fastapi.tiangolo.com/).
78+
- [fastapi-alembic-sqlmodel-async](https://github.com/jonra1993/fastapi-alembic-sqlmodel-async).
79+
- [full-stack-fastapi-postgresql](https://github.com/tiangolo/full-stack-fastapi-postgresql).
80+
- [sqlmodel-tutorial](https://sqlmodel.tiangolo.com/tutorial/fastapi/).
81+
- [asyncer-tutorial](https://asyncer.tiangolo.com/tutorial/).
82+
- [fastapi-pagination](https://github.com/uriyyo/fastapi-pagination).
83+
- [fastapi-best-practices](https://github.com/zhanymkanov/fastapi-best-practices).
84+
- [awesome-fastapi](https://github.com/mjhea0/awesome-fastapi).
85+
86+
## Why use Create FastAPI Project?
87+
88+
`create-fastapi-project` provides a streamlined way to kickstart your FastAPI projects. Here are some compelling reasons to choose it for your project setup:
89+
90+
### Interactive Experience
91+
92+
Running `pip install create-fastapi-project@latest` (with no arguments) launches an interactive experience that guides you through the process of setting up your project. This interactive approach simplifies the initial configuration and gets you started quickly.
93+
94+
### Zero Dependencies
95+
96+
`create-fastapi-project` has been designed to be lightweight and efficient. It requires zero external dependencies, ensuring that your project remains unburdened by unnecessary packages.
97+
98+
### Reliability and Maintenance
99+
100+
`create-fastapi-project` is officially maintained by the [Allient development team](https://www.allient.io/). It is well-tested and aligns with best practices, ensuring that it functions as expected and remains up to date with FastAPI's releases.
101+
102+
By choosing `create-fastapi-project`, you streamline your initial project setup, leverage reliable patterns, and enjoy the convenience of a tool tailored for FastAPI development.
103+
104+
We love ❤️ [FastAPI](https://fastapi.tiangolo.com/) and its ecosystem. You can check out the [create-fastapi-project GitHub repository](https://github.com/allient/create-fastapi-project) - your feedback and contributions are welcome!

create_fastapi_project/templates/langchain_basic/app/README.md

Whitespace-only changes.

create_fastapi_project/templates/langchain_basic/app/app/__init__.py

Whitespace-only changes.

create_fastapi_project/templates/langchain_basic/app/app/api/__init__.py

Whitespace-only changes.

create_fastapi_project/templates/langchain_basic/app/app/api/v1/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from fastapi import APIRouter
2+
from app.api.v1.endpoints import (
3+
chat,
4+
)
5+
6+
api_router = APIRouter()
7+
api_router.include_router(chat.router, prefix="/chat", tags=["chat"])

create_fastapi_project/templates/langchain_basic/app/app/api/v1/endpoints/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)