-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate LangChain with FastAPI (#49)
Co-authored-by: Felix T.J. Dietrich <[email protected]> Co-authored-by: Felix T.J. Dietrich <[email protected]>
- Loading branch information
1 parent
bd0be7c
commit 1c13b58
Showing
16 changed files
with
1,993 additions
and
121 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# For Non-Azure OpenAI | ||
OPENAI_API_KEY=your-openai-secret-key | ||
|
||
# For Azure OpenAI | ||
AZURE_OPENAI_API_KEY=your-azure-api-key | ||
AZURE_OPENAI_ENDPOINT=your-azure-base-url | ||
AZURE_OPENAI_API_VERSION=your-api-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM python:3.12 as requirements-stage | ||
|
||
WORKDIR /tmp | ||
|
||
RUN pip install poetry | ||
COPY ./pyproject.toml ./poetry.lock* /tmp/ | ||
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes | ||
|
||
FROM python:3.12 | ||
|
||
WORKDIR /code | ||
|
||
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt | ||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt | ||
COPY ./app /code/app | ||
|
||
CMD ["fastapi", "run", "app/main.py", "--port", "5000"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,41 @@ | ||
# Intelligence Service | ||
|
||
This is a FastAPI service for interfacing with LangChain and other machine learning services. | ||
## Overview | ||
|
||
## Table of Contents | ||
A FastAPI service for interfacing with machine learning models. | ||
|
||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Project Structure](#project-structure) | ||
- [Testing](#testing) | ||
## Setup | ||
|
||
## Installation | ||
### Prerequisites | ||
|
||
To set up the project locally, follow these steps: | ||
- **Python 3.12** | ||
- **Poetry** for dependency management | ||
- **Docker** for containerization | ||
|
||
1. **Install dependencies:** | ||
The project uses `poetry` for dependency management. Install the dependencies by running: | ||
```bash | ||
poetry install | ||
``` | ||
### Installation | ||
|
||
2. **Run the application:** | ||
You can start the FastAPI application with Uvicorn: | ||
```bash | ||
poetry run uvicorn src.main:app --reload | ||
``` | ||
Install dependencies using Poetry: | ||
|
||
## Usage | ||
```bash | ||
pip install poetry | ||
poetry install | ||
``` | ||
|
||
After running the application, you can access the FastAPI API documentation at `http://127.0.0.1:8000/docs` or `http://127.0.0.1:8000/redoc`. | ||
## Running the Service | ||
|
||
## Project Structure | ||
### Development | ||
|
||
The project is organized as follows: | ||
``` | ||
intelligence-service/ | ||
├── pyproject.toml | ||
├── README.md | ||
├── poetry.lock | ||
├── .pytest_cache/ | ||
├── tests/ | ||
│ ├── __init__.py | ||
│ └── test_hello.py | ||
├── src/ | ||
│ ├── config.py | ||
│ ├── main.py | ||
│ └── auth/ | ||
│ └── router.py | ||
└── ... | ||
```bash | ||
fastapi dev | ||
``` | ||
## Testing | ||
|
||
The project includes a set of unit tests to ensure that the core functionalities work as expected. These tests are located in the `tests/` directory. | ||
### Production | ||
|
||
### Running Tests | ||
```bash | ||
fastapi run | ||
``` | ||
|
||
## Usage | ||
|
||
To run all tests, use the following command: | ||
After running the application, you can access the FastAPI API documentation at `http://127.0.0.1:8000/docs` or `http://127.0.0.1:8000/redoc`. | ||
|
||
```bash | ||
poetry run pytest |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
class Settings(BaseSettings): | ||
OPENAI_API_KEY: str = "" | ||
|
||
AZURE_OPENAI_API_KEY: str = "" | ||
AZURE_OPENAI_ENDPOINT: str = "" | ||
AZURE_OPENAI_API_VERSION: str = "" | ||
|
||
@property | ||
def is_openai_available(self): | ||
return bool(self.OPENAI_API_KEY) | ||
|
||
@property | ||
def is_azure_openai_available(self): | ||
return bool(self.AZURE_OPENAI_API_KEY) and bool(self.AZURE_OPENAI_ENDPOINT) and bool(self.AZURE_OPENAI_API_VERSION) | ||
|
||
settings = Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from fastapi import FastAPI, HTTPException | ||
from pydantic import BaseModel | ||
from .model import model | ||
|
||
app = FastAPI() | ||
|
||
|
||
class ChatRequest(BaseModel): | ||
message: str | ||
|
||
|
||
@app.post("/chat", response_model=dict, summary="Chat with LLM") | ||
async def chat(request: ChatRequest): | ||
try: | ||
response = model.invoke(request.message) | ||
return { "response": response.content } | ||
except Exception as e: | ||
raise HTTPException(status_code=500, detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from langchain.chat_models.base import BaseChatModel | ||
from langchain_openai import ChatOpenAI, AzureChatOpenAI | ||
from .config import settings | ||
|
||
|
||
model: BaseChatModel | ||
|
||
if settings.is_openai_available: | ||
model = ChatOpenAI() | ||
elif settings.is_azure_openai_available: | ||
model = AzureChatOpenAI() | ||
else: | ||
raise EnvironmentError("No LLM available") |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
[tool.poetry] | ||
name = "intelligence-service" | ||
version = "0.1.0" | ||
description = "A FastAPI service for interfacing with large language models (LLMs) and providing intelligent text-based responses" | ||
authors = ["milenasrb <[email protected]>"] | ||
description = "A FastAPI service for interfacing with machine learning models." | ||
authors = ["Felix T.J. Dietrich <[email protected]>", "milenasrb <[email protected]>"] | ||
readme = "README.md" | ||
package-mode = false | ||
|
||
|
||
[tool.poetry.dependencies] | ||
python = "^3.12" | ||
fastapi = "^0.112.0" | ||
uvicorn = "^0.30.5" | ||
|
||
fastapi = {extras = ["standard"], version = "0.112.1"} | ||
pydantic-settings = "2.4.0" | ||
langchain = "0.2.15" | ||
langchain-openai = "0.1.23" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
httpx = "^0.27.0" | ||
pytest = "^8.3.2" | ||
[virtualenvs] | ||
create = true | ||
in-project = true | ||
path = "./.venv" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters