Skip to content

Instantly share code, notes, and snippets.

@jart
Created July 5, 2024 13:29
Show Gist options
  • Save jart/2f3960cfde7df07d951f9e9d6947d5ab to your computer and use it in GitHub Desktop.
Save jart/2f3960cfde7df07d951f9e9d6947d5ab to your computer and use it in GitHub Desktop.
Using an LLM as an HTTP proxy with LLaMAfile
#!/usr/bin/env python
import socket
import threading
import subprocess
def handle_client(client_socket):
request = client_socket.recv(8192).decode('utf-8', errors='ignore')
first_line = request.split('\n')[0]
if (((not first_line.startswith('GET') and
not first_line.startswith('POST')) or
' ' not in first_line)):
client_socket.close()
return
url = first_line.split(' ')[1].lower()
# Ignore images and telemetry
if (('unsplash.com' in url or
'detectportal' in url or
url.endswith('.js') or
url.endswith('.ico') or
url.endswith('.png') or
url.endswith('.jpg') or
url.endswith('.jpeg') or
url.endswith('.gif'))):
client_socket.close()
return
print("Received:", first_line)
# Response message with custom content
# Designed for TinyLLaMA and Mistral so far
prompt = f"""\
<|system|>
You are an imaginative HTTP proxy that generates a fully synthetic world wide web on demand.
Today's date is 2006-04-19.
## Guidelines
- Use a serious tone. Never reveal that it's all imaginary.
- Every HTML page should contain hyperlinks to other pages.
- All content must be original writing (no "lorem ipsum")
- Pages should be filled with links to other pages.
- Each page should have multiple sections.
- Longer web pages are better.
- Be imaginative.
## Coding
- CSS must be embedded in the HTML.
- <form> must have method="get"
- <img> must have alt="..."
- Do not use JavaScript.
</s>
<|user|>
GET {url}
</s>
<|assistant|>
```html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
"""
client_socket.send(b"HTTP/1.0 200 OK\r\n"
b"Content-Type: text/html\r\n"
b"\r\n"
b"<!doctype html>\r\n"
b"<html lang=\"en\">\r\n"
b"<meta charset=\"utf-8\">\r\n")
proc = subprocess.Popen(["ape",
"/home/jart/Mistral-7B-Instruct-v0.3.F16.llamafile",
"--temp", "0", "-ngl", "999",
"--no-display-prompt",
"--log-disable",
"-p", prompt,
"-r", "```\n",
"-c", "0"],
stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if line == b'':
break
line = line.replace(b'https', b'http')
line = line.replace(b'#', b'')
print("Relaying:", line.decode('utf-8', errors='ignore').rstrip())
client_socket.send(line)
client_socket.close()
def run_proxy_server(port):
"""
Start a simple HTTP proxy server that listens on a specified port.
"""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', port))
server_socket.listen(5)
print("Proxy server listening on port", port)
try:
while True:
client_socket, addr = server_socket.accept()
print('Connected by', addr)
try:
handle_client(client_socket)
except BrokenPipeError:
pass
# threading.Thread(target=handle_client, args=(client_socket,)).start()
except KeyboardInterrupt:
print("Shutting down the server.")
server_socket.close()
# Start the proxy server on port 8080
if __name__ == '__main__':
run_proxy_server(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment