├── .gitignore ├── README.md ├── example.env ├── groq.jpeg ├── langchain_groq_chainlit.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Chainlit 10 | .chainlit/ 11 | chainlit.md 12 | 13 | # Distribution / packaging 14 | .Python 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | wheels/ 27 | share/python-wheels/ 28 | *.egg-info/ 29 | .installed.cfg 30 | *.egg 31 | MANIFEST 32 | 33 | # PyInstaller 34 | # Usually these files are written by a python script from a template 35 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 36 | *.manifest 37 | *.spec 38 | 39 | # Installer logs 40 | pip-log.txt 41 | pip-delete-this-directory.txt 42 | 43 | # Unit test / coverage reports 44 | htmlcov/ 45 | .tox/ 46 | .nox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *.cover 53 | *.py,cover 54 | .hypothesis/ 55 | .pytest_cache/ 56 | cover/ 57 | 58 | # Translations 59 | *.mo 60 | *.pot 61 | 62 | # Django stuff: 63 | *.log 64 | local_settings.py 65 | db.sqlite3 66 | db.sqlite3-journal 67 | 68 | # Flask stuff: 69 | instance/ 70 | .webassets-cache 71 | 72 | # Scrapy stuff: 73 | .scrapy 74 | 75 | # Sphinx documentation 76 | docs/_build/ 77 | 78 | # PyBuilder 79 | .pybuilder/ 80 | target/ 81 | 82 | # Jupyter Notebook 83 | .ipynb_checkpoints 84 | 85 | # IPython 86 | profile_default/ 87 | ipython_config.py 88 | 89 | # pyenv 90 | # For a library or package, you might want to ignore these files since the code is 91 | # intended to run in multiple environments; otherwise, check them in: 92 | # .python-version 93 | 94 | # pipenv 95 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 96 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 97 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 98 | # install all needed dependencies. 99 | #Pipfile.lock 100 | 101 | # poetry 102 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 103 | # This is especially recommended for binary packages to ensure reproducibility, and is more 104 | # commonly ignored for libraries. 105 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 106 | #poetry.lock 107 | 108 | # pdm 109 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 110 | #pdm.lock 111 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 112 | # in version control. 113 | # https://pdm.fming.dev/#use-with-ide 114 | .pdm.toml 115 | 116 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 117 | __pypackages__/ 118 | 119 | # Celery stuff 120 | celerybeat-schedule 121 | celerybeat.pid 122 | 123 | # SageMath parsed files 124 | *.sage.py 125 | 126 | # Environments 127 | .env 128 | .venv 129 | env/ 130 | venv/ 131 | ENV/ 132 | env.bak/ 133 | venv.bak/ 134 | 135 | # Spyder project settings 136 | .spyderproject 137 | .spyproject 138 | 139 | # Rope project settings 140 | .ropeproject 141 | 142 | # mkdocs documentation 143 | /site 144 | 145 | # mypy 146 | .mypy_cache/ 147 | .dmypy.json 148 | dmypy.json 149 | 150 | # Pyre type checker 151 | .pyre/ 152 | 153 | # pytype static type analyzer 154 | .pytype/ 155 | 156 | # Cython debug symbols 157 | cython_debug/ 158 | 159 | # PyCharm 160 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 161 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 162 | # and can be added to the global gitignore or merged into this file. For a more nuclear 163 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 164 | #.idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # langchain-groq-chainlit 2 | Simple Chat UI using Large Language Model Groq, LangChain and Chainlit 3 | 4 | ### Tech stack being used 5 | - LLMs from [Groq](https://groq.com/) website. 6 | - [LangChain](https://www.langchain.com/) as a Framework for LLM 7 | - [LangSmith](https://smith.langchain.com/) for developing, collaborating, testing, deploying, and monitoring LLM applications. 8 | - [Chainlit](https://docs.chainlit.io/langchain) for deploying. 9 | 10 | ## System Requirements 11 | 12 | You must have Python 3.10 or later installed. Earlier versions of python may not compile. 13 | 14 | ## Steps to Replicate 15 | 16 | 1. Fork this repository (optional) and clone it locally. 17 | ``` 18 | git clone https://github.com/sudarshan-koirala/langchain-groq-chainlit.git 19 | cd langchain-groq-chainlit 20 | ``` 21 | 22 | 2. Create a virtualenv and activate it. 23 | ``` 24 | python3 -m venv .venv && source .venv/bin/activate 25 | ``` 26 | 27 | 3. OPTIONAL - Rename example.env to .env with `cp example.env .env`and input the environment variables from [LangSmith](https://smith.langchain.com/). You need to create an account in LangSmith website if you haven't already. Also, you need to get api key for groq from this [link](https://console.groq.com/keys). 28 | ``` 29 | LANGCHAIN_TRACING_V2=true 30 | LANGCHAIN_ENDPOINT="https://api.smith.langchain.com" 31 | LANGCHAIN_API_KEY="your-api-key" 32 | LANGCHAIN_PROJECT="your-project" 33 | GROQ_API_KEY="YOUR_GROQ_API_KEY" 34 | ``` 35 | 36 | 4. Run the following command in the terminal to install necessary python packages: 37 | ``` 38 | pip install -r requirements.txt 39 | ``` 40 | 41 | 5. Run the following command in your terminal to start the chat UI: 42 | ``` 43 | chainlit run langchain_groq_chainlit.py 44 | ``` 45 | 46 | ## Disclaimer 47 | This is test project and is presented in my youtube video to learn new stuffs. It is not meant to be used in production as it's not production ready. You can modify the code and use for your usecases ✌️ 48 | -------------------------------------------------------------------------------- /example.env: -------------------------------------------------------------------------------- 1 | LANGCHAIN_TRACING_V2=true 2 | LANGCHAIN_ENDPOINT="https://api.smith.langchain.com" 3 | LANGCHAIN_API_KEY="YOUR_LANGCHAIN_API_KEY" 4 | LANGCHAIN_PROJECT="groq-langchain" 5 | GROQ_API_KEY="YOUR_GROQ_API_KEY" -------------------------------------------------------------------------------- /groq.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sudarshan-koirala/langchain-groq-chainlit/b21fff2e445639aa65896c1b29bceaf3b8e8872b/groq.jpeg -------------------------------------------------------------------------------- /langchain_groq_chainlit.py: -------------------------------------------------------------------------------- 1 | from langchain_groq import ChatGroq 2 | from langchain_core.prompts import ChatPromptTemplate 3 | from langchain.schema import StrOutputParser 4 | from langchain.schema.runnable import Runnable 5 | from langchain.schema.runnable.config import RunnableConfig 6 | 7 | import chainlit as cl 8 | 9 | 10 | @cl.on_chat_start 11 | async def on_chat_start(): 12 | 13 | # Sending an image with the local file path 14 | elements = [ 15 | cl.Image(name="image1", display="inline", path="groq.jpeg") 16 | ] 17 | await cl.Message(content="Hello there, I am Groq. How can I help you ?", elements=elements).send() 18 | 19 | model = ChatGroq(temperature=0,model_name="mixtral-8x7b-32768") 20 | prompt = ChatPromptTemplate.from_messages( 21 | [ 22 | ( 23 | "system", 24 | "You're a very knowledgeable Machine Learning Engineer.", 25 | ), 26 | ("human", "{question}"), 27 | ] 28 | ) 29 | runnable = prompt | model | StrOutputParser() 30 | cl.user_session.set("runnable", runnable) 31 | 32 | 33 | @cl.on_message 34 | async def on_message(message: cl.Message): 35 | runnable = cl.user_session.get("runnable") # type: Runnable 36 | 37 | msg = cl.Message(content="") 38 | 39 | async for chunk in runnable.astream( 40 | {"question": message.content}, 41 | config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]), 42 | ): 43 | await msg.stream_token(chunk) 44 | 45 | await msg.send() 46 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | langchain-groq 2 | langchain 3 | chainlit 4 | --------------------------------------------------------------------------------