Jump to content

Script Action with Bash Script randomly stops before echo


Recommended Posts

I am having problem with a script from a workflow I’m creating to interact with OpenAI’s API. I’m not a developer and I’ve used a lot of GPT help for this, but I’m at the point where I’m lost and seems to be hitting the limit of how GPT 4 can help me with this.

 

The idea of the workflow is so that I can be in Bear, my note-taking app (or any markdown note taking app) and that I can start a Chat GPT chat right there, just below a markdown line (---) . Applescript is used to select my text, copy it, extract the actual request text (after the last line), and then it get to this Bash script which does the request and where somewhere in there the problem lies.

 

The reason it’s making me crazy is because it seems to work perfectly at first, but randomly after a few messages (or sometimes on the first one), and without showing anything at all in the debugging console, it just doesn’t echo the assistant response, which a following action should then paste in my frontmost app. The response is also saved to a json file that is used to keep track of context, and it does gets output there every time without issues. It’s just that for some reason the script action doesn’t echoes the variable so that the can workflow continue... seems to just stop running. 

 

Any idea of what could be happening would be very helpful. I thought it had something to do with timeouts but since it is written to the context json file then I don’t think that’s the reason. There’s also been a few cases where the context json file gets some ?? Characters at the end making it unreadable, and I have no idea of the reason for that, and can’t reproduce it yet... but this first issue is keeping me sleepless.

 

I’m on Alfred 5.1.4 [2195], Mac OS 14.2.1 (23C71). 

 

The script is this:

 

#!/bin/bash

# Configuration variables
CONTEXT_MESSAGES_LIMIT=10
SYSTEM_ROLE_MESSAGE="You are a helpful AI assistant. Your answers do not include a description unless prompted to do so."
MODEL="gpt-3.5-turbo-1106"
TEMPERATURE=0.5
MAX_TOKENS=""  # Set to an empty string if not needed: MAX_TOKENS=""

# Load API key
OPENAI_API_KEY="${APIToken}"

# Define the context file path
CONTEXT_FILE="./context.json"

# Read the new user message from the first argument
new_message="${theRequest}"

# Initialize or read existing context from file
if [[ ! -f "$CONTEXT_FILE" ]]; then
    # If context doesn't exist, prepend with system role message
    messages=$(jq -n --arg msg "$SYSTEM_ROLE_MESSAGE" '[{"role":"system","content":$msg}]')
else
    messages=$(cat "$CONTEXT_FILE")
fi

# Append the new message to the context array
messages=$(echo "$messages" | jq --arg msg "$new_message" '. + [{"role":"user","content":$msg}]')

# Ensure that we keep only the last N messages as defined by CONTEXT_MESSAGES_LIMIT
messages=$(echo "$messages" | jq --argjson limit "$CONTEXT_MESSAGES_LIMIT" '.[-$limit:]')

# Formulate the API request payload
payload=$(jq -n \
    --arg model "$MODEL" \
    --argjson messages "$messages" \
    --argjson temperature "$TEMPERATURE" \
    '{model: $model, messages: $messages, temperature: $temperature}')

# If MAX_TOKENS is set, add it to the payload
if [[ -n "$MAX_TOKENS" ]]; then
    payload=$(echo "$payload" | jq --argjson max_tokens "$MAX_TOKENS" '. + {max_tokens: $max_tokens}')
fi

# Make the API request
response=$(curl -s -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d "$payload" \
    https://api.openai.com/v1/chat/completions)

# Extract the assistant's response and append it to the context
assistant_response=$(echo "$response" | jq -r '.choices[0].message.content')
messages=$(echo "$messages" | jq --arg msg "$assistant_response" '. + [{"role":"assistant","content":$msg}]')

# Update the context file with the new messages
echo "$messages" > "$CONTEXT_FILE"

# Output the assistant's response
echo $assistant_response

 

Link to comment

Ok, after lots of testing I've come to the conclusion that if I perform a script action in Alfred, and if one step takes a bit longer to complete, the script will still run everything that follows as long as it's inside the same action block. But it will stop Alfred from going to the next one. The times that my workflow fails is because curl delays a bit. So my only workaround so far is to output the assistant's response to a txt file, and trigger the following Alfred's action block from my workflow like this:

 

RESPONSE_FILE="./lastresponse.txt"

# Write the assistant's response to a separate file
echo "$assistant_response" > "$RESPONSE_FILE"

# Run the next step to read the text
osascript -e 'tell application id "com.runningwithcrayons.Alfred" to run trigger "receiver" in workflow "com.afadingthought.alfred"'

 

So the "receiver" reads the response from the txt file and pastes to my frontmost app. I guess this is a bit slower than doing it all without jumping from one action to the next one with applescript, but it just seems to be a limitation or bug within Alfred.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...