DEV Community

br-raia
br-raia

Posted on

Using AI to facilitate backend logic

Hey DEV community,

I guess everyone has heard of GitHub Copilot right now, and many of you are using it (well, I am) to help with code development.

In addition, I wanted to use AI to hold much of the backend logic. To test that, I decided to build a Stock Analysis application.

Here is what I used and did:

  • I created a frontend where my users could enter the Stock Symbol they want to receive the analysis
  • I created a Python backend with a few endpoints (to send data and receive data)
  • I used my account on Raia to create the stock analysis process
  • The analysis process created on Raia then calls back the backend Python service, sending the result

Here is the code for my backend:

import logging
import uuid
from flask import render_template, request, jsonify
from app import app
import requests

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

users = {}
user_analysis_results = {}

@app.route('/')
def index():
    logger.info("Rendering index page")
    return render_template('index.html')

@app.route('/submit-symbol', methods=['POST'])
def submit_symbol():
    try:
        user_id = str(uuid.uuid4())
        stock_symbol = request.form['stock_symbol']
        users[user_id] = stock_symbol
        logger.info(f"Received symbol: {stock_symbol} for generated user ID: {user_id}")
        # Trigger external workflow here
        trigger_workflow()
        return jsonify({"message": "Symbol received", "user_id": user_id, "stock_symbol": stock_symbol})
    except Exception as e:
        logger.error(f"Error in submit_symbol: {e}")
        return jsonify({"error": str(e)}), 500

def trigger_workflow():
    try:
        external_workflow_url = "https://hooks.raia.live/hooks/catch/xNVvu31VX9EItm4"
        response = requests.post(external_workflow_url)
        logger.info(f"Workflow triggered with status code: {response.status_code}")
        return response.status_code
    except Exception as e:
        logger.error(f"Error in trigger_workflow: {e}")
        return None

@app.route('/get-symbols', methods=['GET'])
def get_symbols():
    try:
        return jsonify(users)
    except Exception as e:
        logger.error(f"Error in get_symbols: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/receive-analysis', methods=['POST'])
def receive_analysis():
    try:
        user_id = request.json['user_id']
        analysis_result = request.json['analysis_result']
        user_analysis_results[user_id] = analysis_result
        logger.info(f"Received analysis for user: {user_id}")
        return jsonify({"message": "Analysis received", "user_id": user_id})
    except Exception as e:
        logger.error(f"Error in receive_analysis: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/get-analysis/<user_id>', methods=['GET'])
def get_analysis(user_id):
    try:
        analysis_result = user_analysis_results.get(user_id, "")
        return jsonify({"user_id": user_id, "analysis_result": analysis_result})
    except Exception as e:
        logger.error(f"Error in get_analysis: {e}")
        return jsonify({"error": str(e)}), 500
Enter fullscreen mode Exit fullscreen mode

Stock Analysis

On the Raia front, here is the sample stock analysis workflow I created:

Raia Workflow

Here is what this workflow does:

  • Receives the stock symbol from the application
  • Gathers data like the company financials, analyst recommendations, stock history, and company news
  • Perform the analysis based on all this data and gives me an estimated trend for the stock
  • Sends it back to the application/the user

By using this approach, I created a stock analysis "platform" having to handle very few details on the application side and using AI to help me handle the heavy logic, data integration, and data analysis

I also used AI to help me create the frontend and backend code.

Overall, this took me about 30 minutes to create everything, which would have taken me way more to create everything if I had to handle everything myself, including the analysis

Is that the future? Have you all considered using AI to handle the backend logic for your applications instead of having to build it all in?

Top comments (0)