This repository contains the backend server for the Agronomy & Finance AI application. It is built with Python using the FastAPI framework and is powered by Google's Gemini models on Vertex AI.
The server exposes a single, powerful endpoint that provides access to specialized AI agents for answering questions related to agriculture and agricultural finance.
Your frontend application will communicate with this backend through a single primary endpoint.
-
URL:
http://127.0.0.1:8000/query(when running locally) -
Method:
POST -
Description: This is the main endpoint for sending user questions to the AI. The backend orchestrator will classify the question and route it to the appropriate agent.
You must send a POST request with a JSON body containing the user's query and a unique session ID.
Headers:
Content-Type:application/json
Body:
{
"query": "What is a good soil pH for growing maize?",
"session_id": "user-session-f8a9b2c1"
}-
query(string, required): The full, unprocessed question from the user. -
session_id(string, required): A unique identifier for the user's session. This is used for logging and potential future features like conversation history. You can generate a UUID on the client side when a new chat session begins.
The server will respond with a JSON object containing the AI's answer and which agent was used.
On Success (Status Code: 200 OK):
{
"answer": "According to the internal knowledge base, the optimal soil pH for maize is between 5.8 and 7.0...",
"agent_used": "Agronomy Agent",
"session_id": "user-session-f8a9b2c1"
}-
answer(string): The raw markdown formatted answer, before displaying the answer it need to be formatted usingreact-markdown. -
agent_used(string): Indicates which agent processed the request (Agronomy Agent,Agri-Finance Agent, orNone). This can be used for debugging or displaying an agent icon in the UI. -
session_id(string): The session ID that was passed in the request.
You can use this curl command to test the backend from your terminal:
curl -X POST "[http://127.0.0.1:8000/query](http://127.0.0.1:8000/query)" \
-H "Content-Type: application/json" \
-d '{"query": "What is the interest rate for a Kisan Credit Card loan?", "session_id": "test-finance-002"}'To run the server on your machine for development and testing:
-
Clone the repository:
git clone [https://github.com/BhimPrasadAdhikari/chatbot-backend.git](https://github.com/BhimPrasadAdhikari/chatbot-backend.git) cd chatbot-backend -
Set up the environment: Make sure you have Python 3.9+ installed.
python -m venv venv source venv/bin/activate # On Windows: .\venv\Scripts\activate pip install -r requirements.txt
-
Configure Environment Variables:
-
Create a file named
.envin the root of the project. -
Add the required variables (ask a backend developer for the
GCLOUD_PROJECT_IDand the service account key). -
Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable.
-
-
Run the server:
uvicorn main:app --reload
The API will now be available at
http://127.0.0.1:8000. You can see the interactive documentation athttp://127.0.0.1:8000/docs.