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

RAG Chatbot on docs.md #160

Open
wants to merge 377 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
377 commits
Select commit Hold shift + click to select a range
b3e4760
fast api renamed
raphaeltm May 29, 2024
8d51d64
Merge pull request #56 from DefangLabs/rename-fastapi
raphaeltm May 29, 2024
e6d5d0e
updated the prod command
Chrisyhjiang May 29, 2024
1cebf5b
Merge branch 'angular' of https://github.com/DefangLabs/samples into …
Chrisyhjiang May 29, 2024
fdc79ed
Merge branch 'main' into sample-repo-automation
raphaeltm May 29, 2024
6f2dbb8
testrun
raphaeltm May 29, 2024
f9e5efd
run another test
raphaeltm May 29, 2024
611b081
change token
raphaeltm May 29, 2024
c9589b3
update subtree push
raphaeltm May 29, 2024
b6ee796
update subtree push
raphaeltm May 29, 2024
ed187da
took out unecessary Dockerfile.dev
Chrisyhjiang May 29, 2024
a22a85f
took out the log files
Chrisyhjiang May 29, 2024
7aa059b
update subtree push
raphaeltm May 29, 2024
8cd87ae
update subtree push
raphaeltm May 29, 2024
9fb82de
update subtree push
raphaeltm May 29, 2024
ce95916
update subtree push
raphaeltm May 29, 2024
14baf8c
fastapi-postgres
aRorschach May 29, 2024
fb18edd
split
raphaeltm May 29, 2024
153810f
trying something...
raphaeltm May 29, 2024
f7d1afb
trying something...
raphaeltm May 29, 2024
3ae686d
trying something...
raphaeltm May 29, 2024
d79ecef
success plz
raphaeltm May 29, 2024
6740878
success plz
raphaeltm May 29, 2024
225fd3c
this should be the last time...
raphaeltm May 29, 2024
2784d7e
this should be the last time...
raphaeltm May 29, 2024
53bb7df
this should be the last time...
raphaeltm May 29, 2024
8ddf730
Merge pull request #39 from DefangLabs/angular
raphaeltm May 29, 2024
0e6ebba
brought in main
Chrisyhjiang May 29, 2024
91b9cc9
Merge pull request #45 from DefangLabs/sailsjs-postgres
Chrisyhjiang May 29, 2024
1b117cd
remove .gitignore
aRorschach May 29, 2024
8aac276
please, please, work.
raphaeltm May 29, 2024
4470e0a
Merge remote-tracking branch 'origin/main' into sample-repo-automation
raphaeltm May 29, 2024
d5a18c8
testing
raphaeltm May 29, 2024
5db73e2
test multiple samples
raphaeltm May 29, 2024
4fb0395
checkout current branch afterward
raphaeltm May 29, 2024
3a2f0a6
checkout gh ref
raphaeltm May 29, 2024
950f3a4
current branch set
raphaeltm May 29, 2024
5c78918
next template
raphaeltm May 29, 2024
8865c65
update actions
raphaeltm May 29, 2024
b0074e7
Merge pull request #38 from DefangLabs/sample-repo-automation
raphaeltm May 30, 2024
c24ae93
publish sample template on main
raphaeltm May 30, 2024
69bd57b
Merge pull request #58 from DefangLabs/sample-repo-automation
raphaeltm May 30, 2024
630cacd
default to main
raphaeltm May 30, 2024
e22c088
Merge pull request #59 from DefangLabs/sample-repo-automation
raphaeltm May 30, 2024
39db0bd
sample next template
raphaeltm May 30, 2024
69b39ea
Merge pull request #60 from DefangLabs/sample-repo-automation
raphaeltm May 30, 2024
427d8ce
sample next template
raphaeltm May 30, 2024
4057d85
sample next template
raphaeltm May 30, 2024
3cdf9cd
Merge pull request #61 from DefangLabs/sample-repo-automation
raphaeltm May 30, 2024
5da2baf
sample next template
raphaeltm May 30, 2024
c364cd2
Merge branch 'main' of github.com:DefangLabs/samples
raphaeltm May 30, 2024
b65a425
update next
raphaeltm May 30, 2024
79a1afd
update nextjs readme
raphaeltm May 30, 2024
0a931b0
restructure
aRorschach May 30, 2024
0b5f871
Merge branch 'main' into nodejs-react-postgres
raphaeltm May 31, 2024
0d4f608
Update compose.yaml
raphaeltm May 31, 2024
09da858
Update .env
raphaeltm May 31, 2024
974e7a4
changes to get dev working
raphaeltm May 31, 2024
2e8b6c7
set .env to localhost:3010
raphaeltm May 31, 2024
ad94983
fix for template manager
raphaeltm May 31, 2024
8e34799
Merge pull request #32 from DefangLabs/nodejs-react-postgres
raphaeltm May 31, 2024
2a6bbd7
functional elysia sample
raphaeltm Jun 1, 2024
994a0d3
Merge pull request #63 from DefangLabs/25-elysia-sample
raphaeltm Jun 1, 2024
7da9397
fixed readme no mentions of Hasura
Chrisyhjiang Jun 5, 2024
2faa23b
fixed grammar
Chrisyhjiang Jun 5, 2024
2eef7ed
reorg and working
raphaeltm Jun 5, 2024
1f099d5
Update README.md
raphaeltm Jun 5, 2024
eab08eb
Merge pull request #102 from DefangLabs/101-fix-sails-example-readme
raphaeltm Jun 5, 2024
54f0f09
Merge pull request #57 from DefangLabs/fastapi-postgres
raphaeltm Jun 5, 2024
50c838c
fix readme to include local dev
raphaeltm Jun 5, 2024
c0ba32d
Merge pull request #105 from DefangLabs/fastapi-postgres
raphaeltm Jun 5, 2024
f616191
working version of the langchain sample need to write README
Chrisyhjiang Jun 5, 2024
af5d2a7
changed compose and README
Chrisyhjiang Jun 5, 2024
c69ce5a
changed readme
Chrisyhjiang Jun 6, 2024
e085adf
reorganized files
Chrisyhjiang Jun 6, 2024
e61d067
minor readme tweak
Chrisyhjiang Jun 6, 2024
74a76a7
golang http refractoring
Chrisyhjiang Jun 6, 2024
994daf2
finished refractoring for golang-http-form
Chrisyhjiang Jun 6, 2024
8a1d438
changed compose yaml
Chrisyhjiang Jun 6, 2024
9a57524
fixed golang mongodb
Chrisyhjiang Jun 6, 2024
f2e78a7
finished golang-openai
Chrisyhjiang Jun 6, 2024
df801fd
readme change
Chrisyhjiang Jun 6, 2024
d5ae4ab
fixed golang rest api
Chrisyhjiang Jun 6, 2024
d91eb85
fixed slackbot and s3 for go
Chrisyhjiang Jun 6, 2024
556e479
fixed golang-s3
Chrisyhjiang Jun 6, 2024
25a63f4
nextjs blog
Chrisyhjiang Jun 6, 2024
b9dc435
nextjs boilerplate fixed
Chrisyhjiang Jun 6, 2024
986cc2f
nextjs documentation
Chrisyhjiang Jun 6, 2024
cd84d01
nextjs documentation README change
Chrisyhjiang Jun 6, 2024
7b0705f
nodejs-chatroom fixed
Chrisyhjiang Jun 6, 2024
b4da94f
nodejs express refractored
Chrisyhjiang Jun 6, 2024
14e3682
check in reorganizations
Chrisyhjiang Jun 6, 2024
875bf7e
nodejs rest-api
Chrisyhjiang Jun 6, 2024
042de37
refractored nodejs-as3
Chrisyhjiang Jun 6, 2024
20798f7
flask form refractored
Chrisyhjiang Jun 6, 2024
2da8d0c
working locally
raphaeltm Jun 6, 2024
18b8dfa
much better bullmq sample
raphaeltm Jun 7, 2024
6e9f93d
refractored implicit flask
Chrisyhjiang Jun 7, 2024
e4a306d
python-minimal refractored
Chrisyhjiang Jun 7, 2024
6b1bb14
python openai refractored
Chrisyhjiang Jun 7, 2024
2a6e142
refractored python rest api
Chrisyhjiang Jun 7, 2024
535ead5
refractored python s3
Chrisyhjiang Jun 7, 2024
b9bb83a
django sample
Chrisyhjiang Jun 7, 2024
c0ed46b
refractor django postgres
Chrisyhjiang Jun 7, 2024
ac51f78
sample request to test
raphaeltm Jun 7, 2024
8b13b87
remove healthcheck response
raphaeltm Jun 7, 2024
3e0f4c7
remove healthcheck response
raphaeltm Jun 7, 2024
61dcea0
show replicas 2
raphaeltm Jun 7, 2024
f40228e
remove version:
lionello Jun 8, 2024
2665566
Merge pull request #111 from DefangLabs/lio-version
lionello Jun 8, 2024
9e450f6
new refractoring done to README
Chrisyhjiang Jun 12, 2024
9bb74a6
update readme and add project name
raphaeltm Jun 12, 2024
89a2ec9
changed back into app waiting for Lio and Edward to add in projects l…
Chrisyhjiang Jun 12, 2024
a0ae044
Merge remote-tracking branch 'origin/main' into 107-reorg-older-samples
Chrisyhjiang Jun 12, 2024
5f30e21
new changes to README
Chrisyhjiang Jun 12, 2024
f1a5e04
took out Defang in README
Chrisyhjiang Jun 12, 2024
f13023e
update compose files to remove name and version
raphaeltm Jun 12, 2024
d611c37
amend template-manager for new DefangSamples org
lionello Jun 12, 2024
882a652
update template manager to point to DefangSamples
raphaeltm Jun 12, 2024
7863e86
Merge branch 'update-sample-deploy' into 110-bullmq-bull-board-redis-…
raphaeltm Jun 12, 2024
85f60ae
minor reame tweak
raphaeltm Jun 13, 2024
cdeaa39
renamed service 1
Chrisyhjiang Jun 13, 2024
d0fd403
Merge branch 'main' into lio-samples-org
raphaeltm Jun 13, 2024
c0b1820
Merge pull request #116 from DefangLabs/lio-samples-org
raphaeltm Jun 13, 2024
2265a9d
add gh actions to all samples
raphaeltm Jun 14, 2024
519ee4d
Merge branch '107-reorg-older-samples' of github.com:DefangLabs/sampl…
raphaeltm Jun 14, 2024
829c334
reorg rails and svelte-mysql
raphaeltm Jun 14, 2024
e91c92e
make titles consistent
raphaeltm Jun 14, 2024
1a99a58
remove compose project names
raphaeltm Jun 14, 2024
5126f3f
make readmes consistent
raphaeltm Jun 14, 2024
ff065f1
Merge pull request #108 from DefangLabs/107-reorg-older-samples
raphaeltm Jun 14, 2024
bab43ed
add Dockerfile to huginn readme languages
raphaeltm Jun 14, 2024
2671543
add GPU to python implicit
raphaeltm Jun 14, 2024
25b2fab
pulumi remix postgres updated
raphaeltm Jun 14, 2024
8ac8d1b
remove references to service1
raphaeltm Jun 17, 2024
0d91b78
Merge pull request #121 from DefangLabs/113-warning-in-nodejs-http-sa…
raphaeltm Jun 17, 2024
50fbeff
C# .NET Sample
Chrisyhjiang Jun 18, 2024
229e880
C# sample with .NET
Chrisyhjiang Jun 18, 2024
89a042e
rename redis service
lionello Jun 21, 2024
2655ea2
support private repos in deploy action
lionello Jun 21, 2024
bfb8631
Merge pull request #127 from DefangLabs/lio-deploy-private-repo
lionello Jun 21, 2024
ca70e90
gpu tags
lionello Jun 23, 2024
6600fbb
Merge pull request #128 from DefangLabs/lio-tags
raphaeltm Jun 23, 2024
6ea7525
Merge branch 'main' into 110-bullmq-bull-board-redis-sample
raphaeltm Jun 23, 2024
ff945c3
add contents read
raphaeltm Jun 23, 2024
138bd39
Merge branch '110-bullmq-bull-board-redis-sample' of github.com:Defan…
raphaeltm Jun 23, 2024
56fa611
Merge pull request #114 from DefangLabs/110-bullmq-bull-board-redis-s…
raphaeltm Jun 23, 2024
9e3e724
update compose
raphaeltm Jun 23, 2024
ea8ef95
Merge pull request #131 from DefangLabs/110-bullmq-bull-board-redis-s…
raphaeltm Jun 23, 2024
ba92152
fix compose errors
lionello Jun 24, 2024
21ae962
Merge branch 'main' of https://github.com/DefangLabs/samples
Chrisyhjiang Jun 25, 2024
c683c00
validate config files
lionello Jun 26, 2024
d38dd7b
Merge pull request #133 from DefangLabs/lio-warnings
raphaeltm Jun 26, 2024
5cad6dd
featherjs application
Chrisyhjiang Jun 26, 2024
bf82761
Merge branch 'main' of https://github.com/DefangLabs/samples
Chrisyhjiang Jun 27, 2024
18bd62a
Update README.md
Prakash-Sundaresan Jul 4, 2024
94d10f8
added empty debug field for testing if the parsing logic for env logi…
Chrisyhjiang Jul 4, 2024
f6b3723
removed config stuff
Chrisyhjiang Jul 4, 2024
0c24328
tookout debug env var
Chrisyhjiang Jul 4, 2024
23657ae
added lines in between README.md
Chrisyhjiang Jul 5, 2024
f2ea403
fix healthcheck for flask
lionello Jul 5, 2024
9b330eb
Merge pull request #141 from DefangLabs/lio-flask-healthcheck
edwardrf Jul 6, 2024
0f89b12
finished checking the FeatherJS sample
Chrisyhjiang Jul 8, 2024
e1f6653
flask app with langchain fixed
Chrisyhjiang Jul 8, 2024
f5d3db5
fixed the context
Chrisyhjiang Jul 8, 2024
2403f51
changed README content
Chrisyhjiang Jul 8, 2024
382ca83
Merge pull request #140 from DefangLabs/sailsjs-postgres
raphaeltm Jul 10, 2024
0187990
Merge pull request #139 from DefangLabs/Prakash-Sundaresan-readme-1
raphaeltm Jul 10, 2024
f1a121f
fixed README
Chrisyhjiang Jul 10, 2024
9a3d219
fixed README
Chrisyhjiang Jul 10, 2024
5186a01
changes to README
Chrisyhjiang Jul 10, 2024
1e540f2
fixed README
Chrisyhjiang Jul 10, 2024
1f91c22
README changes
Chrisyhjiang Jul 10, 2024
4b201c1
changes to README
Chrisyhjiang Jul 10, 2024
1d0e5c5
Merge branch 'main' of https://github.com/DefangLabs/samples
Chrisyhjiang Jul 12, 2024
bc79aa9
Merge pull request #109 from DefangLabs/16-langchain-sample
raphaeltm Jul 12, 2024
e6f523e
Update README.md
raphaeltm Jul 12, 2024
7ce2d9c
Merge pull request #136 from DefangLabs/73-feathersjs-sample
raphaeltm Jul 12, 2024
4926a03
Merge branch 'main' of https://github.com/DefangLabs/samples
Chrisyhjiang Jul 12, 2024
6c76146
Update README.md
raphaeltm Jul 12, 2024
79f938d
Merge pull request #123 from Chrisyhjiang/c#
raphaeltm Jul 12, 2024
0216c3c
Merge branch 'main' of https://github.com/DefangLabs/samples
Chrisyhjiang Jul 12, 2024
706b917
updates to README to normalize capitalization and other display incon…
Chrisyhjiang Jul 16, 2024
b085996
made nodejs one word to address the parsing issue
Chrisyhjiang Jul 16, 2024
c3a9bd2
fixed inconsistencies
Chrisyhjiang Jul 16, 2024
f643e1c
Http -> http
Chrisyhjiang Jul 16, 2024
2d88c82
langchain => LangChain
Chrisyhjiang Jul 16, 2024
5f77e2a
normalization
Chrisyhjiang Jul 18, 2024
211cf06
more normalization
Chrisyhjiang Jul 18, 2024
8b14b03
more normalization
Chrisyhjiang Jul 18, 2024
5747e30
fixed more README
Chrisyhjiang Jul 19, 2024
885d68a
adsf
Chrisyhjiang Jul 19, 2024
5ca0c15
finished revision
Chrisyhjiang Jul 19, 2024
7a41a6b
fixed
Chrisyhjiang Jul 19, 2024
134a42d
Merge pull request #149 from Chrisyhjiang/normalization
raphaeltm Jul 24, 2024
daaf69d
rm noco
raphaeltm Jul 24, 2024
5f56a13
rm Node.js from languages in bullmq
raphaeltm Jul 24, 2024
60089a4
compose validation temporarily disabled
raphaeltm Jul 24, 2024
71d244f
trial
Chrisyhjiang Jul 24, 2024
8a85685
fixed READMEs to have tags include languages and revert language capi…
Chrisyhjiang Jul 24, 2024
4c6163b
fixed wrong READMEs
Chrisyhjiang Jul 24, 2024
8b477fc
Merge pull request #153 from DefangLabs/typescript-addendum
raphaeltm Jul 24, 2024
1bef62a
add space after languages
raphaeltm Jul 24, 2024
68ebb8a
Merge remote-tracking branch 'origin' into RAG
Chrisyhjiang Jul 25, 2024
f45175c
current version
Chrisyhjiang Jul 26, 2024
56d5077
current version
Chrisyhjiang Jul 26, 2024
0de5976
RAG system working
Chrisyhjiang Jul 29, 2024
6610f11
project info
Chrisyhjiang Jul 29, 2024
9824194
working version of RAG but no query processing
Chrisyhjiang Jul 29, 2024
72d76f8
add reservations placeholder
lionello Jul 9, 2024
3431152
removed env var
Chrisyhjiang Jul 30, 2024
a77b10b
working version of processing query
Chrisyhjiang Jul 30, 2024
712acfd
more accurate version of query processing
Chrisyhjiang Jul 30, 2024
074239a
working version of RAG chatbot
Chrisyhjiang Jul 30, 2024
ec547e0
resolve merge conflict accepted main branch
Chrisyhjiang Jul 30, 2024
320e11e
added README
Chrisyhjiang Jul 30, 2024
77e4dff
changed readme
Chrisyhjiang Jul 30, 2024
53d4cc4
changed README
Chrisyhjiang Jul 30, 2024
9fcf7c2
parsed the docs website using the sitemap XML
Chrisyhjiang Jul 31, 2024
7811970
commit save openai migrate
Chrisyhjiang Jul 31, 2024
683ecde
before openai migrate
Chrisyhjiang Jul 31, 2024
632b642
updated requirements
Chrisyhjiang Jul 31, 2024
d78b778
used model 4
Chrisyhjiang Jul 31, 2024
7903e7e
Merge branch 'main' of https://github.com/DefangLabs/samples into RAG
Chrisyhjiang Jul 31, 2024
6ccaeb9
README edits
Chrisyhjiang Jul 31, 2024
f3d8d39
added some changes to make sure the context is correct
Chrisyhjiang Aug 1, 2024
2022717
added a LRU cache for common queries
Chrisyhjiang Aug 1, 2024
aa6b16d
added caching
Chrisyhjiang Aug 1, 2024
e2b9fad
improved UI
Chrisyhjiang Aug 1, 2024
39017f5
normalize query for caching and improved UI to match the main Defang …
Chrisyhjiang Aug 1, 2024
d73fc30
README updates
Chrisyhjiang Aug 1, 2024
72ef6c2
Merge branch 'main' of https://github.com/DefangLabs/samples into RAG
Chrisyhjiang Aug 6, 2024
f7fe8d8
added md display as well as increased max tokens to 1024 to finish th…
Chrisyhjiang Aug 7, 2024
9e6c726
fixed incorrect output
Chrisyhjiang Aug 7, 2024
0729785
enhanced knowledge base to parse directly from the docs repo with the…
Chrisyhjiang Aug 7, 2024
f16fc46
get knowledgebase implementation
Chrisyhjiang Aug 7, 2024
5eda5a2
removed debugging
Chrisyhjiang Aug 7, 2024
e5ce451
reparsed knowledge base and added in fuzzy matching logic with the ab…
Chrisyhjiang Aug 13, 2024
53229ba
parsing logic
Chrisyhjiang Aug 13, 2024
592012b
updated the knowledge base and rag system
Chrisyhjiang Aug 14, 2024
ab26514
updated retrieval logic
Chrisyhjiang Aug 14, 2024
3e2be81
testing
Chrisyhjiang Aug 15, 2024
9b27e51
added context limit
Chrisyhjiang Aug 20, 2024
19b5ac6
changes to parsing and knowledge base
Chrisyhjiang Aug 21, 2024
0bd647c
fixed some stuff
Chrisyhjiang Aug 21, 2024
f5abd01
Merge branch 'main' of https://github.com/DefangLabs/samples into RAG
Chrisyhjiang Aug 21, 2024
ea69b4c
fixed typo and better UI (no typing when question being processed)
Chrisyhjiang Aug 22, 2024
2fe8de2
added deploy.yaml
Chrisyhjiang Aug 22, 2024
2af9f08
add openai config in deploy action for 1-click
raphaeltm Aug 22, 2024
a36cdc9
make it prod ready
Chrisyhjiang Aug 22, 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
1 change: 1 addition & 0 deletions samples/RAG-chatbot/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm
10 changes: 10 additions & 0 deletions samples/RAG-chatbot/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"features": {
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
}
}
1 change: 1 addition & 0 deletions samples/RAG-chatbot/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myenv
24 changes: 24 additions & 0 deletions samples/RAG-chatbot/.github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Deploy

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write

steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Deploy
uses: DefangLabs/[email protected]
with:
config-env-vars: OPENAI_API_KEY
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
1 change: 1 addition & 0 deletions samples/RAG-chatbot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myenv/
38 changes: 38 additions & 0 deletions samples/RAG-chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Scikit RAG + OpenAI

This sample demonstrates how to deploy a Flask-based Retrieval-Augmented Generation (RAG) chatbot using OpenAI's GPT model. The chatbot retrieves relevant documents from a knowledge base using scikit-learn and Sentence Transformers and then generates responses using OpenAI's GPT model. There is an LRU caching scheme of 128 queries.

## Prerequisites

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticated with your AWS account
3. (Optional - for local development) [Docker CLI](https://docs.docker.com/engine/install/)

## Deploying

1. Open the terminal and type `defang login`
2. Type `defang compose up` in the CLI.
3. Your app will be running within a few minutes.

## Local Development

1. Clone the repository.
2. Create a `.env` file in the root directory and set your OpenAI API key or add the OPENAI_API_KEY into your .zshrc or .bashrc file:
3. Run the command `docker compose up --build` to spin up a docker container for this RAG chatbot

## Configuration

- The knowledge base is acquired via parsing an sitemap located at "https://docs.defang.io/sitemap.xml".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this accurate @Chrisyhjiang ? I thought you were using markdown files. Or is that just in the actual ask.defang.io implementation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks out of date, the docs chatbot has the final readme, I think it's because I never ended up editing and moving everything in the docs chatbot over to the real samples repo

- The file `scrape_sitemap.py` parses every webpage as specified into paragraphs and writes to `knowledge_base.json` for the RAG retrieval.
- To obtain your own knowledge base, either use another sitemap or write your own parsing scheme to parse into knowledge_base.json.
- A least recently used (LRU) caching scheme is also in place as can be seen in `rag_system.py`. This caches common queries to have a faster response time. Feel free to adjust as needed.

---

Title: Scikit RAG + OpenAI

Short Description: A short hello world application demonstrating how to deploy a Flask-based Retrieval-Augmented Generation (RAG) chatbot using OpenAI's GPT model onto Defang.

Tags: Flask, Scikit, Python, RAG, OpenAI, GPT, Machine Learning

Languages: python
28 changes: 28 additions & 0 deletions samples/RAG-chatbot/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file first to leverage Docker's cache
COPY requirements.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

RUN pip install gunicorn

# Install additional packages
RUN pip install sentence-transformers openai

# Copy the current directory contents into the container at /app
COPY . /app

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV FLASK_APP=app.py

# Run app.py when the container launches
CMD ["flask", "run", "--host=0.0.0.0"]
36 changes: 36 additions & 0 deletions samples/RAG-chatbot/app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from flask import Flask, request, jsonify, render_template
from rag_system import rag_system

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
query = request.form.get('query')
if not query:
return render_template('index.html', query=None, response="No query provided")

try:
response = rag_system.answer_query(query)
return render_template('index.html', query=query, response=response)
except Exception as e:
print(f"Error in /ask endpoint: {e}")
return render_template('index.html', query=query, response="Internal Server Error")
return render_template('index.html', query=None, response=None)

@app.route('/ask', methods=['POST'])
def ask():
data = request.get_json()
query = data.get('query')
if not query:
return jsonify({"error": "No query provided"}), 400

try:
response = rag_system.answer_query(query)
return jsonify({"response": response})
except Exception as e:
print(f"Error in /ask endpoint: {e}")
return jsonify({"error": "Internal Server Error"}), 500

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
442 changes: 442 additions & 0 deletions samples/RAG-chatbot/app/knowledge_base.json

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions samples/RAG-chatbot/app/parse_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import re
import json
import os

# Function to reset knowledge_base.json
def reset_knowledge_base():
with open('knowledge_base.json', 'w') as output_file:
json.dump([], output_file)

def parse_markdown_file_to_json(file_path):
try:
# Load existing content if the file exists
with open('knowledge_base.json', 'r') as existing_file:
json_output = json.load(existing_file)
current_id = len(json_output) + 1 # Start ID from the next available number
except (FileNotFoundError, json.JSONDecodeError):
# If the file doesn't exist or is empty, start fresh
json_output = []
current_id = 1

with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()

# Skip the first 5 lines
markdown_content = "".join(lines[5:])

# First pass: Determine headers for 'about' section
sections = []
current_section = {"about": [], "text": []}
has_main_header = False

for line in markdown_content.split('\n'):
header_match = re.match(r'^(#{1,6}|\*\*+)\s+(.*)', line) # Match `#`, `##`, ..., `######` and `**`
if header_match:
header_level = len(header_match.group(1).strip())
header_text = header_match.group(2).strip()

if header_level == 1 or header_match.group(1).startswith('**'): # Treat `**` as a main header
if current_section["about"] or current_section["text"]:
sections.append(current_section)
current_section = {"about": [header_text], "text": []}
has_main_header = True
else:
if has_main_header:
current_section["about"].append(header_text)
else:
if header_level == 2:
if current_section["about"] or current_section["text"]:
sections.append(current_section)
current_section = {"about": [header_text], "text": []}
else:
current_section["about"].append(header_text)
else:
current_section["text"].append(line.strip())

if current_section["about"] or current_section["text"]:
sections.append(current_section)

# Second pass: Combine text while ignoring headers and discard entries with empty 'about' or 'text'
for section in sections:
about = ", ".join(section["about"])
text = " ".join(line for line in section["text"] if line)

if about and text: # Only insert if both 'about' and 'text' are not empty
json_output.append({
"id": current_id,
"about": about,
"text": text
})
current_id += 1

# Write the augmented JSON output to knowledge_base.json
with open('knowledge_base.json', 'w', encoding='utf-8') as output_file:
json.dump(json_output, output_file, indent=2, ensure_ascii=False)

def parse_cli_markdown(file_path):
try:
# Load existing content if the file exists
with open('knowledge_base.json', 'r') as existing_file:
json_output = json.load(existing_file)
current_id = len(json_output) + 1 # Start ID from the next available number
except (FileNotFoundError, json.JSONDecodeError):
# If the file doesn't exist or is empty, start fresh
json_output = []
current_id = 1

with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()

if len(lines) < 5:
print(f"File {file_path} does not have enough lines to parse.")
return

# Extract 'about' from the 5th line (index 4)
about = lines[4].strip()

# Combine all remaining lines after the first 5 lines into 'text'
text_lines = lines[5:]
text = "".join(text_lines).strip()

# Only append if both 'about' and 'text' are not empty
if about and text:
json_output.append({
"id": current_id,
"about": about,
"text": text
})
current_id += 1

# Write the augmented JSON output to knowledge_base.json
with open('knowledge_base.json', 'w', encoding='utf-8') as output_file:
json.dump(json_output, output_file, indent=2, ensure_ascii=False)

def recursive_parse_directory(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if filename.lower().endswith('.md'):
if 'cli' in dirpath.lower() or 'cli' in filename.lower():
parse_cli_markdown(file_path)
else:
parse_markdown_file_to_json(file_path)

# Example usage:
if __name__ == "__main__":
reset_knowledge_base() # Reset knowledge_base.json to empty at the start
recursive_parse_directory('/Users/chris/Desktop/tmp') # Parse the entire directory
print("Parsing completed successfully.")
Loading
Loading