Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions groq_chatbot/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GROQ_API_KEY="your_groq_api_key_here"
53 changes: 53 additions & 0 deletions groq_chatbot/Readme.md
Original file line number Diff line number Diff line change
@@ -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








49 changes: 49 additions & 0 deletions groq_chatbot/main.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions groq_chatbot/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
langchain
python-dotenv
pydantic
duckduckgo-search
ddgs
langchain-community