Skip to content

Commit

Permalink
Fixing Systemd ex
Browse files Browse the repository at this point in the history
Added a custom venv to the gptd unit file with download, making the footprint smaller and env management way easier.
Also, getting closer and closer to plug and play that is good enough for adding to repos. 
Client is unchanged from 1.1.0, just a bit cleaner.
  • Loading branch information
cbigger authored Jul 6, 2023
1 parent b2cd3d8 commit a935997
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 9 deletions.
21 changes: 13 additions & 8 deletions gptd_1.1.1/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
#!/bin/bash

INSTALL_DIR="/usr/local/bin"
INSTALL_DIR="/home/$SUDO_USER/.local/share/gpt.d"
SERVICE_FILE="/lib/systemd/system/gptd.service"
CONFIGURATION_DIR="/etc/gpt.d"


#sudo -u $SUDO_USER bash << EOF

# Create the installation directories
mkdir -p $INSTALL_DIR
mkdir -p $CONFIGURATION_DIR

# Copy the Python files to the installation directory

cp -r usr/local/lib/gpt.d/gptd-uds.py $INSTALL_DIR
chown $SUDO_USER:$SUDO_USER $INSTALL_DIR/gptd-uds.py

cp -r usr/local/lib/gpt.d/requirements.txt $INSTALL_DIR
chmod +x $INSTALL_DIR/gptd-uds.py
python3 -m venv $INSTALL_DIR/gptdservice
# Install the Python requirements
pip install -r $INSTALL_DIR/requirements.txt
$INSTALL_DIR/gptdservice/bin/python -m pip install -r $INSTALL_DIR/requirements.txt
$INSTALL_DIR/gptdservice/bin/python -m pip install systemd-python

#EOF


# Create the configuration file
cat << EOF > $CONFIGURATION_DIR/.env
# CONF
CHAT_MODEL="gpt-3.5-turbo"
# Security
OPENAI_API_KEY=<api key here>
# Flavour
AI_TEXT_COLOUR='\033[34m\033[1m'
AI_NAME_COLOUR='\033[34m\033[1m\033[47m'
Expand All @@ -36,10 +41,10 @@ cat << EOF > $SERVICE_FILE
Description=gptd
[Service]
ExecStart=/gptd-uds.py
ExecStart=$INSTALL_DIR/gptdservice/bin/python3 $INSTALL_DIR/gptd-uds.py
User=$SUDO_USER
Group=$(id -gn $SUDO_USER)
EnvironmentFile=/etc/gpt.d/.env
#Environment=PYTHONPATH=$INSTALL_DIR/gptdservice/bin/python
Restart=no
[Install]
Expand Down
2 changes: 1 addition & 1 deletion gptd_1.1.1/usr/local/lib/gpt.d/gptd-uds.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import codecs
import traceback
from systemd import journal
#from systemd.journal import JournalHandler

logger = logging.getLogger(__name__)
logger.addHandler(journal.JournalHandler(SYSLOG_IDENTIFIER='gptd-uds'))

config_file = "/etc/gpt.d/.env"
if not os.path.exists(config_file):
logger.error("Configuration file not found. Please update your configuration in /etc/gpt.d/gptd.conf")
print(f'Config not found at {config_file}')
exit()

load_dotenv(dotenv_path=config_file)
Expand Down
Empty file.
77 changes: 77 additions & 0 deletions gptd_1.1.1/usr/local/lib/gpt.d/gptd_client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import socket
import os
import threading
import argparse
import sys

# Path to the Unix domain socket
socket_file_path = "/tmp/gptd.sock"

def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Flags and arguments for handling connections to gptd')
parser.add_argument('-c', '--closed', help='Start a closed context session')
parser.add_argument('-q', '--quick', action='store_true', help='Close connection immediately. Use with -s for piping, or for testing server availability')
parser.add_argument('-s', '--string', help='Send a string on connecting to server')
args = parser.parse_args()

# Ensure that the socket file exists
if not os.path.exists(socket_file_path):
print("The server is not running.")
exit(1)

# Create a socket object
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

def send_data():
if args.closed:
sock.send("#personal".encode('utf-8'))

if args.string:
# If a string was passed as a command line argument, send it to the server
sock.sendall(args.string.encode('utf-8'))

if args.quick:
sock.shutdown(socket.SHUT_WR)


else:
while True:
# Get input from the user
message = input()

# Send the message to the server
sock.sendall(message.encode('utf-8'))

def receive_data():
while True:
# Receive the response from the server
data = sock.recv(16)
if data:
print(f"{data.decode('utf-8')}", end='', flush=True)
else:
break
sys.exit(0) # exit after receiving response

try:
# Connect the socket
sock.connect(socket_file_path)

# Start threads for sending and receiving data
send_thread = threading.Thread(target=send_data)
receive_thread = threading.Thread(target=receive_data)
send_thread.start()
receive_thread.start()

# Wait for both threads to finish
send_thread.join()
receive_thread.join()

finally:
# Close the socket when we're done with it
print("Closing client...")
sock.close()

if __name__ == "__main__":
main()

35 changes: 35 additions & 0 deletions gptd_1.1.1/usr/local/lib/gpt.d/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
aiohttp==3.8.4
aiosignal==1.3.1
annotated-types==0.5.0
anyio==3.7.0
async-timeout==4.0.2
attrs==23.1.0
certifi==2023.5.7
charset-normalizer==3.1.0
exceptiongroup==1.1.1
fire==0.5.0
frozenlist==1.3.3
h11==0.14.0
httpcore==0.17.2
httpx==0.24.1
idna==3.4
markdown-it-py==3.0.0
mdurl==0.1.2
multidict==6.0.4
openai==0.27.8
orjson==3.9.1
pydantic==1.10.7
pydantic-core==2.0.1
Pygments==2.15.1
python-dateutil==2.8.2
python-dotenv==1.0.0
requests==2.31.0
rich==13.4.2
six==1.16.0
sniffio==1.3.0
systemd-python==235
termcolor==2.3.0
tqdm==4.65.0
typing-extensions==4.7.0
urllib3==2.0.3
yarl==1.9.2
12 changes: 12 additions & 0 deletions gptd_1.1.1/usr/local/lib/gpt.d/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from setuptools import setup, find_packages

setup(
name='gptd-client',
version='0.3',
packages=find_packages(),
entry_points={
'console_scripts': [
'gptd-client=gptd_client.client:main',
],
},
)

0 comments on commit a935997

Please sign in to comment.