Navya-Sree commited on
Commit
2de8f47
·
verified ·
1 Parent(s): 4b79491

Create src/agents/genai_integration.py

Browse files
Files changed (1) hide show
  1. src/agents/genai_integration.py +98 -0
src/agents/genai_integration.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import pandas as pd
3
+ import numpy as np
4
+ from typing import Dict, List, Optional
5
+ import json
6
+ import logging
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+ class ForecastingAIAssistant:
11
+ """AI assistant for forecasting tasks using GenAI."""
12
+
13
+ def __init__(self, api_key: str, model_name: str = "gpt-3.5-turbo"):
14
+ self.api_key = api_key
15
+ self.model_name = model_name
16
+ openai.api_key = api_key
17
+
18
+ def generate_forecast_interpretation(self, data_summary: Dict,
19
+ forecast_results: Dict,
20
+ metrics: Dict) -> str:
21
+ """Generate comprehensive interpretation of forecasting results."""
22
+ try:
23
+ prompt = f"""
24
+ As a senior data scientist with 35 years of experience, provide a comprehensive analysis of these forecasting results:
25
+
26
+ Data Summary: {json.dumps(data_summary, indent=2)}
27
+ Forecast Results: {json.dumps(forecast_results, indent=2)}
28
+ Performance Metrics: {json.dumps(metrics, indent=2)}
29
+
30
+ Please provide:
31
+ 1. Key insights about the forecast quality
32
+ 2. Potential business implications
33
+ 3. Limitations of the current approach
34
+ 4. Recommendations for improvement
35
+ 5. Any anomalies or patterns worth noting
36
+
37
+ Write in a professional yet accessible tone suitable for both technical and business audiences.
38
+ """
39
+
40
+ response = openai.ChatCompletion.create(
41
+ model=self.model_name,
42
+ messages=[
43
+ {"role": "system", "content": "You are a senior data scientist with expertise in time series forecasting."},
44
+ {"role": "user", "content": prompt}
45
+ ],
46
+ temperature=0.3,
47
+ max_tokens=1000
48
+ )
49
+
50
+ return response.choices[0].message.content
51
+ except Exception as e:
52
+ logger.error(f"Error generating interpretation: {str(e)}")
53
+ return f"Interpretation generation failed: {str(e)}"
54
+
55
+ def generate_business_recommendations(self, business_context: str,
56
+ forecast_results: Dict,
57
+ historical_data: pd.Series) -> str:
58
+ """Generate business recommendations based on forecasts."""
59
+ try:
60
+ # Create historical data summary
61
+ hist_summary = {
62
+ "period": f"{historical_data.index.min()} to {historical_data.index.max()}" if hasattr(historical_data, 'index') else "N/A",
63
+ "data_points": len(historical_data),
64
+ "mean_value": historical_data.mean(),
65
+ "trend": "upward" if historical_data.iloc[-1] > historical_data.iloc[0] else "downward" if len(historical_data) > 1 else "stable"
66
+ }
67
+
68
+ prompt = f"""
69
+ Based on the forecasting results and business context, provide actionable recommendations:
70
+
71
+ Business Context: {business_context}
72
+ Forecast Results: {json.dumps(forecast_results, indent=2)}
73
+ Historical Trends: {json.dumps(hist_summary, indent=2)}
74
+
75
+ Provide specific, actionable recommendations including:
76
+ 1. Operational adjustments
77
+ 2. Risk mitigation strategies
78
+ 3. Opportunities to capitalize on
79
+ 4. Timeline considerations
80
+ 5. Key metrics to monitor
81
+
82
+ Tailor recommendations to the specific business context.
83
+ """
84
+
85
+ response = openai.ChatCompletion.create(
86
+ model=self.model_name,
87
+ messages=[
88
+ {"role": "system", "content": "You are a business strategy consultant with expertise in data-driven decision making."},
89
+ {"role": "user", "content": prompt}
90
+ ],
91
+ temperature=0.3,
92
+ max_tokens=1000
93
+ )
94
+
95
+ return response.choices[0].message.content
96
+ except Exception as e:
97
+ logger.error(f"Error generating recommendations: {str(e)}")
98
+ return f"Recommendation generation failed: {str(e)}"