|  | import os | 
					
						
						|  | import json | 
					
						
						|  | import requests | 
					
						
						|  | from dotenv import load_dotenv | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | load_dotenv("key.env") | 
					
						
						|  |  | 
					
						
						|  | class ContextualAPIUtils: | 
					
						
						|  | def __init__(self, api_key, endpoint_url, application_id): | 
					
						
						|  | self.api_key = api_key | 
					
						
						|  | self.endpoint_url = endpoint_url | 
					
						
						|  | self.application_id = application_id | 
					
						
						|  |  | 
					
						
						|  | def chat(self, prompt): | 
					
						
						|  | """Make a request to the API with the given prompt.""" | 
					
						
						|  | body = { | 
					
						
						|  | "application_id": self.application_id, | 
					
						
						|  | "messages": [{"role": "user", "content": prompt}], | 
					
						
						|  | "stream": False | 
					
						
						|  | } | 
					
						
						|  | headers = {"Authorization": f"Bearer {self.api_key}"} | 
					
						
						|  |  | 
					
						
						|  | try: | 
					
						
						|  | response = requests.post(self.endpoint_url, headers=headers, json=body) | 
					
						
						|  | if response.status_code == 200: | 
					
						
						|  | return response.json().get("message", {}).get("content", "No content") | 
					
						
						|  | else: | 
					
						
						|  | return f"Error: {response.status_code} - {response.text}" | 
					
						
						|  | except Exception as e: | 
					
						
						|  | return f"Request failed: {e}" | 
					
						
						|  |  | 
					
						
						|  | def get_api_credentials(): | 
					
						
						|  | """Fetch API credentials and validate them.""" | 
					
						
						|  | api_key = os.getenv("API_KEY") | 
					
						
						|  | endpoint_url = os.getenv("ENDPOINT_URL") | 
					
						
						|  | application_id = os.getenv("APPLICATION_ID") | 
					
						
						|  |  | 
					
						
						|  | if not all([api_key, endpoint_url, application_id]): | 
					
						
						|  | raise ValueError("Missing API credentials. Check your 'key.env' file.") | 
					
						
						|  | return api_key, endpoint_url, application_id | 
					
						
						|  |  | 
					
						
						|  | def main(): | 
					
						
						|  |  | 
					
						
						|  | try: | 
					
						
						|  | api_key, endpoint_url, application_id = get_api_credentials() | 
					
						
						|  | except ValueError as e: | 
					
						
						|  | print(e) | 
					
						
						|  | return | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | api_utils = ContextualAPIUtils(api_key, endpoint_url, application_id) | 
					
						
						|  |  | 
					
						
						|  | print("Welcome! Press Ctrl+C to exit at any time.") | 
					
						
						|  | try: | 
					
						
						|  | while True: | 
					
						
						|  | prompt = input("\nEnter your prompt: ").strip() | 
					
						
						|  | if not prompt: | 
					
						
						|  | print("Prompt cannot be empty. Please try again.") | 
					
						
						|  | continue | 
					
						
						|  |  | 
					
						
						|  | print("Fetching response...") | 
					
						
						|  | response = api_utils.chat(prompt) | 
					
						
						|  | print("Response: \n", response) | 
					
						
						|  |  | 
					
						
						|  | except KeyboardInterrupt: | 
					
						
						|  | print("\nProgram terminated by user.") | 
					
						
						|  |  | 
					
						
						|  | if __name__ == "__main__": | 
					
						
						|  | main() | 
					
						
						|  |  | 
					
						
						|  |  |