pitwall-f1-copilot / src /granite_engine.py
ashish-doing's picture
fix: chart PNG render fix
11b96a1
Raw
History Blame Contribute Delete
2.39 kB
import os
from openai import OpenAI
HF_TOKEN = os.environ.get("HF_TOKEN", "")
client = OpenAI(
base_url="https://api.groq.com/openai/v1",
api_key=os.environ.get("GROQ_API_KEY", ""),
)
def query_granite(prompt: str) -> str:
try:
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}],
max_tokens=400,
temperature=0.7,
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
def analyze_strategy(race_summary: dict) -> str:
try:
from docling_parser import get_pit_rules_context
reg_context = get_pit_rules_context()[:800]
except:
reg_context = "Standard F1 pit stop rules apply."
prompt = f"""You are an expert F1 race strategist. Analyze this race data and provide:
1. Assessment of the pit stop strategy used
2. Whether the timing was optimal
3. What alternative strategy could have been faster
4. Key insights from the tire compounds used
REGULATIONS CONTEXT:
{reg_context}
RACE DATA:
- Driver: {race_summary['driver']}
- Grand Prix: {race_summary['grand_prix']} {race_summary['year']}
- Total Laps: {race_summary['total_laps']}
- Compounds Used: {', '.join(race_summary['compounds_used'])}
- Pit Stop Laps: {race_summary['pit_stop_laps']}
- Average Lap Time: {race_summary['avg_lap_time']}s
- Best Lap Time: {race_summary['best_lap_time']}s
Provide a concise strategic analysis in 3 paragraphs."""
return query_granite(prompt)
def recommend_pit_window(lap: int, compound: str, tyre_life: int, lap_time_delta: float) -> str:
prompt = f"""You are an F1 pit wall strategist. Given the current race state:
- Current lap: {lap}
- Tyre compound: {compound}
- Tyre age: {tyre_life} laps
- Lap time delta vs personal best: +{lap_time_delta:.3f}s
Respond in exactly this format:
DECISION: [PIT NOW / PIT IN 2-3 LAPS / STAY OUT]
REASONING:
- [One sentence on tyre condition]
- [One sentence on timing/track position risk]
- [One sentence on recommended action]
RISK IF IGNORED: [One sentence on consequence of not following recommendation]"""
return query_granite(prompt)
if __name__ == "__main__":
print("Testing Granite via HF OpenAI endpoint...")
result = recommend_pit_window(25, "MEDIUM", 20, 0.8)
print(result)