Skip to content

Commit

Permalink
Integrate LangChain with FastAPI (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix T.J. Dietrich <[email protected]>
Co-authored-by: Felix T.J. Dietrich <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent bd0be7c commit 1c13b58
Show file tree
Hide file tree
Showing 16 changed files with 1,993 additions and 121 deletions.
File renamed without changes.
7 changes: 7 additions & 0 deletions server/intelligence-service/.env.example
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
17 changes: 17 additions & 0 deletions server/intelligence-service/Dockerfile
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"]
67 changes: 24 additions & 43 deletions server/intelligence-service/README.md
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.
19 changes: 19 additions & 0 deletions server/intelligence-service/app/config.py
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()
18 changes: 18 additions & 0 deletions server/intelligence-service/app/main.py
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))
13 changes: 13 additions & 0 deletions server/intelligence-service/app/model.py
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")
1,922 changes: 1,883 additions & 39 deletions server/intelligence-service/poetry.lock

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions server/intelligence-service/pyproject.toml
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"]
Expand Down
8 changes: 0 additions & 8 deletions server/intelligence-service/src/auth/router.py

This file was deleted.

5 changes: 0 additions & 5 deletions server/intelligence-service/src/config.py

This file was deleted.

8 changes: 0 additions & 8 deletions server/intelligence-service/src/main.py

This file was deleted.

10 changes: 0 additions & 10 deletions server/intelligence-service/tests/test_main.py

This file was deleted.

1 change: 1 addition & 0 deletions server/webhook-ingest/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pydantic-settings = "2.4.0"
[virtualenvs]
create = true
in-project = true
path = "./.venv"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 1c13b58

Please sign in to comment.