-
Notifications
You must be signed in to change notification settings - Fork 16
feat: upgraded rag model from ollama to chatgpt #256
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
Open
yk-chenyang
wants to merge
3
commits into
pingcap:main
Choose a base branch
from
yk-chenyang:feature/openai-ver-rag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,67 @@ | ||
| # Chatbot with RAG | ||
|
|
||
| * An RAG-Driven AI Chatbot that allows users to manage a private knowledge base by feeding private files, therefore obtaining more accurate, reliable, and secure answers about any private fields that simple LLMs cannot answer | ||
| * Use `pytidb` to connect to TiDB | ||
| * Use `openai` to deploy embedding model and response generation | ||
| * Use Streamlit as web ui | ||
|
|
||
| ## Prerequisites | ||
| * Python 3.10+ | ||
| * A TiDB Cloud Serverless cluster: Create a free cluster here: tidbcloud.com | ||
| * OpenAI API key: Go to Open AI to get your own API key | ||
| * Google Auth: Create a web application in Google Cloud Console (https://docs.streamlit.io/develop/tutorials/authentication/google) | ||
|
|
||
| ## How to run | ||
|
|
||
| **Step1**: Clone the repo | ||
|
|
||
| ```bash | ||
| git clone https://github.com/pingcap/pytidb.git | ||
| cd examples/chatbot_with_rag | ||
| ``` | ||
|
|
||
| **Step2**: Install the required packages and setup environment | ||
|
|
||
| ```bash | ||
| python -m venv .venv | ||
| source .venv/bin/activate | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| **Step3**: Set up environment to connect to storage | ||
|
|
||
| As you are using a local TiDB server, you can set up the environment variable like this: | ||
| (You can also referense) | ||
|
|
||
| ```bash | ||
| cat > .env <<EOF | ||
| OPENAI_API_KEY= | ||
| TIDB_HOST=localhost | ||
| TIDB_PORT=4000 | ||
| TIDB_USERNAME=root | ||
| TIDB_PASSWORD= | ||
| TIDB_DATABASE=test | ||
| EOF | ||
| ``` | ||
|
|
||
| **Step4**: Set up Google Auth Platform info | ||
|
|
||
| ```bash | ||
| cat > .streamlit/secrets.toml <<EOF | ||
| [auth] | ||
| redirect_uri = "http://localhost:8501/oauth2callback" | ||
| cookie_secret = | ||
| client_id = | ||
| client_secret = | ||
| server_metadata_url = "https://accounts.google.com/.well-known/openid-configuration" | ||
| EOF | ||
| ``` | ||
|
|
||
| **Step5**: Run the Streamlit app | ||
|
|
||
| ```bash | ||
| streamlit run src/app.py | ||
| ``` | ||
|
|
||
| **Step6**: open the browser and visit `http://localhost:8501` | ||
|
|
This file contains hidden or 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 @@ | ||
| OPENAI_API_KEY = "sk-xxxxxx" | ||
|
|
||
| TIDB_HOST=xxxxxx | ||
| TIDB_PORT=xxxxxx | ||
| TIDB_USERNAME=xxxxxx | ||
| TIDB_PASSWORD=xxxxxx | ||
| TIDB_DATABASE=test |
This file contains hidden or 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,11 @@ | ||
| streamlit | ||
| openai | ||
| pytidb | ||
|
|
||
| python-dotenv | ||
| streamlit-authenticator | ||
|
|
||
| sqlalchemy | ||
| litellm | ||
| PyPDF2 | ||
| langchain_text_splitters |
This file contains hidden or 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 @@ | ||
| import streamlit as st | ||
|
|
||
| doc_page = st.Page("page_files/doc_page.py", title = "Manage Uploaded Files") | ||
| main_page = st.Page("page_files/main_page.py", title = "Chats") | ||
| login_page = st.Page("page_files/login_page.py") | ||
|
|
||
|
|
||
| def main(): | ||
| if not st.user.is_logged_in: | ||
| pg = st.navigation([login_page]) | ||
| else: | ||
| pg = st.navigation([main_page, doc_page]) | ||
| pg.run() | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
This file contains hidden or 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,73 @@ | ||
| from typing import Optional, Any | ||
| from pytidb.schema import TableModel, Field | ||
| from pytidb.embeddings import EmbeddingFunction | ||
|
|
||
|
|
||
| from datetime import datetime | ||
| from sqlalchemy import Text, Column, DateTime, text | ||
|
|
||
| # models we use | ||
| text_embed = EmbeddingFunction("openai/text-embedding-3-small") | ||
| llm_model = "gpt-4o-mini" | ||
|
|
||
| class Chunk(TableModel, table=True): | ||
| __tablename__ = "chunks" | ||
| __table_args__ = {"extend_existing": True} | ||
|
|
||
| id: int = Field(primary_key=True) | ||
| text: str = Field(sa_type=Text) | ||
| document_id: int | None = Field( | ||
| foreign_key="documents.id", | ||
| ondelete="CASCADE", | ||
| index=True | ||
| ) | ||
| text_vec: Optional[Any] = text_embed.VectorField( | ||
| source_field="text", | ||
| ) | ||
|
|
||
| class Document(TableModel, table=True): | ||
| __tablename__ = "documents" | ||
| __table_args__ = {"extend_existing": True} | ||
| id: int = Field(primary_key=True) | ||
| user_id: int| None = Field(nullable=True) | ||
| document_name: str = Field(sa_type=Text) | ||
|
|
||
| # table chat_history that stores the references to chat sessions of all users. Each row stores info of a session that chat_message represents | ||
| class Chat(TableModel, table=True): | ||
| __tablename__ = "chat_history" | ||
|
|
||
| id: int = Field(primary_key=True) | ||
| user_id: int = Field( | ||
| foreign_key="users.id", | ||
| ondelete="CASCADE", | ||
| index=True | ||
| ) | ||
| updated_at: datetime = Field( | ||
| sa_column=Column( | ||
| DateTime, | ||
| nullable=False, | ||
| server_default=text("CURRENT_TIMESTAMP"), | ||
| server_onupdate=text("CURRENT_TIMESTAMP") | ||
| ) | ||
| ) | ||
|
|
||
| # table chat_message is the sub-table of chat_history related by foreign key. Each row stores the message text, either a question asked by the user or the answer generated by the AI assistant | ||
| class ChatMessage(TableModel, table=True): | ||
| __tablename__ = "chat_message" | ||
|
|
||
| id: int = Field(primary_key=True) | ||
| chat_history_id: int = Field( | ||
| foreign_key="chat_history.id", | ||
| ondelete="CASCADE", | ||
| index=True | ||
| ) | ||
| speaker_id: int | ||
| text: str = Field(sa_type=Text) | ||
|
|
||
| # table user_chart stores info of users | ||
| class User(TableModel, table=True): | ||
| __tablename__ = "users" | ||
|
|
||
| id: int = Field(primary_key=True) | ||
| email: str = Field(unique=True, index=True) | ||
| username: str | None = Field(default=None, max_length=225) |
This file contains hidden or 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,29 @@ | ||
| import streamlit as st | ||
| import json | ||
|
|
||
| from utils import list_file_names, delete_file, create_user | ||
|
|
||
| user_id = create_user(st.user.email) | ||
|
|
||
| st.title("Manage Uploaded Files") | ||
|
|
||
| def show_file_list(user_id: int): | ||
| files_json = list_file_names(user_id) | ||
| files_dict = json.loads(files_json) | ||
| files = files_dict.get("files", []) | ||
|
|
||
| if not files: | ||
| st.info("No files uploaded yet.") | ||
| return | ||
|
|
||
| for file_name in files: | ||
| col1, col2 = st.columns([4, 1]) | ||
| with col1: | ||
| st.write(file_name) | ||
| with col2: | ||
| if st.button("Delete", key=file_name): | ||
| delete_file(user_id, file_name) | ||
| st.success(f"Deleted '{file_name}'") | ||
| st.rerun() | ||
|
|
||
| show_file_list(user_id) |
This file contains hidden or 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,8 @@ | ||
| import streamlit as st | ||
|
|
||
| def login_screen(): | ||
| st.header("This app is private.") | ||
| st.subheader("Please log in.") | ||
| st.button("Log in with Google", on_click=st.login) | ||
|
|
||
| login_screen() |
This file contains hidden or 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,10 @@ | ||
| import streamlit as st | ||
| from ui import initialize_chat_state | ||
| from utils import create_user | ||
|
|
||
| st.set_page_config(page_title="Intramind", page_icon="💬") | ||
| user_id = create_user(st.user.email) | ||
| st.session_state.user_id = user_id | ||
| st.session_state.user_name = st.user.name | ||
| st.session_state.user_email = st.user.email | ||
| initialize_chat_state(user_id) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid commit
.DS_Storeto git repo