· last year · Jul 11, 2024, 10:05 AM
1#!/bin/bash
2#version=1.30.4
3#
4# Chatgpt in the terminal.
5# commands get parsed into clipboard.. I'm lazy...
6#
7# no other dependencies.. just a lack of judgement
8# in putting an api key in a script. Cap your spending at a few bucks
9# this is meant to be brief, for just getting command help etc so
10# you barely use a few cents a week, depending on use.
11#
12# $ gpt how do I get the installed kernel version
13# $ gpt: ###uname -r###
14# $ copied to clip: uname -r
15# $ you:
16#
17# Oh, I use a nerdfont, so there are symbols in it. If you don't like them
18# change them out (58 and 75, aren't I nice)
19# Those lines might move in the future, I might not update the line numbers.
20# I might not.
21# I have faith in you.
22#
23#
24#
25
26
27# Replace with your own OpenAI playground API key
28API_KEY="sk-rikA-n3v3r60nn461v3y0uuPn3v3R60nn4L27yu060"
29
30# The endpoint for the ChatGPT API
31URL="https://api.openai.com/v1/chat/completions"
32
33# Initialize an empty array to store the conversation messages
34messages=()
35
36# Add a system message to instruct the assistant to format commands with ###
37messages+=('{"role": "system", "content": "You are a helpful assistant. I use Arch based Linux. Be very brief. If asked for a command give only that as the reply enclosed inside ###"}')
38
39# Log file path
40LOG_FILE="$HOME/gpt.log"
41
42# Function to send a message to ChatGPT and receive a response
43chat_with_gpt() {
44 local input="$1"
45 messages+=('{"role": "user", "content": "'"${input//\"/\\\"}"'"}')
46
47 local payload="{\"model\": \"gpt-4\", \"messages\": [$(IFS=,; echo "${messages[*]}")], \"temperature\": 0.7}"
48
49 response=$(curl -s -X POST "$URL" -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d "$payload")
50
51 # Extract the content from the response
52 content=$(echo "$response" | jq -r '.choices[0].message.content')
53
54 # Append assistant's response to the messages array
55 messages+=('{"role": "assistant", "content": "'"${content//\"/\\\"}"'"}')
56
57 # Print the assistant's response
58 echo -e " gpt $content"
59
60 # Highlight text enclosed in ###
61 highlighted=$(echo "$content" | grep -oP '(?<=###).*?(?=###)')
62 if [ -n "$highlighted" ]; then
63 echo -e "\033[1;33mcopied to clip: $highlighted\033[0m"
64 echo -n "$highlighted" | xclip -selection primary
65 fi
66
67 # Log the conversation
68 echo -e "User: $input\nGPT: $content\n" >> "$LOG_FILE"
69}
70
71# Function to start the conversation loop
72start_conversation() {
73 while true; do
74 # Prompt the user for input
75 read -p " you " question
76
77 # Exit if the user presses enter with no text
78 if [ -z "$question" ]; then
79 #echo "Goodbye!"
80 exit 0
81 fi
82
83 # Send the input to ChatGPT
84 chat_with_gpt "$question"
85 done
86}
87
88# Check if a question is provided as an argument
89if [ $# -gt 0 ]; then
90 initial_question="$*"
91 chat_with_gpt "$initial_question"
92fi
93
94# Start the conversation
95start_conversation
96