Skip to content

Commit

Permalink
Merge branch 'langchain-template'
Browse files Browse the repository at this point in the history
  • Loading branch information
LeooR22 committed Aug 22, 2023
2 parents 2aed480 + 304110a commit a5f0f3e
Show file tree
Hide file tree
Showing 34 changed files with 1,541 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ sonarqube/logs

backend

.terraform
.terraform

my_app
5 changes: 3 additions & 2 deletions create_fastapi_project/helpers/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def install_dependencies(root: str, dependencies: list[str], dev: bool = False):
if process.returncode != 0:
raise Exception(f"Error installing dependencies: {stderr.decode('utf-8')}")

print(stdout.decode("utf-8"))
decoded_stdout = stdout.decode("utf-8", errors="replace")
print(decoded_stdout)


def add_configuration_to_pyproject(
Expand All @@ -87,7 +88,7 @@ def add_configuration_to_pyproject(
"readme": "README.md",
"packages": [{"include": "app"}],
"dependencies": {
"python": ">3.9,<3.12"
"python": ">=3.10,<3.12"
}
},
"black": {
Expand Down
11 changes: 9 additions & 2 deletions create_fastapi_project/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ def create_project():
validate=ProjectNameValidator,
default="backend",
).ask()
basic = ITemplate.basic.value
langchain_basic = ITemplate.langchain_basic.value
full = ITemplate.full.value
template_type: str = questionary.select(
"Choose a template", choices=[ITemplate.basic, questionary.Choice(ITemplate.full, disabled=disabled_message)]
"Choose a template", choices=[
basic,
langchain_basic,
questionary.Choice(full, disabled=disabled_message)
]
).ask()
if template_type != ITemplate.basic:
if template_type == ITemplate.full:
questionary.select(
"Choose the authentication service",
choices=[
Expand Down
10 changes: 10 additions & 0 deletions create_fastapi_project/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class ITemplate(str, Enum):
basic = "basic"
langchain_basic = "langchain_basic"
full = "full"

def install_template(root: str, template: ITemplate, app_name: str):
Expand Down Expand Up @@ -42,6 +43,15 @@ def install_template(root: str, template: ITemplate, app_name: str):

if has_pyproject:
dependencies = ["fastapi[all]", "fastapi-pagination[sqlalchemy]@^0.12.7", "asyncer@^0.0.2", "httpx@^0.24.1"]
if template == ITemplate.langchain_basic:
langchain_dependencies = [
"langchain@^0.0.265",
"openai@^0.27.8",
"adaptive-cards-py@^0.0.7",
"google-search-results@^2.4.2"
]
dependencies[0] = "fastapi[all]@^0.99.1"
dependencies.extend(langchain_dependencies)
dev_dependencies = ["pytest@^5.2", "mypy@^1.5.0", "ruff@^0.0.284", "black@^23.7.0"]
print("- Installing main packages. This might take a couple of minutes.")
install_dependencies(poetry_path, dependencies)
Expand Down
4 changes: 4 additions & 0 deletions create_fastapi_project/templates/langchain_basic/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PROJECT_NAME=
OPENAI_API_KEY=
UNSPLASH_API_KEY=
SERP_API_KEY=
19 changes: 19 additions & 0 deletions create_fastapi_project/templates/langchain_basic/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11
ENV PYTHONUNBUFFERED=1
WORKDIR /code
# Install Poetry
RUN apt clean && apt update && apt install curl -y
RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python && \
cd /usr/local/bin && \
ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false

# Copy poetry.lock* in case it doesn't exist in the repo
COPY app/pyproject.toml app/poetry.lock* /code/

# Allow installing dev dependencies to run tests
ARG INSTALL_DEV=false
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"

ENV PYTHONPATH=/code
EXPOSE 8000
72 changes: 72 additions & 0 deletions create_fastapi_project/templates/langchain_basic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/make

include .env

help:
@echo "make"
@echo " install"
@echo " Install all packages of poetry project locally."
@echo " run-app"
@echo " Run app locally without docker."
@echo " run-dev-build"
@echo " Run development docker compose and force build containers."
@echo " run-dev"
@echo " Run development docker compose."
@echo " stop-dev"
@echo " Stop development docker compose."
@echo " run-prod"
@echo " Run production docker compose."
@echo " stop-prod"
@echo " Run production docker compose."
@echo " formatter"
@echo " Apply black formatting to code."
@echo " mypy"
@echo " Check typing."
@echo " lint"
@echo " Lint code with ruff, and check if black formatter should be applied."
@echo " lint-watch"
@echo " Lint code with ruff in watch mode."
@echo " lint-fix"
@echo " Lint code with ruff and try to fix."

install:
cd app && poetry install && cd ..

run-app:
cd app && poetry run uvicorn app.main:app --host 0.0.0.0 --port 8000 && cd ..


run-dev-build:
docker compose -f docker-compose-dev.yml up --build

run-dev:
docker compose -f docker-compose-dev.yml up

stop-dev:
docker compose -f docker-compose-dev.yml down

run-prod:
docker compose up --build

stop-prod:
docker compose down

formatter:
cd app && \
poetry run black app

mypy:
cd app && \
poetry run mypy .

lint:
cd app && \
poetry run ruff app && poetry run black --check app

lint-watch:
cd app && \
poetry run ruff app --watch

lint-fix:
cd app && \
poetry run ruff app --fix
104 changes: 104 additions & 0 deletions create_fastapi_project/templates/langchain_basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# FastAPI Project with `create-fastapi-project`

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/).

## Required API Keys

### OpenAI

1. Create an account on [OpenAI](https://platform.openai.com/).
2. Get your API key from [API Keys - OpenAI](https://platform.openai.com/account/api-keys).
3. Set your API key as an environment variable named `OPENAI_API_KEY`.

## Other API Keys (Optional if you want to use the template's custom tools)

### Unsplash (Image Search)

1. Create an account on [Unsplash](https://unsplash.com/developers).
2. Create an app on [Unsplash Developers](https://unsplash.com/oauth/applications).
3. Get your Access Key from [Your Applications - Unsplash Developers](https://unsplash.com/oauth/applications).
4. Set your Access Key as an environment variable named `UNSPLASH_API_KEY`.

### SerpApi (Search Engine Results Page API)

1. Create an account on [SerpApi](https://serpapi.com/).
2. Get your API key from [API Key - SerpApi](https://serpapi.com/manage-api-key).
3. Set your API key as an environment variable named `SERP_API_KEY`.

## Getting Started

The commands in this documentation can be customized on the **Makefile**. It can be started with and without docker.

- Run the server (Recommended using docker):

```bash
# Run locally with docker in dev mode and force build
make run-dev-build
# or
# Run locally with docker in dev mode
make run-dev
# or
# Run locally with docker in prod mode (Autoreload disabled)
make run-prod
```

- Run the server without docker:

First, make sure you have all packages installed:

```bash
make install
```

```bash
make run-app
```

Open [http://localhost:8000/docs](http://localhost:8000/docs) with your browser to see the result.

You can start editing the server by modifying `app/main.py`.

## Demo Langchain Tools

This template includes some tools to help you get started with your project:

- Search pokemon by name or id
- Search weather by city name
- Search images by keyword
- Search videos by keyword

And also includes a agent that uses the tools to answer your questions.
You can access the agent by opening [http://localhost:8000/chat](http://localhost:8000/chat) with your browser.

## Learn More

To learn more about Fastapi, take a look at the following resources:

- [Fastapi Documentation](https://fastapi.tiangolo.com/).
- [fastapi-alembic-sqlmodel-async](https://github.com/jonra1993/fastapi-alembic-sqlmodel-async).
- [full-stack-fastapi-postgresql](https://github.com/tiangolo/full-stack-fastapi-postgresql).
- [sqlmodel-tutorial](https://sqlmodel.tiangolo.com/tutorial/fastapi/).
- [asyncer-tutorial](https://asyncer.tiangolo.com/tutorial/).
- [fastapi-pagination](https://github.com/uriyyo/fastapi-pagination).
- [fastapi-best-practices](https://github.com/zhanymkanov/fastapi-best-practices).
- [awesome-fastapi](https://github.com/mjhea0/awesome-fastapi).

## Why use Create FastAPI Project?

`create-fastapi-project` provides a streamlined way to kickstart your FastAPI projects. Here are some compelling reasons to choose it for your project setup:

### Interactive Experience

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.

### Zero Dependencies

`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.

### Reliability and Maintenance

`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.

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.

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!
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fastapi import APIRouter
from app.api.v1.endpoints import (
chat,
)

api_router = APIRouter()
api_router.include_router(chat.router, prefix="/chat", tags=["chat"])
Empty file.
Loading

0 comments on commit a5f0f3e

Please sign in to comment.