Skip to main content

Why Build Your Chatbot with Bigdata.com?

Bigdata.com is designed specifically for business and finance professionals, delivering real-time, AI-driven insights with precision and speed. βœ… Unmatched Data Access β€” Instantly analyze vast, high-quality datasets.
βœ… Grounding with Bigdata.com β€” Generated responses reflect the accurate and real-time market conditions.
βœ… Finance & Business Expertise β€” Designed for professionals, not generic use.
βœ… Seamless Integration β€” Easily power your chatbot with cutting-edge AI.

πŸ“ Overview

This tutorial walks you through deploying and integrating the Financial Research Assistant chatbot on your proprietary platform using a sample Python server chat-server.py and connecting it to Bigdata.com’s Chat Service. Flow Summary:
  • The Web App sends users’ inputs to the Python-based chat server chat-server.py.
  • The server processes these inputs and communicates with Bigdata.com’s Chat Service API.
  • Responses are routed back to the Web App for display.

🧰 Prerequisites

  • Create a directory for your project and navigate to it.
mkdir chat-server
cd chat-server/
  • Follow the how-to guide Prerequisites to set up your Bigdata.com Python SDK environment.

πŸ§ͺ Step 1: Set Up chat-server.py

  1. Place the files chat-server.py and requirements.txt in your project directory.
from flask import Flask, request, jsonify
from flask_cors import CORS
import uuid
from bigdata_client import Bigdata
from bigdata_client.models.chat import ChatScope
from flask import Response, stream_with_context
from dotenv import load_dotenv

app = Flask(__name__)
from flask import Response

class UnbufferedResponse(Response):
    direct_passthrough = True

app.response_class = UnbufferedResponse
CORS(app)

# Initialize Bigdata instance
load_dotenv()
db = Bigdata()

@app.route('/chats', methods=['POST', 'OPTIONS'])
def create_chat():
    if request.method == 'OPTIONS':
        return _build_cors_preflight_response()

    data = request.json
    chat_title = data.get("chat_title")

    chat = db.chat.new(chat_title)
    chat_id = chat.id  # Extract chat ID from the chat object
    print(f"[INFO] Creating chat with ID: {chat_id}")
    print(f"[INFO] Created chat with ID: {chat_id}, Title: {chat_title}")
    return jsonify({"chat_id": chat_id})

@app.route('/chats/ask-stream', methods=['GET'])
def ask_question_stream():
    chat_id = request.args.get("chat_id")
    question = request.args.get("question")
    scope = request.args.get("scope")

    if not chat_id or not question:
        return jsonify({"error": "Missing required parameters"}), 400

    try:
        scope_enum = ChatScope[scope] if scope and scope != "ALL" else None
    except KeyError:
        return jsonify({"error": "Invalid scope value"}), 400

    chat_instance = db.chat.get(chat_id)

    def generate():
        if scope_enum:
            stream = chat_instance.ask(question, scope=scope_enum, streaming=True)
        else:
            stream = chat_instance.ask(question, streaming=True)

        for chunk in stream:
            # Force flush-friendly formatting (newline-separated, short)
            yield f"data: {chunk}\n\n"

        yield "event: done\ndata: [DONE]\n\n"


    return Response(stream_with_context(generate()), mimetype='text/event-stream')


@app.route('/chats', methods=['DELETE', 'OPTIONS'])
def delete_chat():
    if request.method == 'OPTIONS':
        return _build_cors_preflight_response()

    data = request.json
    chat_id = data.get("chat_id")

    db.chat.delete(chat_id)
    print(f"[INFO] Deleted chat with ID: {chat_id}")

    return jsonify({"message": "Chat deleted successfully"})

def _build_cors_preflight_response():
    response = jsonify({})
    response.headers.add("Access-Control-Allow-Origin", "*")
    response.headers.add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE")
    response.headers.add("Access-Control-Allow-Headers", "Content-Type")
    return response

if __name__ == "__main__":
    app.run(debug=True, port=5000)
flask>=2.0.0
flask-cors>=3.0.10
bigdata-client>=2.14.0
Project structure:
chat-server/
β”œβ”€β”€ bigdata_venv/
β”œβ”€β”€ .env
β”œβ”€β”€ chat-server.py
└── requirements.txt
  1. Install dependencies:
pip install -r requirements.txt
  1. Run the server:
python chat-server.py
By default, this starts on http://localhost:5000 unless specified otherwise.

πŸ§‘β€πŸ’» Step 2: Connect from Your Web App

We built a Sample App that you can use as PoC. By default it communicates to the chat-server.py on http://localhost:5000

Sample App

Click here to open it and start chating with Bigdata.com πŸ₯³πŸš€
⌘I