- An Ollama AI model server
- SerpAPI for web search
- MongoDB for persistent chat memory
-
Clone the repo:
git clone https://github.com/drewesk/AIQueryApp.git cd langchain_flask_serpapi_mongo
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
Create a
.env
file with:SERPAPI_API_KEY=your_serpapi_api_key MONGODB_URI=your_mongodb_connection_string
SerpAPI setup
- Go to serpapi.com, sign up.
- Copy your API key.
- Add to
.env
MongoDB setup
- Go to mongodb.com/cloud/atlas and sign up.
- Create a free shared cluster.
- In Database Access, create a user with a username and password.
- In Network Access, add IP
0.0.0.0/0
to allow all connections. - Click Connect → Connect your application.
- Choose:
- Driver: Python
- Version: Latest (e.g. 3.12 or later)
- Copy the connection auto-genearated string, then add to your
.env
file
-
Run Ollama model server:
ollama serve
-
Pull model:
ollama pull WhiteRabbitNeo/WhiteRabbitNeo-2.5-Qwen-2.5-Coder-7B
or use the of your choice and then modify
app.py
to reflect new new Model Name in GET request. -
Run the Flask app:
python app.py
-
Send POST requests to the Flask app
/chat
endpoint with JSON body:
To activate real-time web search (instead of using the local LLM), prefix your prompt with the word
search
.
This bypasses the local language model and uses your SerpAPI key to fetch live results directly from Google Search.
curl -X POST http://127.0.0.1:5000/chat \
-H "Content-Type: application/json" \
-d '{"prompt":"search the weather in NYC"}'
Or to prompt the AI without using SerpAI, omit using Search
as the first word of the prompt.
Copyright (c) 2025 Andrew Eskenazi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.