From f41fea3a5ff005d67a94ee8ea40a11cf674dfc7e Mon Sep 17 00:00:00 2001 From: Kyleigh Date: Thu, 23 Oct 2025 18:32:47 -0500 Subject: [PATCH 1/2] Create files needed --- .github/ISSUE_TEMPLATE/groq_chatbot/.env | 0 .github/ISSUE_TEMPLATE/groq_chatbot/Readme.md | 0 .github/ISSUE_TEMPLATE/groq_chatbot/main.py | 0 .github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt | 0 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/.env create mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/Readme.md create mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/main.py create mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/.env b/.github/ISSUE_TEMPLATE/groq_chatbot/.env new file mode 100644 index 0000000..e69de29 diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/Readme.md b/.github/ISSUE_TEMPLATE/groq_chatbot/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/main.py b/.github/ISSUE_TEMPLATE/groq_chatbot/main.py new file mode 100644 index 0000000..e69de29 diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt b/.github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt new file mode 100644 index 0000000..e69de29 From 537c1bc06c10feb72f260f28a3a1689b12d6a4b3 Mon Sep 17 00:00:00 2001 From: Kyleigh Date: Thu, 23 Oct 2025 18:48:49 -0500 Subject: [PATCH 2/2] Add Groq chatbot features into main.py and create readme instructions --- .github/ISSUE_TEMPLATE/groq_chatbot/.env | 0 .github/ISSUE_TEMPLATE/groq_chatbot/Readme.md | 0 .github/ISSUE_TEMPLATE/groq_chatbot/main.py | 0 .../groq_chatbot/requirements.txt | 0 groq_chatbot/.env | 1 + groq_chatbot/Readme.md | 53 +++++++++++++++++++ groq_chatbot/main.py | 49 +++++++++++++++++ groq_chatbot/requirements.txt | 6 +++ 8 files changed, 109 insertions(+) delete mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/.env delete mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/Readme.md delete mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/main.py delete mode 100644 .github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt create mode 100644 groq_chatbot/.env create mode 100644 groq_chatbot/Readme.md create mode 100644 groq_chatbot/main.py create mode 100644 groq_chatbot/requirements.txt diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/.env b/.github/ISSUE_TEMPLATE/groq_chatbot/.env deleted file mode 100644 index e69de29..0000000 diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/Readme.md b/.github/ISSUE_TEMPLATE/groq_chatbot/Readme.md deleted file mode 100644 index e69de29..0000000 diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/main.py b/.github/ISSUE_TEMPLATE/groq_chatbot/main.py deleted file mode 100644 index e69de29..0000000 diff --git a/.github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt b/.github/ISSUE_TEMPLATE/groq_chatbot/requirements.txt deleted file mode 100644 index e69de29..0000000 diff --git a/groq_chatbot/.env b/groq_chatbot/.env new file mode 100644 index 0000000..a2ef835 --- /dev/null +++ b/groq_chatbot/.env @@ -0,0 +1 @@ +GROQ_API_KEY="your_groq_api_key_here" \ No newline at end of file diff --git a/groq_chatbot/Readme.md b/groq_chatbot/Readme.md new file mode 100644 index 0000000..336df48 --- /dev/null +++ b/groq_chatbot/Readme.md @@ -0,0 +1,53 @@ +# AI Web Assistant (Groq + DuckDuckGo) + +A small Python AI chatbot that uses **Groq** for natural language processing and **DuckDuckGo** for web search. +The assistant can answer user questions and perform simple calculations. + +--- +## Features + +- Chatbot powered by Groq LLM +- Search the web via DuckDuckGo +- Perform basic math calculations +- Easy to run locally with Python + +--- + +## Setup Instructions + +### 1. Get a Groq API Key + +1. Go to [Groq Developer Portal](https://www.groq.com/) and sign up for a free account. +2. Generate a free **Groq API key** from your account dashboard. +3. Copy the API key and paste it inside the quotes in the .env file. + +--- + +### 2. Set up a Python virtual environment (recommended) +1. Open your terminal and navigate to the project folder. +2. Run the following to create a virtual environment: + ```bash + python -m venv .venv (or py -m venv .venv) + +3. Activate the virtual environment by running: + ```bash + .venv\Scripts\activate (on Windows) + or + source .venv/bin/activate (on Linux and MacOS) + +### 3. Install dependancies +1. Once your virtual environment is active, install the required packages: + ```bash + pip install -r requirements.txt + +### 4. Run the chatbot + ```bash + python main.py + + + + + + + + diff --git a/groq_chatbot/main.py b/groq_chatbot/main.py new file mode 100644 index 0000000..bb37e99 --- /dev/null +++ b/groq_chatbot/main.py @@ -0,0 +1,49 @@ +from dotenv import load_dotenv +from langchain_groq import ChatGroq +from langchain.agents import create_agent +from tools import search_tool, handle_tool_errors +from langchain_community.tools import DuckDuckGoSearchRun +from langchain.tools import tool +from langchain_core.messages import ToolMessage +from langchain.agents.middleware import wrap_tool_call + + +search = DuckDuckGoSearchRun() + +@tool +def search_tool(query: str) -> str: + """Use DuckDuckGo to search for recent information.""" + results = search.run(query) + return f"Results for: {results}" + +@wrap_tool_call +def handle_tool_errors(request, handler): + """Handle tool execution errors with custom messages.""" + try: + return handler(request) + except Exception as e: + # Return a custom error message to the model + return ToolMessage( + content=f"Tool error: Please check your input and try again. ({str(e)})", + tool_call_id=request.tool_call["id"] + ) + +load_dotenv() + +model = ChatGroq( + model="llama-3.1-8b-instant", + temperature=0, +) + +agent = create_agent( + model, + tools=[search_tool], + middleware=[handle_tool_errors], + system_prompt="You are an intelligent " \ + "assistant that provides accurate information." +) + +question = input("Enter your question: ") + +for chunk in agent.stream({"messages": [{"role": "user", "content": "why sky blue?"}]}, stream_mode="updates"): + print(chunk) \ No newline at end of file diff --git a/groq_chatbot/requirements.txt b/groq_chatbot/requirements.txt new file mode 100644 index 0000000..8f3cdc8 --- /dev/null +++ b/groq_chatbot/requirements.txt @@ -0,0 +1,6 @@ +langchain +python-dotenv +pydantic +duckduckgo-search +ddgs +langchain-community \ No newline at end of file