Skip to content

Repository files navigation

Agronomy & Finance AI Backend

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.

For Frontend Developers: How to Interact with the API

Your frontend application will communicate with this backend through a single primary endpoint.

🚀 Primary Endpoint: /query

  • 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.

Request Format

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.

Response Format

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 using react-markdown.

  • agent_used (string): Indicates which agent processed the request (Agronomy Agent, Agri-Finance Agent, or None). 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.

Example Interaction using curl

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"}'

How to Run the Backend Locally

To run the server on your machine for development and testing:

  1. Clone the repository:

    git clone [https://github.com/BhimPrasadAdhikari/chatbot-backend.git](https://github.com/BhimPrasadAdhikari/chatbot-backend.git)
    cd chatbot-backend
  2. 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
  3. Configure Environment Variables:

    • Create a file named .env in the root of the project.

    • Add the required variables (ask a backend developer for the GCLOUD_PROJECT_ID and the service account key).

    • Set the GOOGLE_APPLICATION_CREDENTIALS environment variable.

  4. 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 at http://127.0.0.1:8000/docs.

About

FastAPI designed to interact with top-notch gemini model

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages