/ simple_ai.py
simple_ai.py
1 #!/usr/bin/env python3 2 """ 3 SIMPLE AI QUERY - WORKS ON KALI 4 """ 5 6 import urllib.request 7 import json 8 import sys 9 10 GEMINI_KEY = "AIzaSyC9g4B4sY9xeaUntjNmN2MeWFyp5gL3_EM" 11 12 def query_gemini_simple(prompt): 13 """Query Gemini using urllib (no external packages needed)""" 14 15 # Try different endpoints 16 endpoints = [ 17 "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent", 18 "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent", 19 "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent" 20 ] 21 22 for endpoint in endpoints: 23 url = f"{endpoint}?key={GEMINI_KEY}" 24 25 data = json.dumps({ 26 "contents": [{ 27 "parts": [{"text": prompt}] 28 }], 29 "generationConfig": { 30 "maxOutputTokens": 200 31 } 32 }).encode('utf-8') 33 34 try: 35 req = urllib.request.Request( 36 url, 37 data=data, 38 headers={'Content-Type': 'application/json'} 39 ) 40 41 with urllib.request.urlopen(req, timeout=10) as response: 42 result = json.loads(response.read().decode('utf-8')) 43 44 # Extract text 45 if 'candidates' in result and result['candidates']: 46 text = result['candidates'][0]['content']['parts'][0]['text'] 47 return f"š¤ Gemini: {text}" 48 49 except Exception as e: 50 continue 51 52 return "ā Could not connect to Gemini API" 53 54 def main(): 55 if len(sys.argv) > 1: 56 question = " ".join(sys.argv[1:]) 57 else: 58 question = input("Ask me anything: ").strip() or "What is AI?" 59 60 print(f"\nš Your question: {question}") 61 print("\nš Getting response...\n") 62 63 response = query_gemini_simple(question) 64 print(response) 65 66 # Also test Groq 67 print("\n\nš Trying Groq API...") 68 try: 69 groq_data = json.dumps({ 70 "model": "llama3-8b-8192", 71 "messages": [{"role": "user", "content": question}], 72 "max_tokens": 100 73 }).encode('utf-8') 74 75 groq_req = urllib.request.Request( 76 "https://api.groq.com/openai/v1/chat/completions", 77 data=groq_data, 78 headers={ 79 'Authorization': 'Bearer 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', 80 'Content-Type': 'application/json' 81 } 82 ) 83 84 with urllib.request.urlopen(groq_req, timeout=10) as response: 85 groq_result = json.loads(response.read().decode('utf-8')) 86 groq_text = groq_result['choices'][0]['message']['content'] 87 print(f"ā” Groq: {groq_text}") 88 89 except Exception as e: 90 print(f"ā Groq error: {str(e)[:100]}") 91 92 if __name__ == "__main__": 93 main()