Prompt Engineering
Relevant source files
The following files were used as context for generating this wiki page:
Prompt engineering in this project is the practice of carefully designing inputs for the Google Gemini AI model to achieve specific, reliable outcomes. It is primarily used for two distinct features: structured data extraction from emergency call transcripts and dynamic, tool-augmented conversational chat. The system leverages prompt instructions, conversation history, and schema definitions to guide the AI's responses.
The core logic for interacting with the Gemini API is centralized in geminiService.ts, which is then consumed by the AiDemo and Chatbot components.
Sources: services/geminiService.ts, components/AiDemo.tsx, components/Chatbot.tsx, App.tsx:64,115
Use Case 1: Emergency Transcript Analysis
This feature, demonstrated in the AiDemo component, simulates the real-time analysis of an emergency call. The primary goal of the prompt engineering here is to convert an unstructured conversation transcript into a structured JSON object that the application can easily parse and display.
Sources: components/AiDemo.tsx, services/geminiService.ts
Prompt Structure and Schema Enforcement
The analyzeEmergencyCallWithGemini function constructs a prompt that instructs the AI to analyze a transcript and return the output in a specific JSON format. This is achieved by combining a direct instruction with the transcript data and, most importantly, by specifying a responseSchema in the API call configuration. This forces the Gemini model to return a JSON object that conforms to the predefined structure.
The prompt template is as follows:
const prompt = `Analyze the following emergency call transcript. Extract the key information and classify the incident.
Transcript:
---
${transcript}
---
Return the analysis in the specified JSON format. If a value is not present in the transcript, return an empty string or null for it.
`;
Sources: services/geminiService.ts:31-38
The responseMimeType: "application/json" and responseSchema configurations are critical for ensuring a predictable output format.
Sources: services/geminiService.ts:43-46
Data Flow for Transcript Analysis
The diagram below illustrates the process from the user starting the simulation to the AI analysis being displayed.
This flowchart shows the data flow for the AI-powered emergency dispatch simulation.
Sources: components/AiDemo.tsx:50-63, 91-94, services/geminiService.ts:10-63
Output Data Schema (AiAnalysisResult)
The prompt is engineered to produce a JSON object matching the AiAnalysisResult type. The schema is defined both in types.ts and reiterated in the analyzeEmergencyCallWithGemini function for the API call.
| Field | Type | Description | Required |
|---|---|---|---|
incidentType | string | null | e.g., Cardiac Emergency, Trauma, Fire | Yes |
location | string | null | The full address of the incident | Yes |
severity | 'Critical' | 'Urgent' | 'Non-Urgent' | null | One of: Critical, Urgent, Non-Urgent | Yes |
recommendedAction | string | null | e.g., Dispatch ALS Ambulance | Yes |
extractedData | object | Contains nested patient data | Yes |
extractedData.name | string | Yes | |
extractedData.age | string | Yes | |
extractedData.condition | string | Summary of patient's condition | Yes |
Sources: types.ts:11-21, services/geminiService.ts:12-29
Use Case 2: Conversational AI Chatbot
The Chatbot component provides a conversational interface. Its prompt engineering is more dynamic, adapting the AI's capabilities based on user-selected settings. It also manages conversation history to provide context for follow-up questions.
Sources: components/Chatbot.tsx
Dynamic Prompt Configuration
The chatbot UI provides toggles for "Thinking Mode", "Search", and "Maps". These settings directly alter the parameters sent to the Gemini API via the sendMessageToChatStream function.
| Setting | State Variable | Effect on API Call |
|---|---|---|
| Thinking Mode | isThinkingMode | If true, uses the gemini-2.5-pro model and sets thinkingBudget. If false, uses gemini-2.5-flash. |
| Search | useSearch | If true, adds { googleSearch: {} } to the tools array in the API call. |
| Maps | useMaps | If true, adds { googleMaps: {} } to the tools array in the API call. |
This dynamic configuration allows the prompt to be implicitly enhanced with different models or external tools without changing the user's text input.
Sources: components/Chatbot.tsx:43-45, 221-223, services/geminiService.ts:72-86
The logic for this is handled in sendMessageToChatStream:
// services/geminiService.ts
export async function sendMessageToChatStream(
// ...
isThinkingMode: boolean,
useSearch: boolean,
useMaps: boolean
) {
const model = isThinkingMode ? 'gemini-2.5-pro' : 'gemini-2.5-flash';
const tools: any[] = [];
if (useSearch) tools.push({ googleSearch: {} });
if (useMaps) tools.push({ googleMaps: {} });
const config: any = {};
if (isThinkingMode) {
config.thinkingConfig = { thinkingBudget: 32768 };
}
if (tools.length > 0) {
config.tools = tools;
}
// ...
}
Sources: services/geminiService.ts:72-89
Chat Interaction Flow
The chatbot maintains a state of the conversation and sends it along with the new message. This provides the necessary context for the AI to hold a coherent conversation. The response is streamed back to the UI for a more responsive user experience.
This sequence diagram shows the process of a user sending a message in the chatbot and receiving a streamed response.
Sources: components/Chatbot.tsx:78-111, services/geminiService.ts:72-92
Summary
The project employs two distinct prompt engineering strategies. For transcript analysis, it uses a static prompt with a strict JSON schema to ensure reliable, structured data extraction. For the chatbot, it uses a more dynamic approach, modifying the AI model and enabling tools based on user settings, while maintaining conversational context through history management. Both strategies are essential for powering the core AI features of the application.