├── .gitattributes ├── .gitignore ├── CITATION.cff ├── LICENSE ├── README.md ├── app.py ├── endpoints ├── chat.py └── ingest.py ├── example.env ├── handlers └── base.py ├── poetry.lock ├── pyproject.toml ├── startup.py ├── ui └── main.py └── utils └── alerts.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 105 | __pypackages__/ 106 | 107 | # Celery stuff 108 | celerybeat-schedule 109 | celerybeat.pid 110 | 111 | # SageMath parsed files 112 | *.sage.py 113 | 114 | # Environments 115 | .env 116 | .venv 117 | venv2 118 | env/ 119 | venv/ 120 | ENV/ 121 | env.bak/ 122 | venv.bak/ 123 | vectorstore 124 | 125 | # Spyder project settings 126 | .spyderproject 127 | .spyproject 128 | 129 | # Rope project settings 130 | .ropeproject 131 | 132 | # mkdocs documentation 133 | /site 134 | 135 | # mypy 136 | .mypy_cache/ 137 | .dmypy.json 138 | dmypy.json 139 | 140 | # Pyre type checker 141 | .pyre/ 142 | 143 | # pytype static type analyzer 144 | .pytype/ 145 | 146 | # Cython debug symbols 147 | cython_debug/ 148 | 149 | # PyCharm 150 | # JetBrains specific template is maintainted in a separate JetBrains.gitignore that can 151 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 152 | # and can be added to the global gitignore or merged into this file. For a more nuclear 153 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 154 | #.idea/ 155 | 156 | .vscode/ 157 | 158 | -------------------------------------------------------------------------------- /CITATION.cff: -------------------------------------------------------------------------------- 1 | cff-version: 1.2.0 2 | message: "If you use this software, please cite it as below." 3 | authors: 4 | - family-names: "Chase" 5 | given-names: "Harrison" 6 | title: "LangChain" 7 | date-released: 2022-10-17 8 | url: "https://github.com/hwchase17/langchain" 9 | 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Haste171 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Efficiently use Langchain for Complex Tasks 3 |

4 | 5 | 6 | # 🚀 Installation 7 | 8 | ## Dev-Setup 9 | Prerequisites: 10 | - [Git](https://git-scm.com/downloads) - Free 11 | - [Pinecone Database](https://youtu.be/tp0bQNDtLPc?t=48) - Free 12 | - [OpenAI API Key](https://platform.openai.com/account/api-keys) - Billing Required 13 | 14 | ### Setup 15 | ``` 16 | git clone https://github.com/langschain/langchain-chatbot.git 17 | ``` 18 | 19 | Reference [example.env](https://github.com/langschain/langchain-chatbot/blob/main/example.env) to create `.env` file 20 | ```python 21 | OPENAI_API_KEY= 22 | PINECONE_API_KEY= 23 | PINECONE_ENV= 24 | PINECONE_INDEX= 25 | ``` 26 | 27 | ### Install Requirements 28 | 29 | ```python 30 | poetry install 31 | ``` 32 | 33 | ### Activate Environment 34 | ```python 35 | poetry shell 36 | ``` 37 | 38 | ### Run Startup 39 | ```python 40 | python3 startup.py 41 | ``` 42 | 43 | 44 | # 🔧 Key Features 45 | 46 | ✅ Interactive Ingestion UI for files 47 | 48 | ✅ Chat UI with source, temperature, vector_k, and other parameter changing abilities 49 | 50 | ✅ More features coming very soon 51 | 52 | 53 | # 💻 Contributing 54 | 55 | If you would like to contribute to the LangChain Chatbot, please follow these steps: 56 | 57 | 1. Fork the repository 58 | 2. Create a new branch for your feature or bug fix 59 | 3. Write tests for your changes 60 | 4. Implement your changes and ensure that all tests pass 61 | 5. Submit a pull request 62 | 63 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | import uvicorn 3 | 4 | app = FastAPI() 5 | 6 | @app.get("/") 7 | def read_root(): 8 | return {"detail": "Langchain Chatbot is Running!"} 9 | 10 | from endpoints import ( 11 | ingest, 12 | chat, 13 | ) 14 | 15 | for endpoint in [ingest, chat]: 16 | app.include_router(endpoint.router) 17 | 18 | if __name__ == "__main__": 19 | uvicorn.run('app:app', port=9091, reload=True) -------------------------------------------------------------------------------- /endpoints/chat.py: -------------------------------------------------------------------------------- 1 | from fastapi import APIRouter, HTTPException 2 | from pydantic import BaseModel 3 | from handlers.base import BaseHandler 4 | from typing import Optional 5 | 6 | router = APIRouter() 7 | 8 | class ChatModel(BaseModel): 9 | query: str 10 | model: str = "gpt-3.5-turbo" 11 | temperature: float 12 | vector_fetch_k: Optional[int] = 5 # Number of vectors to fetch from Pinecone as source documents 13 | chat_history: list[str] = [] # Example input: [("You are a helpful assistant.", "What is your name?")] 14 | namespace: Optional[str] = None 15 | 16 | @router.post("/chat") 17 | async def chat( 18 | chat_model: ChatModel, 19 | ): 20 | available_models = ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "gpt-4-32k", "gpt-4-1106-preview", "claude-3-sonnet-20240229", "claude-3-opus-20240229"] 21 | 22 | if chat_model.model not in available_models: 23 | raise HTTPException(status_code=400, detail=f"Invalid model name. Please select a valid model from the list of available models: \n{str(available_models)}") 24 | 25 | if chat_model.temperature < 0.0 or chat_model.temperature > 2.0: 26 | raise HTTPException(status_code=400, detail="Invalid temperature value. Please select a value between 0.0 and 2.0") 27 | 28 | handler = BaseHandler(chat_model=chat_model.model, openai_chat_temperature=chat_model.temperature) 29 | response = handler.chat( 30 | chat_model.query, 31 | chat_model.chat_history, 32 | namespace=(chat_model.namespace or None), 33 | search_kwargs=({"k": chat_model.vector_fetch_k} or {"k": 5}) 34 | ) 35 | return {"response": response} 36 | -------------------------------------------------------------------------------- /endpoints/ingest.py: -------------------------------------------------------------------------------- 1 | from fastapi import APIRouter 2 | from typing import List 3 | from fastapi import UploadFile, Form 4 | from handlers.base import BaseHandler 5 | from typing import Optional 6 | 7 | router = APIRouter() 8 | 9 | @router.post("/ingest") 10 | async def ingest_documents( 11 | files: List[UploadFile], 12 | namespace: Optional[str] = Form(None), 13 | ): 14 | handler = BaseHandler( 15 | #embeddings_model='text-embedding-3-large' # Uncomment this kwarg to use the large embeddings model if you have Pinecone configured to that dimension size 16 | ) 17 | documents = handler.load_documents(files, namespace) 18 | handler.ingest_documents(documents) 19 | return {"message": "Documents ingested"} -------------------------------------------------------------------------------- /example.env: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY= 2 | PINECONE_API_KEY= 3 | PINECONE_ENV= 4 | PINECONE_INDEX= 5 | -------------------------------------------------------------------------------- /handlers/base.py: -------------------------------------------------------------------------------- 1 | import tempfile 2 | import pinecone 3 | import os 4 | from utils.alerts import alert_exception, alert_info 5 | from typing import List 6 | from pinecone.core.client.exceptions import ApiException 7 | from langchain.chains import ConversationalRetrievalChain 8 | from langchain_openai.embeddings import OpenAIEmbeddings 9 | from langchain_openai.chat_models import ChatOpenAI 10 | from langchain_anthropic import ChatAnthropic 11 | from langchain_community.vectorstores.pinecone import Pinecone 12 | from langchain_community.document_loaders import TextLoader, PyMuPDFLoader, Docx2txtLoader 13 | from fastapi import UploadFile 14 | from fastapi import HTTPException 15 | from dotenv import load_dotenv 16 | # from langchain.chains.question_answering import load_qa_chain 17 | # from langchain.chains.llm import LLMChain 18 | # from langchain.chains.conversational_retrieval.prompts import ( 19 | # from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler 20 | # ) 21 | # QA_PROMPT, 22 | # CONDENSE_QUESTION_PROMPT, 23 | from langchain.text_splitter import ( 24 | TokenTextSplitter, 25 | TextSplitter, 26 | Tokenizer, 27 | Language, 28 | RecursiveCharacterTextSplitter, 29 | RecursiveJsonSplitter, 30 | LatexTextSplitter, 31 | PythonCodeTextSplitter, 32 | KonlpyTextSplitter, 33 | SpacyTextSplitter, 34 | NLTKTextSplitter, 35 | SentenceTransformersTokenTextSplitter, 36 | ElementType, 37 | HeaderType, 38 | LineType, 39 | HTMLHeaderTextSplitter, 40 | MarkdownHeaderTextSplitter, 41 | MarkdownTextSplitter, 42 | CharacterTextSplitter, 43 | ) 44 | 45 | load_dotenv() 46 | 47 | class BaseHandler(): 48 | def __init__( 49 | self, 50 | chat_model: str = 'gpt-3.5-turbo', 51 | temperature: float = 0.7, 52 | **kwargs 53 | ): 54 | 55 | self.pinecone_api_key = os.getenv('PINECONE_API_KEY') 56 | self.pinecone_env = os.getenv('PINECONE_ENV') 57 | self.pinecone_index = os.getenv('PINECONE_INDEX') 58 | self.llm_map = { 59 | 'gpt-4': lambda: ChatOpenAI(model='gpt-4', temperature=temperature, openai_api_key=os.getenv('OPENAI_API_KEY')), 60 | 'gpt-4-32k': lambda: ChatOpenAI(model='gpt-4-32k', temperature=temperature, openai_api_key=os.getenv('OPENAI_API_KEY')), 61 | 'gpt-4-1106-preview': lambda: ChatOpenAI(model='gpt-4', temperature=temperature, openai_api_key=os.getenv('OPENAI_API_KEY')), 62 | 'gpt-3.5-turbo-16k': lambda: ChatOpenAI(model='gpt-3.5-turbo-16k', temperature=temperature, openai_api_key=os.getenv('OPENAI_API_KEY')), 63 | 'gpt-3.5-turbo': lambda: ChatOpenAI(model='gpt-3.5-turbo', temperature=temperature, openai_api_key=os.getenv('OPENAI_API_KEY')), 64 | 'claude-3-sonnet-20240229': lambda: ChatAnthropic(model_name='claude-3-sonnet-20240229', temperature=temperature, anthropic_api_key=os.getenv('ANTHROPIC_API_KEY')), 65 | 'claude-3-opus-20240229': lambda: ChatAnthropic(model_name='claude-3-opus-20240229', temperature=temperature, anthropic_api_key=os.getenv('ANTHROPIC_API_KEY')), 66 | } 67 | self.chat_model = chat_model 68 | # self.streaming_llm = ChatOpenAI( 69 | # model=openai_chat_model, 70 | # streaming=True, 71 | # callbacks=[StreamingStdOutCallbackHandler()], 72 | # temperature=0, 73 | # openai_api_key=os.getenv('OPENAI_API_KEY'), 74 | # ) 75 | 76 | if kwargs.get('embeddings_model') == 'text-embedding-3-large': 77 | self.embeddings = OpenAIEmbeddings( 78 | openai_api_key=os.getenv('OPENAI_API_KEY'), 79 | model='text-embedding-3-large' 80 | ) 81 | self.dimensions = 3072 82 | else: 83 | self.embeddings = OpenAIEmbeddings( 84 | openai_api_key=os.getenv('OPENAI_API_KEY'), 85 | model='text-embedding-3-small' 86 | ) 87 | self.dimensions = 1536 88 | 89 | def load_documents(self, files: list[UploadFile], namespace: str = None) -> list[list[str]]: 90 | documents = [] 91 | 92 | loader_map = { 93 | 'txt': TextLoader, 94 | 'pdf': PyMuPDFLoader, 95 | 'docx': Docx2txtLoader, 96 | } 97 | 98 | allowed_extensions = [key for key in loader_map.keys()] 99 | try: 100 | for file in files: 101 | if file.filename.split(".")[-1] not in allowed_extensions: 102 | raise HTTPException(status_code=400, detail="File type not permitted") 103 | 104 | with tempfile.NamedTemporaryFile(delete=True, prefix=file.filename + '___') as temp: 105 | temp.write(file.file.read()) 106 | temp.seek(0) 107 | loader = loader_map[file.filename.split(".")[-1]](temp.name) 108 | documents.append(loader.load()) 109 | except Exception as e: 110 | alert_exception(e, "Error loading documents") 111 | raise HTTPException(status_code=500, detail=f"Error loading documents: {str(e)}") 112 | 113 | 114 | return documents 115 | 116 | def ingest_documents(self, documents: list[list[str]], chunk_size: int = 1000, chunk_overlap: int = 100, **kwargs): 117 | """ 118 | documents: list of loaded documents 119 | chunk_size: number of documents to ingest at a time 120 | chunk_overlap: number of documents to overlap when ingesting 121 | 122 | kwargs: 123 | split_method: 'recursive', 'token', 'text', 'tokenizer', 'language', 'json', 'latex', 'python', 'konlpy', 'spacy', 'nltk', 'sentence_transformers', 'element_type', 'header_type', 'line_type', 'html_header', 'markdown_header', 'markdown', 'character' 124 | """ 125 | pinecone.init(api_key=self.pinecone_api_key, environment=self.pinecone_env) 126 | 127 | splitter_map = { 128 | 'recursive': RecursiveCharacterTextSplitter, 129 | 'token': TokenTextSplitter, 130 | 'text': TextSplitter, 131 | 'tokenizer': Tokenizer, 132 | 'language': Language, 133 | 'json': RecursiveJsonSplitter, 134 | 'latex': LatexTextSplitter, 135 | 'python': PythonCodeTextSplitter, 136 | 'konlpy': KonlpyTextSplitter, 137 | 'spacy': SpacyTextSplitter, 138 | 'nltk': NLTKTextSplitter, 139 | 'sentence_transformers': SentenceTransformersTokenTextSplitter, 140 | 'element_type': ElementType, 141 | 'header_type': HeaderType, 142 | 'line_type': LineType, 143 | 'html_header': HTMLHeaderTextSplitter, 144 | 'markdown_header': MarkdownHeaderTextSplitter, 145 | 'markdown': MarkdownTextSplitter, 146 | 'character': CharacterTextSplitter 147 | } 148 | 149 | split_method = kwargs.get('split_method', 'recursive') 150 | test_splitter = splitter_map[split_method](chunk_size=chunk_size, chunk_overlap=chunk_overlap) 151 | 152 | alert_info(f"Ingesting {len(documents)} document(s)...\nParams: chunk_size={chunk_size}, chunk_overlap={chunk_overlap}, split_method={split_method}") 153 | for document in documents: 154 | split_document = test_splitter.split_documents(document) 155 | try: 156 | Pinecone.from_documents( 157 | split_document, 158 | self.embeddings, 159 | index_name=self.pinecone_index, 160 | namespace=kwargs.get('namespace', None) # You can only specify a namespace if you have a premium Pinecone pod 161 | ) 162 | except ApiException as e: 163 | alert_exception(e, "Error ingesting documents - Make sure you\'re dimensions match the embeddings model (1536 for text-embedding-3-small, 3072 for text-embedding-3-large)") 164 | raise HTTPException(status_code=500, detail=f"Error ingesting documents: {str(e)}") 165 | 166 | def chat(self, query: str, chat_history: list[str] = [], **kwargs): 167 | """ 168 | query: str 169 | chat_history: list of previous chat messages 170 | kwargs: 171 | namespace: str 172 | search_kwargs: dict 173 | """ 174 | # alert_info(f"Querying with: {query} and chat history: {chat_history}\nParams: namespace={kwargs.get('namespace', None)}, search_kwargs={kwargs.get('search_kwargs', {'k': 5})}\nModel: {self.llm.model_name} with temperature: {self.llm.temperature}") 175 | try: 176 | pinecone.init(api_key=self.pinecone_api_key, environment=self.pinecone_env) 177 | 178 | vectorstore = Pinecone.from_existing_index( 179 | index_name=self.pinecone_index, 180 | embedding=self.embeddings, 181 | text_key='text', 182 | namespace=kwargs.get('namespace', None) # You can only specify a namespace if you have a premium Pinecone pod 183 | ) 184 | 185 | retriever = vectorstore.as_retriever(search_kwargs=kwargs.get('search_kwargs', {"k": 5})) 186 | 187 | bot = ConversationalRetrievalChain.from_llm( 188 | self.llm_map[self.chat_model], 189 | retriever, 190 | return_source_documents=True 191 | ) 192 | 193 | # question_generator = LLMChain(llm=self.llm, prompt=CONDENSE_QUESTION_PROMPT) 194 | # doc_chain = load_qa_chain(self.streaming_llm, chain_type="stuff", prompt=QA_PROMPT) 195 | 196 | # bot = ConversationalRetrievalChain( 197 | # retriever=retriever, 198 | # combine_docs_chain=doc_chain, 199 | # question_generator=question_generator, 200 | # return_source_documents=True, 201 | # ) 202 | 203 | result = bot.invoke({"question": query, "chat_history": chat_history}) 204 | return result 205 | except ApiException as e: 206 | alert_exception(e, "Pinecone API Error") 207 | raise HTTPException(status_code=500, detail=f"Error chatting: {str(e)}") 208 | except Exception as e: 209 | alert_exception(e, "Error chatting") 210 | raise HTTPException(status_code=500, detail=f"Error chatting: {str(e)}") 211 | # for chunk in result: 212 | # yield chunk -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "langchain-chatbot" 3 | version = "0.1.0" 4 | description = "" 5 | authors = ["Your Name <34923485+Haste171@users.noreply.github.com>"] 6 | license = "MIT" 7 | readme = "README.md" 8 | 9 | [tool.poetry.dependencies] 10 | python = "^3.10" 11 | langchain = "^0.1.11" 12 | langchain-openai = "^0.0.8" 13 | python-dotenv = "^1.0.1" 14 | pinecone-client = "2.2.1" 15 | colorama = "^0.4.6" 16 | streamlit = "^1.32.0" 17 | watchdog = "^4.0.0" 18 | python-multipart = "^0.0.9" 19 | pymupdf = "^1.23.26" 20 | fastapi = "^0.110.0" 21 | uvicorn = "^0.28.0" 22 | langchain-anthropic = "^0.1.4" 23 | docx2txt = "^0.8" 24 | 25 | 26 | [build-system] 27 | requires = ["poetry-core"] 28 | build-backend = "poetry.core.masonry.api" 29 | -------------------------------------------------------------------------------- /startup.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import threading 3 | 4 | print('Make sure to set .env variables.') 5 | input('Also make sure you are in the Poetry shell before running this... (enter to start)') 6 | 7 | def run_app(): 8 | subprocess.run(['python3', 'app.py']) 9 | 10 | def run_streamlit(): 11 | subprocess.run(['streamlit', 'run', 'ui/main.py']) 12 | 13 | def run(): 14 | # Run each process in a separate thread 15 | t1 = threading.Thread(target=run_app) 16 | t2 = threading.Thread(target=run_streamlit) 17 | t1.start() 18 | t2.start() 19 | 20 | run() 21 | -------------------------------------------------------------------------------- /ui/main.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import requests 3 | import json 4 | 5 | def post_data(api_endpoint, data): 6 | try: 7 | response = requests.post(api_endpoint, data=json.dumps(data)) 8 | if response.status_code == 200: 9 | return response.json() 10 | else: 11 | return None 12 | except Exception as e: 13 | st.error(f"Error occurred: {e}") 14 | return None 15 | 16 | def chat_page(): 17 | st.title("Langchain Chatbot Interface") 18 | api_endpoint = "http://localhost:9091/chat" 19 | 20 | param1 = st.text_input("Query") 21 | namespace = st.text_input("Namespace (Optional)", value=None) 22 | # make default model_selector to gpt-3.5-turbo 23 | model_selector = st.selectbox("Chat Model", 24 | ["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "gpt-4-32k", "gpt-4-1106-preview", "claude-3-sonnet-20240229", "claude-3-opus-20240229"] 25 | ) 26 | temperature = st.slider("Temperature", 0.0, 2.0, 0.7) 27 | source_documents = st.slider("Number of Source Documents", 1, 10, 5) 28 | 29 | 30 | if api_endpoint: 31 | if st.button("Send"): 32 | data = { 33 | "query": str(param1), 34 | "chat_history": [], 35 | "model": model_selector, 36 | "temperature": temperature, 37 | "vector_fetch_k": source_documents, 38 | "namespace": namespace if namespace else None 39 | } 40 | 41 | data = post_data(api_endpoint, data) 42 | 43 | if data: 44 | st.markdown(data['response']['answer']) 45 | st.header("Source Documents") 46 | doc_num_init = 1 47 | for msg in data['response']['source_documents']: 48 | st.markdown(f"""### Document **[{doc_num_init}]**: \n__{msg.get('metadata').get('source')}__\n\n*Page Content:*\n\n```{msg.get('page_content')}```""") 49 | doc_num_init += 1 50 | else: 51 | st.warning("Failed to fetch data from the API endpoint. Please check the endpoint URL.") 52 | 53 | def ingest_page(): 54 | st.title("Document Ingestion") 55 | FASTAPI_URL = "http://localhost:9091/ingest" 56 | 57 | uploaded_files = st.file_uploader("Upload Document(s)", accept_multiple_files=True) 58 | namespace = st.text_input("Namespace (Optional)") 59 | 60 | if st.button("Ingest Documents"): 61 | if uploaded_files: 62 | try: 63 | files = [("files", file) for file in uploaded_files] 64 | payload = {"namespace": namespace} 65 | response = requests.post(FASTAPI_URL, files=files, data=payload) 66 | 67 | if response.status_code == 200: 68 | st.success("Documents ingested successfully!") 69 | else: 70 | st.error(f"Failed to ingest documents. Error: {response.text}") 71 | except Exception as e: 72 | st.error(f"An error occurred: {str(e)}") 73 | else: 74 | st.warning("Please upload at least one document.") 75 | 76 | def main(): 77 | st.sidebar.title("Navigation") 78 | tabs = ["Ingestion", "Chat"] 79 | selected_tab = st.sidebar.radio("Go to", tabs) 80 | 81 | if selected_tab == "Chat": 82 | chat_page() 83 | elif selected_tab == "Ingestion": 84 | ingest_page() 85 | 86 | if __name__ == "__main__": 87 | main() 88 | -------------------------------------------------------------------------------- /utils/alerts.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from colorama import Fore, Style, init 3 | 4 | # logging config to print in color 5 | logging.basicConfig(level=logging.INFO) 6 | logging.addLevelName(logging.INFO, f"{Fore.GREEN}{Style.BRIGHT}{logging.getLevelName(logging.INFO)}{Style.RESET_ALL}") 7 | 8 | init(autoreset=True) 9 | 10 | def alert_exception(e: Exception, message: str = None): 11 | """ 12 | Print an error message to the console and log the exception 13 | """ 14 | if message: 15 | print(f"{Fore.RED}{message}{Style.RESET_ALL}") 16 | print(f"{Fore.RED}{e}{Style.RESET_ALL}") 17 | logging.exception(e) 18 | 19 | def alert_info(message: str): 20 | """ 21 | Print an info message to the console and log the message 22 | """ 23 | print(f"{Fore.MAGENTA}{message}{Style.RESET_ALL}") 24 | logging.info(message) --------------------------------------------------------------------------------