| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #!/usr/bin/env python3
- import os
- import sys
- import json
- import signal
- import hashlib
- import requests
- from pathlib import Path
- # Configuration
- PROXY_HOST = "chat.reesec.net"
- PROXY_PORT = 443
- MODEL = "mistral:7b"
- #MODEL = "deepseek-r1:8b"
- #MODEL = "gemma3:4b"
- 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)
|