Browse Source

updated chat script

control 1 week ago
parent
commit
231b4be91a
1 changed files with 72 additions and 10 deletions
  1. 72 10
      scripts/chat

+ 72 - 10
scripts/chat

@@ -1,17 +1,79 @@
-#!/usr/bin/python3
+#!/usr/bin/env python3
 
 import os
 import sys
+import json
+import signal
+import hashlib
+import requests
 
-URL = 'https://ch.at'
+from pathlib import Path
 
-# Usage & argument check
-if len(sys.argv) <= 1:
-    print(f'Usage: chat <query (STR): your question>')
-    sys.exit()
+# Configuration
+PROXY_HOST = "chat.reesec.net"
+PROXY_PORT = 443
 
-user_input = sys.argv[1:]
-user_query = '+'.join(user_input)
-query_url = f'{URL}/?q={user_query}'
+MODEL = "mistral:7b"
+#MODEL = "deepseek-r1:8b"
+#MODEL = "gemma3:4b"
 
-os.system(f'curl "{query_url}"')
+def signal_handler(sig, frame):
+    print()  # Newline for clean exit
+    sys.exit(0)
+
+signal.signal(signal.SIGINT, signal_handler)
+
+def chat(host, port, api_key, model, question):
+    """Send a question to Ollama proxy and stream the response."""
+    url = f"https://{host}:{port}/api/generate"
+    
+    payload = {
+        "model": model,
+        "prompt": question,
+        "stream": True
+    }
+    
+    headers = {
+        "X-API-Key": api_key
+    }
+    
+    try:
+        response = requests.post(url, json=payload, stream=True, headers=headers)
+        response.raise_for_status()
+        
+        # Stream and print the response
+        for line in response.iter_lines():
+            if line:
+                data = json.loads(line)
+                if "response" in data:
+                    print(data["response"], end="", flush=True)
+        
+        print()  # Newline at the end
+        
+    except requests.exceptions.HTTPError as e:
+        if e.response.status_code == 401:
+            print("Error: Invalid API key", file=sys.stderr)
+        else:
+            print(f"Error: {e}", file=sys.stderr)
+        sys.exit(1)
+    except requests.exceptions.ConnectionError as e:
+        print(f"Error: Could not connect to proxy at {host}:{port}\n{e}", file=sys.stderr)
+        sys.exit(1)
+    except requests.exceptions.RequestException as e:
+        print(f"Error: {e}", file=sys.stderr)
+        sys.exit(1)
+
+if __name__ == "__main__":
+    home_path = Path.home() 
+    with open(f'{home_path}/.ssh/chat.key', 'r') as f:
+        API_KEY = str(f.read().strip())
+
+    # Get question from command line arguments
+    if len(sys.argv) < 2:
+        print("Usage: python chat.py <question>", file=sys.stderr)
+        print("Example: python chat.py 'What is Python?'", file=sys.stderr)
+        sys.exit(1)
+    
+    question = " ".join(sys.argv[1:])
+    
+    chat(PROXY_HOST, PROXY_PORT, API_KEY, MODEL, question)