/ ai.py
ai.py
1 #!/usr/bin/env python3 2 import json 3 import urllib.request 4 import sys 5 6 # Your working Groq API key 7 API_KEY = "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" 8 9 def ask_ai(question): 10 """Ask the AI a question""" 11 url = "https://api.groq.com/openai/v1/chat/completions" 12 13 data = json.dumps({ 14 "model": "llama-3.3-70b-versatile", 15 "messages": [{"role": "user", "content": question}], 16 "max_tokens": 1000, 17 "temperature": 0.7 18 }).encode('utf-8') 19 20 try: 21 req = urllib.request.Request( 22 url, 23 data=data, 24 headers={ 25 'Authorization': f'Bearer {API_KEY}', 26 'Content-Type': 'application/json' 27 } 28 ) 29 30 with urllib.request.urlopen(req, timeout=20) as response: 31 result = json.loads(response.read().decode('utf-8')) 32 return result['choices'][0]['message']['content'] 33 except Exception as e: 34 return f"Error: {str(e)}" 35 36 # Main execution 37 if __name__ == "__main__": 38 if len(sys.argv) > 1: 39 question = " ".join(sys.argv[1:]) 40 else: 41 # Read from stdin if no arguments 42 question = sys.stdin.read().strip() 43 if not question: 44 question = input("Ask me anything: ") 45 46 print("\n🤖 AI Assistant:") 47 print("=" * 60) 48 response = ask_ai(question) 49 print(response) 50 print("=" * 60)