Created
September 30, 2025 20:46
-
-
Save adeydas/154727458e76f572992d0b1609a2deef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import instructor | |
| import boto3 | |
| import json | |
| from pydantic import BaseModel, Field | |
| from enum import Enum | |
| from typing import List | |
| route1 = """ | |
| 32.7558565,-96.9041442 -> 32.9912375,-96.9416758 -> 32.8646347,-97.0303599 | |
| """ | |
| route2 = """ | |
| 32.7558565,-96.9041442 -> 32.8646347,-97.0303599 | |
| """ | |
| client = boto3.client(service_name='bedrock-runtime', region_name='us-west-2') | |
| model_id = 'us.anthropic.claude-3-7-sonnet-20250219-v1:0' | |
| # Instructor makes it easy to get structured data like JSON from LLMs | |
| client = instructor.from_provider("bedrock/anthropic.claude-3-7-sonnet-20250219-v1:0") | |
| class StopFraudScore(str, Enum): | |
| LOW = "low" | |
| MEDIUM = "medium" | |
| HIGH = "high" | |
| CRITICAL = "critical" | |
| class StopClassification(BaseModel): | |
| stop_fraud_score: StopFraudScore = Field(description="Risk level of potential fraud associated with the route") | |
| confidence: float = Field(ge=0, le=1, description="Confidence score for the classification") | |
| key_information: List[str] = Field(description="List of key points extracted from the ticket") | |
| stop_classification = StopClassification( | |
| stop_fraud_score=StopFraudScore.HIGH, | |
| confidence=0.95, | |
| key_information=["Stopped at a restaurant", "Unusual route pattern"] | |
| ) | |
| SYSTEM_PROMPT = """ | |
| You are an AI assistant for a transportation company focussed on determining if a route driven by a truck driver is potentially fraudulent. | |
| Your role is to analyze the route and provide structured information to help our team assess the situation quickly and effectively. | |
| Business Context: | |
| - We handle thousands of routes daily across various regions. | |
| - Quick and accurate classification is crucial for operational efficiency and fraud prevention. | |
| - Providing the right confidence score helps our team prioritize investigations. | |
| Your tasks: | |
| 1. Classify the route into one of the predefined fraud risk levels (low, medium, high, critical). | |
| 2. Provide a confidence score for your classification (between 0 and 1). | |
| 3. Extract key information that would be helpful for our team to understand the context of the route. | |
| 4. Add the potential fraudelent stop in key information if applicable. Expand the GPS coordinates to the actual location name. | |
| Rules: | |
| 1. A truck driver is considered to be potentially committing fraud if they deviate significantly from their expected route, make unnecessary stops, or take routes that are not optimal for the delivery. | |
| 2. Be objective and base your analysis solely on the information provided in the route. | |
| 3. If you're unsure about any aspect, reflect that in your confidence score. | |
| 4. For 'key_information', extract specific details like stops made, route deviations, or any unusual patterns. | |
| 5. Provide the output in the specified JSON format. | |
| Thought Process: | |
| 1. A driver may stop at a gas station or restaurant, which is normal. However, multiple stops or detours could indicate potential fraud. | |
| 2. Consider the overall route efficiency. A significantly longer route than necessary could be a red flag. | |
| 3. Look for patterns that deviate from typical driving behavior, such as backtracking or circuitous routes. | |
| 4. A driver may not stop at their house or personal locations, as this could indicate misuse of company time or resources. | |
| 5. You will be provided GPS coordinates of the route taken by the driver. Analyze these coordinates and the locations they represent to identify any unusual patterns or stops. | |
| 6. To understand the locations the GPS coordinates represent, you may need to use external tools or databases to map these coordinates to real-world locations (e.g., restaurants, gas stations, residential areas). | |
| """ | |
| def classify_route(route_coords: str) -> StopClassification: | |
| body = json.dumps({ | |
| "anthropic_version": "bedrock-2023-05-31", | |
| "max_tokens": 100, | |
| "messages": [ | |
| {"role": "user", "content": SYSTEM_PROMPT} | |
| ] | |
| }) | |
| response = client.invoke_model( | |
| body=body, | |
| modelId=model_id, | |
| accept='application/json', | |
| contentType='application/json' | |
| ) | |
| return response | |
| result1 = classify_route(route1) | |
| result2 = classify_route(route2) | |
| print(result1.model_dump_json(indent=2)) | |
| print(result2.model_dump_json(indent=2)) | |
Author
adeydas
commented
Sep 30, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment