Skip to content

About

AI coding assistant backend built with FastAPI, LangGraph, and Gemini, featuring intent routing, retrieval, tool execution, and real-time streaming responses.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

28 Commits

Folders and files

Repository files navigation

TalentIQ AI Assistant Backend

A modular AI assistant backend built with FastAPI, LangGraph, and Google Gemini that provides intelligent coding assistance inside the TalentIQ coding workspace.

The backend powers an AI coding instructor capable of:

  • Understanding user coding questions
  • Detecting user intent
  • Building contextual prompts
  • Retrieving relevant programming knowledge
  • Requesting code execution through an external Express service
  • Forwarding emitted model events as Server-Sent Events (SSE)

The system is designed around a LangGraph workflow architecture, allowing future expansion into more advanced agentic capabilities such as dynamic tool selection, multi-step reasoning, and long-term memory.


Features

AI Conversation Pipeline

The assistant processes every request through a structured LangGraph workflow:

User Request
     |
     v
Intent Detection
     |
     v
Conversation Context Builder
     |
     v
Knowledge Retrieval
     |
     v
Tool Planning
     |
     v
Tool Execution
     |
     v
Prompt Assembly
     |
     v
LLM Generation
     |
     v
Streaming Response

Each stage has a dedicated responsibility, making the system easier to maintain and extend.


Core Capabilities

Intent-Based Assistance

The assistant classifies incoming messages into supported interaction modes:

General

General programming conversations and explanations.

Hint

Prompt-guided hints intended to provide incremental guidance without revealing complete solutions.

Debug

Analyzes compiler errors, runtime failures, and incorrect behavior.

Review

Reviews implementations for:

  • Code quality
  • Complexity
  • Maintainability
  • Edge cases

Explain

Focuses on teaching programming concepts and fundamentals.


Architecture

Technology Stack

Backend

  • FastAPI
  • Python
  • LangGraph
  • LangChain
  • Pydantic
  • SSE (Server Sent Events)

AI Model

  • Google Gemini
  • gemini-2.5-flash

External Services

  • External Express.js code-execution service
  • Frontend coding workspace

Project Structure

app
│
├── main.py
│
├── config.py
├── logger.py
│
├── schemas
│   └── request.py
│
├── security
│   └── hmac.py
│
├── streaming
│   └── sse.py
│
└── graph
    │
    ├── builder.py
    ├── state.py
    ├── intents.py
    │
    ├── nodes
    │   ├── node.py
    │   └── routing.py
    │
    ├── prompts
    │   ├── base.py
    │   ├── registry.py
    │   ├── general.py
    │   ├── hint.py
    │   ├── debug.py
    │   ├── review.py
    │   └── explain.py
    │
    ├── context
    │   ├── context.py
    │   └── context_formatter.py
    │
    ├── retrieval
    │   ├── repository.py
    │   └── retrieval.py
    │
    ├── tools
    │   ├── tool_planning.py
    │   ├── tool_execution.py
    │   ├── tool_executor.py
    │   └── tool_schema.py
    │
    ├── llm
    │   └── llm.py
    │
    └── knowledge
        └── data
            ├── arrays.json
            └── hashmap.json

LangGraph Workflow

The assistant uses LangGraph to model the AI reasoning pipeline.

The graph state contains:

GraphState

which stores:

  • User conversation history
  • Problem context
  • Execution context
  • Detected intent
  • Retrieved knowledge
  • Tool plans
  • Tool outputs
  • Prompt messages
  • Final response

Each graph node modifies only the part of the state it owns.


Request Flow

1. Client Request

The frontend sends:

{
  "problem_id": "two-sum",
  "problem_context": {
    "title": "Two Sum",
    "description": "Find two numbers in an array that add up to a target.",
    "constraints": ["Exactly one solution exists"],
    "tags": ["two-sum", "hashmap"]
  },
  "execution_context": {
    "language": "python",
    "user_code": "",
    "compiler_output": ""
  },
  "chat_history": [
    {
      "role": "user",
      "content": "Can you help me solve this?"
    }
  ]
}

The request contains:

Problem Context

Information about the coding challenge:

  • Title
  • Description
  • Constraints

Execution Context

Current workspace state:

  • Programming language
  • User code
  • Compiler output

Chat History

Previous conversation messages.


2. Intent Detection

The router analyzes the latest user message.

Example:

"Why am I getting this runtime error?"

becomes:

DEBUG

Example:

"Can you explain binary search?"

becomes:

EXPLAIN

3. Context Preparation

Conversation history is converted into LangChain message objects.

The assistant currently uses recent conversation history to keep prompts efficient.


4. Retrieval Layer

The retrieval pipeline provides additional knowledge context.

Currently it performs tag-based matching against local JSON knowledge assets:

  • Matches problem-context tags against tags in the local JSON knowledge assets
  • Injects matching entries into the final prompt

It does not currently perform vector search or retrieve directly from compiler output or execution context. The retrieval layer is isolated so it can later be replaced with:

  • MongoDB vector search
  • Chroma
  • Pinecone
  • Weaviate
  • Other vector databases

without affecting the graph architecture.


5. Tool Execution

The assistant integrates with the existing external Express code-execution service.

The backend forwards the selected editor language to the Express execution service. The actual supported languages and runtime availability are determined by that service. The current integration is intended for:

  • Python
  • JavaScript

Java is not implemented or validated in this repository.

The execution pipeline provides:

  • Program output
  • Runtime errors
  • Service-reported compilation failures

which are injected back into the AI reasoning context.

Hint mode is currently prompt-driven. The planner creates a structured request_hint action, but the execution node only dispatches execute_code actions to the Express service; no separate hint service is currently wired.


6. Prompt Construction

Prompts are dynamically selected based on intent.

Example:

DEBUG
 |
 v
debug.py prompt builder
HINT
 |
 v
hint.py prompt builder

Each prompt extends a shared base coding instructor prompt.


7. LLM Generation

The final structured message payload is sent to Gemini.

The model receives:

  • System instructions
  • Problem information
  • User workspace
  • Conversation context
  • Retrieved knowledge
  • Tool execution results

Streaming Responses

The backend returns a Server-Sent Events (SSE) response and forwards chat-model chunks emitted through LangGraph's event stream. Incremental delivery depends on the model provider and deployment path; end-to-end browser streaming has not been independently verified.

The response flow:

Gemini Model Events
      |
      v
LangGraph Event Stream
      |
      v
FastAPI StreamingResponse
      |
      v
Frontend Chat Interface

API Reference

Health Check

GET /health

Response:

{
  "status": "awake"
}

Chat Endpoint

POST /api/graph/chat

Headers:

X-Signature
X-Timestamp

Request:

{
  "problem_id": "two-sum",
  "problem_context": {
    "title": "Two Sum",
    "description": "Find two numbers",
    "constraints": [],
    "tags": ["two-sum"]
  },

  "execution_context": {
    "language": "python",
    "user_code": "",
    "compiler_output": ""
  },

  "chat_history": [
    {
      "role": "user",
      "content": "Help debug this"
    }
  ]
}

Response:

text/event-stream

Example:

data: The issue is caused by...

data: because your loop condition...

event: done
data: done

Environment Setup

Requirements

  • Python 3.12+
  • Google Gemini API Key

Installation

Clone repository:

git clone <repository-url>

cd TalentIQ-AI-Assistant-LangGraph-FastAPI

Install dependencies and sync the managed environment:

uv sync

The project declares dependencies in pyproject.toml and pins the resolved environment in uv.lock. This repository does not include a requirements.txt file.


Environment Variables

Create:

.env

Example:

GOOGLE_API_KEY=your_google_api_key

CLIENT_URL=http://localhost:5173

EXPRESS_API_URL=http://localhost:3000/api

INTERNAL_SHARED_SECRET=your_shared_secret

Running the Backend

Start FastAPI:

uvicorn app.main:app --reload

When using uv, the equivalent command keeps execution inside the managed environment:

uv run uvicorn app.main:app --reload

Server:

http://localhost:8000

Security

The chat endpoint uses HMAC request verification.

The middleware validates:

  • Request signature
  • Timestamp freshness
  • Verification using the configured shared secret

Protection includes:

  • Rejection of requests outside a 30-second timestamp window
  • Timing-attack-resistant signature comparison

This provides request-integrity and authenticity checks between parties that share the secret; it is not user authentication and does not prevent replay within the accepted timestamp window. Configure a strong INTERNAL_SHARED_SECRET in every non-local environment; the current application has a development fallback when the variable is absent. Remove that fallback before deployment.


Design Principles

Separation of Concerns

The project separates:

API Layer

Responsible for:

  • HTTP requests
  • Authentication
  • Streaming responses

Graph Layer

Responsible for:

  • AI workflow orchestration

Prompt Layer

Responsible for:

  • Assistant behavior

Tool Layer

Responsible for:

  • External actions

Retrieval Layer

Responsible for:

  • Knowledge enrichment

Model Layer

Responsible for:

  • LLM provider interaction

Future Improvements

The architecture supports future additions:

Advanced Agent Loop

Move from:

Plan -> Execute -> Respond

towards:

Observe
   |
Reason
   |
Act
   |
Observe
   |
Refine

Persistent Memory

Possible additions:

  • User learning history
  • Previous solutions
  • Coding patterns
  • Personalized teaching

Production Retrieval

Replace current retrieval implementation with:

  • MongoDB Atlas Vector Search
  • Dedicated knowledge database
  • Embedding pipeline

Observability

Future monitoring:

  • LangSmith tracing
  • Request latency tracking
  • Token usage analytics
  • Failure monitoring

Current Status

The backend currently provides:

✅ FastAPI API layer
✅ LangGraph orchestration
✅ Gemini integration
✅ Intent-based routing
✅ Context management
✅ Tag-based local JSON retrieval
✅ Express-backed code execution for debug requests
✅ SSE response streaming when provider events are emitted
✅ HMAC request validation

The repository does not currently include a runnable automated test suite. The Express execution service and Google Gemini credentials are external runtime dependencies.

The architecture is prepared for future expansion into a more advanced coding assistant system.

About

AI coding assistant backend built with FastAPI, LangGraph, and Gemini, featuring intent routing, retrieval, tool execution, and real-time streaming responses.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages