Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAI-271] - Monitoring with Langfuse #1254

Merged
merged 74 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
fd56555
Update scripts to accept langfuse
mdciri Nov 15, 2024
aa188ad
Update app api.md
mdciri Nov 15, 2024
f8a1723
Update modules
mdciri Nov 18, 2024
e740628
Update docker compose yaml file with langfuse
mdciri Nov 18, 2024
8ff9485
Update modules
mdciri Nov 18, 2024
f66ecdc
Update modules
mdciri Nov 18, 2024
591065e
Update modules
mdciri Nov 18, 2024
b801e49
Update chatbot that masks string to send to langfuse server
mdciri Nov 18, 2024
22fe47b
Update src.modules
mdciri Nov 19, 2024
bfd9a0a
Update params
mdciri Nov 19, 2024
2978369
Update env example
mdciri Nov 20, 2024
781e115
Update src.modules
mdciri Nov 20, 2024
02fd9ae
Update config.prompts
mdciri Nov 20, 2024
d23b9c3
Update poetry
mdciri Nov 20, 2024
2168931
Update local Dockerfile
mdciri Nov 21, 2024
cddd034
Update app/main.py
mdciri Nov 21, 2024
d21814e
Remove generate method from chatbot and tests
mdciri Nov 21, 2024
e9c82ec
Add langfuse connection test
mdciri Nov 21, 2024
694519e
Add langfuse connection test
mdciri Nov 21, 2024
a315fa8
Reinstate app/main.py
mdciri Nov 22, 2024
d72d113
Merged branch with main
mdciri Nov 26, 2024
a15a53e
Update authors in pyproject
mdciri Nov 26, 2024
3a3bc36
git merge with main branch
mdciri Nov 26, 2024
4d8583e
Update docker compose test
mdciri Nov 26, 2024
d9fa13b
chore: out dir for tests
batdevis Nov 27, 2024
ee7c252
Update src.modules
mdciri Nov 27, 2024
f9c414a
Update src.app.main
mdciri Nov 27, 2024
e88d8b0
Update docker
mdciri Nov 27, 2024
13ec17d
Restore pre-push
mdciri Nov 27, 2024
c9896c2
Update poetry
mdciri Nov 27, 2024
53ca1ba
Update src.app.main
mdciri Nov 27, 2024
345d630
Update docker
mdciri Nov 27, 2024
9a48b18
chore: use port 3001 for Langfuse because 3000 is used by frontend
batdevis Nov 27, 2024
5d16dad
Merge branch 'main' into feature/langfuse-chatbot/cai-271
mdciri Nov 28, 2024
0872a0f
Remove gradio and webapp
mdciri Nov 28, 2024
1b4dc91
Update src.modules
mdciri Nov 28, 2024
699b8b7
Update docker compose.yaml
mdciri Nov 28, 2024
eb5d59e
chore: redis data persistence, frontend build
batdevis Nov 29, 2024
31a6e7d
chore: langfuse host
batdevis Nov 29, 2024
b6c5217
chore: readme
batdevis Nov 29, 2024
d404aec
Update README
mdciri Nov 29, 2024
583ddf5
chore: scripts
batdevis Nov 29, 2024
3a246f6
Merge branch 'main' into feature/langfuse-chatbot/cai-271
mdciri Nov 29, 2024
c2d583d
fix: create index command
batdevis Dec 2, 2024
5a2276f
Update main.py
mdciri Dec 2, 2024
4d434a4
Update fe package-lock.json
mdciri Dec 2, 2024
dcd2e00
Merge branch 'feature/langfuse-chatbot/cai-271' of github.com:pagopa/…
mdciri Dec 2, 2024
9ba8e27
Update retrieve langfuse api key through SSM
mdciri Dec 2, 2024
fd1d918
Update retrieve langfuse api key through SSM
mdciri Dec 2, 2024
f89cc2a
Update add and remove tag for langfuse
mdciri Dec 2, 2024
c622c2d
Update add and remove tag for langfuse
mdciri Dec 2, 2024
ec8b099
Update chat_generate method
mdciri Dec 2, 2024
79a251d
Update trace id langfuse equal to dynamodb id
mdciri Dec 3, 2024
d768676
Merge branch 'main' into feature/langfuse-chatbot/cai-271
mdciri Dec 3, 2024
29774ac
Merge branch 'main' into feature/langfuse-chatbot/cai-271
batdevis Dec 4, 2024
99dfa43
chore: .env.example order
batdevis Dec 4, 2024
12f753a
chore: langfuse env.example
batdevis Dec 4, 2024
1349aba
chore: env.example fix
batdevis Dec 4, 2024
a1605e9
chore: langfuse env vars
batdevis Dec 4, 2024
e8d02c2
feat: salts table
batdevis Dec 4, 2024
3cf0f8e
feat(chatbot): salt for user id hash
batdevis Dec 4, 2024
f556b82
Merge branch 'main' into feature/langfuse-chatbot/cai-271
batdevis Dec 4, 2024
5a8e1e3
fix(chatbot): salts query
batdevis Dec 5, 2024
e1ec765
Update scripts for langfuse
mdciri Dec 5, 2024
ab03a59
Update prompts
mdciri Dec 5, 2024
63dc519
Update pytest
mdciri Dec 5, 2024
c9918f7
Update chatbot
mdciri Dec 6, 2024
6baa01d
chatbot: langfuse score
batdevis Dec 6, 2024
979f9a8
Update modules.chatbot
mdciri Dec 9, 2024
695dfa8
Update embedding model
mdciri Dec 10, 2024
bedb719
Update feedback
mdciri Dec 18, 2024
9731495
Merge branch 'main' into feature/langfuse-chatbot/cai-271
christian-calabrese Dec 18, 2024
891c058
feat: add missing infra parts
christian-calabrese Dec 18, 2024
f52491a
chore: add changeset
christian-calabrese Dec 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "apps/nextjs-website/.tmp-docs"]
path = apps/nextjs-website/.tmp-docs
url = https://github.com/pagopa/devportal-docs.git
url = https://github.com/pagopa/devportal-docs.git
1 change: 1 addition & 0 deletions apps/chatbot/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
build-devp
.pytest_cache
load-test
*.ipynb
19 changes: 18 additions & 1 deletion apps/chatbot/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ environment=local
CORS_DOMAINS=["*"]
PYTHONPATH=app-path
LOG_LEVEL=DEBUG

CHB_AWS_ACCESS_KEY_ID=...
CHB_AWS_SECRET_ACCESS_KEY=...
CHB_AWS_DEFAULT_REGION=eu-south-1
Expand All @@ -20,12 +21,28 @@ CHB_MODEL_ID=...
CHB_MODEL_TEMPERATURE=...
CHB_MODEL_MAXTOKENS=...
CHB_EMBED_MODEL_ID=...
CHB_USE_CHAT_ENGINE=True
CHB_ENGINE_SIMILARITY_TOPK=...
CHB_ENGINE_SIMILARITY_CUTOFF=...
CHB_ENGINE_USE_ASYNC=True
CHB_ENGINE_USE_STREAMING=...
CHB_ENGINE_USE_CHAT_ENGINE=...
CHB_QUERY_TABLE_PREFIX=chatbot-local
CHB_DYNAMODB_URL=http://locahost:8080
CHB_USE_PRESIDIO=True
CHB_SESSION_MAX_DURATION_DAYS=1

DATABASE_URL=postgresql://postgres:postgres@postgres:5432/postgres
LANGFUSE_HOST=http://langfuse:3000
NEXTAUTH_SECRET=mysecret
SALT=mysalt
ENCRYPTION_KEY=0000000000000000000000000000000000000000000000000000000000000000
LANGFUSE_ORG_ID=...
LANGFUSE_ORG_NAME=...
LANGFUSE_PROJECT_ID=...
LANGFUSE_PROJECT_NAME=...
LANGFUSE_PUBLIC_KEY=...
LANGFUSE_SECRET_KEY=...
[email protected]
LANGFUSE_USER_NAME=User
LANGFUSE_USER_PASSWORD=abcd1234
LANGFUSE_TAG=development
42 changes: 30 additions & 12 deletions apps/chatbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ Even though the provider is the Google one, we stored its API key in AWS. So, be

The Retrieval-Augmented Generation (RAG) was implemented using [llama-index](https://docs.llamaindex.ai/en/stable/). All the parameters and prompts used are stored in `config`.

The monitoring is done using [Langfuse](https://langfuse.com/) deployed on AWS.

## Environment Variables

Create a `.env` file inside this folder and store the environment variables listed in `.env.example`.

cp .env.example .env

Note that the envirables inside `.env` file should be pointing to the AWS infrastructure.s

## Virtual environment

Before creating your virtual environment, install [Miniconda](https://docs.anaconda.com/miniconda/#quick-command-line-install) and [Poetry](https://python-poetry.org/docs/main#installation) on your device.
Expand All @@ -36,30 +42,42 @@ In this way, `PYTHONPATH` points to where the Python packages and modules are, n

To reach the remote redis instance, it is necessary to open a tunnel:

```
./scripts/redis-tunnel.sh
```

Verify that the HTML files that compose the Developer Portal documentation exist in a directory. Otherwise create the documentation. Once you have the documentation directory ready, put its path in `params` and, in the end, create the vector index doing:

```
python src/modules/create_vector_index.py --params config/params.yaml
```

This script reads the documentation, split it into chucks with gerarchical organization, and stores it on Redis.

Check out the params in order to store your vector index accordingly.

## test
## Test

Run the tests for the chatbot and the APIs running:

pytest

## Docker

In order to run the chatbot locally for the first time, you need to:

- install [Docker Compose](https://docs.docker.com/compose/install/),
- create `.env.local` file running:

cp .env.example .env.local

and fill it in,

- run the following bash scripts:

```
pytest
```
./docker/docker-compose-build-local.sh
./docker/docker-compose-run-create_index.sh

## Web App
In this way, the docker images are built and the vector index is stored in Redis.

python src/webapp/app.py
Now you can start the API running:

This scripts uses [Gradio](https://www.gradio.app/) framework to lunch a web application at the [default link](http://127.0.0.1:7860) where the user can interact with the chatbot.
./docker/docker-compose-up-api.sh

Both [`user icon`](https://www.flaticon.com/free-icon/user_1077012) and [`chatbot icon`](https://www.flaticon.com/free-icon/chatbot_8943377) are made by [Freepick](https://www.freepik.com/) and they were downloaded from [Flaticon](https://www.flaticon.com/).
Note that the `docker/compose.yaml` needs `.env.local` file with the correct environment variables.
8 changes: 7 additions & 1 deletion apps/chatbot/config/params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ config_presidio:
allow_list:
- Discovery
- discovery
- rispondo
- Rispondo
- Rif
- SEND
- send
Expand All @@ -61,8 +63,12 @@ config_presidio:
- Gpd
- STATO
- stato
- PagoPA
- PagoPA
- pagoPA
- Devportal
- devPortal
- DevPortal
- devportal
- pagopa
- Pagopa
- Firma con IO
Expand Down
10 changes: 6 additions & 4 deletions apps/chatbot/docker/app.local.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ RUN pip install --upgrade pip \
&& pip install poetry awscli

WORKDIR /app
COPY pyproject.toml .
COPY poetry.lock .
COPY ./pyproject.toml .
COPY ./poetry.lock .
COPY ./src ./src
COPY ./config ./config
COPY ./scripts ./scripts

RUN poetry config virtualenvs.create false
RUN poetry install

COPY . .

CMD ["fastapi", "dev", "src/app/main.py", "--port", "8080", "--host", "0.0.0.0"]
8 changes: 6 additions & 2 deletions apps/chatbot/docker/compose.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ services:
- AWS_SECRET_ACCESS_KEY=dummy
- AWS_DEFAULT_REGION=local
healthcheck:
test: ["CMD-SHELL", '[ "$(curl -s -o /dev/null -I -w ''%{http_code}'' http://localhost:8000)" == "400" ]']
test:
[
"CMD-SHELL",
'[ "$(curl -s -o /dev/null -I -w ''%{http_code}'' http://localhost:8000)" == "400" ]',
]
interval: 10s
timeout: 10s
retries: 10
networks:
- ntw

redis:
image: redis/redis-stack:7.2.0-v13
networks:
Expand Down
45 changes: 43 additions & 2 deletions apps/chatbot/docker/compose.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
services:
api:
build:
context: ..
dockerfile: docker/app.local.Dockerfile
env_file: ../.env.local
command: "./scripts/run.local.sh"
ports:
- "8080:8080"
Expand All @@ -16,6 +16,26 @@ services:
condition: service_started
dynamodb:
condition: service_started
langfuse:
condition: service_started
env_file: ../.env.local

networks:
- ntw

postgres:
image: postgres:17.2-alpine
restart: always
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 3s
timeout: 3s
retries: 10
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
env_file: ../.env.local
networks:
- ntw

Expand All @@ -35,6 +55,8 @@ services:
ports:
- "6379:6379"
- "8001:8001"
volumes:
- redis_data:/data
networks:
- ntw

Expand All @@ -47,13 +69,32 @@ services:
volumes:
- ..:/app
- ../../nextjs-website/out:/app/build-devp/out
command: "python src/modules/create_vector_index.py --params config/params.yaml"
command: >
"python src/modules/create_vector_index.py --params config/params.yaml"
tty: true
depends_on:
redis:
condition: service_started
env_file: ../.env.local
networks:
- ntw

langfuse:
image: langfuse/langfuse:2
depends_on:
postgres:
condition: service_healthy
ports:
- "3001:3000"
env_file: ../.env.local
networks:
- ntw

volumes:
postgres_data:
driver: local
redis_data:
driver: local

networks:
ntw:
2 changes: 1 addition & 1 deletion apps/chatbot/docker/docker-compose-build-local.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
docker compose --env-file .env -f docker/compose.yaml -p chatbot build
docker compose -f docker/compose.yaml -p chatbot build
2 changes: 1 addition & 1 deletion apps/chatbot/docker/docker-compose-run-create_index.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
docker compose --env-file .env -f docker/compose.yaml -p chatbot run create_index
docker compose -f docker/compose.yaml -p chatbot run create_index

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading
Loading