{"id":25213985,"url":"https://github.com/shadowwphoenix/chatbot-using-llama3","last_synced_at":"2026-04-30T01:31:21.621Z","repository":{"id":276809874,"uuid":"930356678","full_name":"ShadowwPhoenix/Chatbot-using-Llama3","owner":"ShadowwPhoenix","description":"A chatbot using LLaMA 3, built with FastAPI (backend) and Streamlit (frontend), allowing real-time interaction while maintaining conversation history and logging chats. The system integrates LangChain for prompt management and memory, ensuring contextual responses. 🚀","archived":false,"fork":false,"pushed_at":"2025-02-10T15:21:40.000Z","size":85,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T15:36:18.249Z","etag":null,"topics":["chatbot","llms","python","streamlit"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShadowwPhoenix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-10T14:03:02.000Z","updated_at":"2025-02-10T15:35:27.000Z","dependencies_parsed_at":"2025-02-10T15:48:11.246Z","dependency_job_id":null,"html_url":"https://github.com/ShadowwPhoenix/Chatbot-using-Llama3","commit_stats":null,"previous_names":["shadowwphoenix/chatbot-using-llama3"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShadowwPhoenix%2FChatbot-using-Llama3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShadowwPhoenix%2FChatbot-using-Llama3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShadowwPhoenix%2FChatbot-using-Llama3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShadowwPhoenix%2FChatbot-using-Llama3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShadowwPhoenix","download_url":"https://codeload.github.com/ShadowwPhoenix/Chatbot-using-Llama3/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247307857,"owners_count":20917569,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","llms","python","streamlit"],"created_at":"2025-02-10T16:58:07.774Z","updated_at":"2026-04-30T01:31:21.576Z","avatar_url":"https://github.com/ShadowwPhoenix.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🤖 LLaMA 3 Chatbot using FastAPI \u0026 Streamlit\n\nThis project uses Streamlit, FastAPI, and the LLaMA 3 model to create an interactive chatbot. The application allows users to chat with the LLaMA 3 model, view conversation history, and log interactions for analytics.\n\n## Project Overview\n\nThis project leverages the LLaMA 3 model for generating conversational responses. The application provides a user-friendly interface where users can input messages, and the chatbot responds in real-time. The conversation history is stored and displayed, and all interactions are logged for future reference.\n\n### Features:\n- **Interactive Chat Interface**: Users can type messages and receive responses from the LLaMA 3 model.\n- **Conversation History**: Keeps track of previous messages exchanged between the user and the chatbot.\n- **Logging**: All interactions are logged to a file for analytics and debugging purposes.\n- **FastAPI Backend**: Handles the communication between the Streamlit frontend and the LLaMA 3 model.\n\n![Chatbot UI](Screenshot.png)  \n\n###  Implement a Simple Context System  \n✅ Allow the chatbot to remember past interactions within the same conversation.  \n✅ Example: If the user asks, *\"Tell me about Shopify,\"* and then follows up with *\"How does it handle payments?\"* → The bot will  refer ‘it’  to Shopify.  \n\nThis project creates an **interactive chatbot** using **LLaMA 3**, **FastAPI**, and **Streamlit**.\n\n---\n\n## **1. FastAPI Backend**\nThe backend is built using **FastAPI** to serve the chatbot API.\n\n### **Imports and Setup**\n```python\nimport threading\nimport uvicorn\nimport requests\nfrom fastapi import FastAPI, HTTPException\nfrom pydantic import BaseModel\nfrom langchain_ollama import OllamaLLM\nfrom langchain.prompts import PromptTemplate\nfrom langchain.memory import ConversationBufferMemory\nfrom langchain.chains import LLMChain\nimport logging\n```\n\n- FastAPI for the API.\n- LangChain for handling LLaMA 3 interactions.\n- Logging for storing chat history\n\n### **FastAPI App Initialization**\n```python\napp = FastAPI()\n```\n- Creates a FastAPI instance.\n\n### **Logging Setup**\n```python\nlogging.basicConfig(filename=\"chat_log.txt\", level=logging.INFO, format=\"%(asctime)s - %(message)s\")\n```\n- Saves user and AI responses to chat_log.txt\n\n### **LLaMA 3 Model Initialization**\n```python\nllm = OllamaLLM(model=\"llama3\")\n\n```\n- Loads the LLaMA 3 model using LangChain.\n### **Chat Prompt and Memory Setup**\n\n```python\nprompt_template = PromptTemplate(\n    input_variables=[\"chat_history\", \"user_input\"],\n    template=\"You are a helpful AI assistant. Here is the conversation so far:\\n{chat_history}\\nUser: {user_input}\\nAI:\"\n)\nmemory = ConversationBufferMemory(memory_key=\"chat_history\", return_messages=True)\nchain = LLMChain(prompt=prompt_template, llm=llm, memory=memory)\n\n```\n- Maintains conversation history for contextual responses.\n### **Chat API Endpoint**\n\n```python\nclass ChatRequest(BaseModel):\n    user_input: str\n\n@app.post(\"/chat\")\ndef chat(request: ChatRequest):\n    try:\n        response = chain.run({\"user_input\": request.user_input})\n        logging.info(f\"User: {request.user_input}\")\n        logging.info(f\"AI: {response}\")\n        return {\"response\": response}\n    except Exception as e:\n        logging.error(f\"Error: {str(e)}\")\n        raise HTTPException(status_code=500, detail=str(e))\n\n```\n\n- Receives user input via POST request.\n- Processes it using LLaMA 3.\n- Logs the chat history.\n\n\n### **Running FastAPI in a Background Thread**\n```python\ndef run_fastapi():\n    uvicorn.run(app, host=\"127.0.0.1\", port=8000)\n\nthreading.Thread(target=run_fastapi, daemon=True).start()\n```\n\n- Runs FastAPI in a separate thread so that it doesn't block Streamlit.\n\n\n## **2. Streamlit Frontend**\n\nThe frontend is built using **Streamlit** for an interactive chat UI.\n\n### **Streamlit Page Setup**\n\n```python\nAPI_URL = \"http://127.0.0.1:8000/chat\"\n\nst.set_page_config(page_title=\"LLaMA 3 Chatbot\", page_icon=\"🤖\")\n\nst.title(\"🤖 AI Chatbot\")\nst.write(\"Chat with LLaMA 3 using FastAPI!\")\n\n```\n\n- Sets up the chat UI.\n\n\n\n\n\n\n\n\n\n\n\n\n\n### **Session State for Chat History**\n\n```python\nif \"messages\" not in st.session_state:\n    st.session_state.messages = []\n\n```\n\n- Ensures chat history persists.\n\n### **Displaying Chat Messages**\n\n```python\nfor msg in st.session_state.messages:\n    with st.chat_message(msg[\"role\"]):\n        st.markdown(msg[\"content\"])\n\n```\n\n- Displays previous chat messages.\n\n### **Handling User Input**\n\n```python\nuser_input = st.chat_input(\"Type your message...\")\n\n```\n- Provides an input box for user messages.\n\n### **Sending Input to API and Displaying Response**\n\n```python\nif user_input:\n    st.session_state.messages.append({\"role\": \"user\", \"content\": user_input})\n    response = requests.post(API_URL, json={\"user_input\": user_input})\n    \n    if response.status_code == 200:\n        bot_response = response.json()[\"response\"]\n    else:\n        bot_response = \"⚠️ Error: Unable to get response from the server.\"\n\n    st.session_state.messages.append({\"role\": \"assistant\", \"content\": bot_response})\n    \n    with st.chat_message(\"assistant\"):\n        st.markdown(bot_response)\n\n```\n- Sends user input to FastAPI.\n- Sends user input to FastAPI.\n- Handles errors if FastAPI is unreachable.\n- Displays the AI’s response.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## Requirements\n\nTo run the project, you'll need to have the following dependencies:\n\n- Python 3.x\n- Streamlit\n- FastAPI\n- Uvicorn\n- Requests\n- Langchain\n- Ollama\n\n## Install Dependencies\n\nYou can install the necessary dependencies using pip:\n\n```bash\npip install streamlit fastapi uvicorn requests langchain ollama\n```\nAdditionally, you must have Ollama installed and the LLaMA 3 model pulled. You can do this by running:\n\n```bash\nollama pull llama3\n```\n\n## How to Run\n\n### Set Up the Model:\n1. Ensure Ollama is installed and the LLaMA 3 model is pulled.\n\n\n### Run the Streamlit App:\n1. Open a terminal or command prompt.\n2. Navigate to the directory containing the Streamlit script.\n3. Run the following command to start the Streamlit app:\n\n   ```bash\n   streamlit run chatbot.py\n   ```\n   ### Access the Application:\nAfter running the commands, the Streamlit app will open in your web browser. You can start chatting with the LLaMA 3 model.\n\n\n---\n\nFeel free to reach out with questions or suggestions.  \n*by Shadow Phoenix*\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshadowwphoenix%2Fchatbot-using-llama3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshadowwphoenix%2Fchatbot-using-llama3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshadowwphoenix%2Fchatbot-using-llama3/lists"}