demeter

Autonomous Hydroponic Intelligence

agent/sub_agents/base_agent.py (1874B)


 1import os
 2from openai import AzureOpenAI
 3from dotenv import load_dotenv
 4
 5load_dotenv()
 6
 7# --- AZURE OPENAI CONFIGURATION ---
 8DEPLOYMENT_NAME = os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4.1")
 9API_KEY = os.environ.get("AZURE_OPENAI_API_KEY")
10ENDPOINT = os.environ.get("AZURE_OPENAI_ENDPOINT")
11API_VERSION = os.environ.get("AZURE_OPENAI_API_VERSION", "2024-12-01-preview")
12
13class BaseReasoningAgent:
14    def __init__(self, name):
15        self.name = name
16
17        if not API_KEY or not ENDPOINT:
18            print(f"[{self.name}] ⚠️ WARNING: Azure OpenAI credentials not found in environment.")
19            self.client = None
20        else:
21            try:
22                self.client = AzureOpenAI(
23                    api_key=API_KEY,
24                    api_version=API_VERSION,
25                    azure_endpoint=ENDPOINT
26                )
27            except Exception as e:
28                print(f"[{self.name}] ⚠️ Azure OpenAI Connection Error: {e}")
29                self.client = None
30
31    def _call_llm(self, prompt):
32        """
33        Helper method to send prompts to Azure OpenAI.
34        """
35        if not self.client:
36            return "Error: LLM Client not connected (Check API Key)."
37        
38        print("Other Prompt:\n", prompt)
39        try:
40            # Azure OpenAI Chat Completion Structure
41            response = self.client.chat.completions.create(
42                model=DEPLOYMENT_NAME,
43                messages=[
44                    {"role": "system", "content": f"You are the {self.name} Agent for a high-tech hydroponic farm."},
45                    {"role": "user", "content": prompt}
46                ],
47                temperature=0.6, 
48                max_tokens=1024
49            )
50            return response.choices[0].message.content
51            
52        except Exception as e:
53            return f"Reasoning Error: {e}"