Help Bash script assistant

Hi everyone
This is my first post on this forum and I hope you have some desire to help me understand how to solve a problem that is taking up 20 hours of my time :frowning:

I’m trying to run a bash script that retrieves the command and sends it to an already configured and working assistant on the openai site (assistant).

This is the script code that takes the STDIN and, once finished, passes it as STDOUT to another service that will read it:

#!/usr/bin/env bash

# Legge l'input testuale da STDIN
read -r text

# Chiave API di OpenAI
OPENAI_API_KEY="sk-XXXXXXXXXXXXXXXXXXXXXXXX"
ASSISTANT_ID="asst_jxLhXXXXXXXXXtVmRLH"

# Crea un Thread
RESPONSEA=$(curl https://api.openai.com/v1/threads \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $OPENAI_API_KEY" \
 -H "OpenAI-Beta: assistants=v1" \
 -d ''
)
THREAD_ID=$(echo "$RESPONSEA" | jq -r '.id')

# Aggiungi un Messaggio al Thread
RESPONSEAA=$(curl https://api.openai.com/v1/threads/$THREAD_ID/messages \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $OPENAI_API_KEY" \
 -H "OpenAI-Beta: assistants=v1" \
 -d '{
      "role": "user",
      "content": "'"$text"'"
 }')

RESPONSEB=$(curl -X POST "https://api.openai.com/v1/threads/$THREAD_ID/runs" \
 -H "Content-Type: application/json" \
 -H "Authorization: Bearer $OPENAI_API_KEY" \
 -H "OpenAI-Beta: assistants=v1" \
 -d '{
    "assistant_id": "'"$ASSISTANT_ID"'",
    "instructions": " "
}')

  
RUNID=$(echo "$RESPONSEB" | jq -r '.id')

# Attendi che il Run sia completato con un timeout
RESPONSEC=$(curl https://api.openai.com/v1/threads/$THREAD_ID/runs/$RUNID \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1"
)


# Recupera la risposta dell'Assistant
RESPONSED=$(curl https://api.openai.com/v1/threads/$THREAD_ID/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v1"
) 

GENERATED_SCRIPT=$(echo "$RESPONSED" | jq -r '.data[] | select(.role == "assistant") | .content[0].text.value')

# DEBUG

echo "______A________"
echo $RESPONSEA

echo "______AA________"
echo $RESPONSEAA



echo "_____B___________"
echo $RESPONSEB


echo "_____C___________"
echo $RESPONSEC


echo "______D__________"
echo $RESPONSED




echo $GENERATED_SCRIPT


I followed this guide: https://platform.openai.com/docs/assistants/overview

Problems:

  1. the first time I run it it brings me a response. The subsequent ones, no.
  2. It doesn’t use the assistant I already configured.

Can you help me?
Yes, I know, it seems like a too ambitious project for me, but I’m stubborn :slight_smile:
Thank you!

So, I’ve solved it, and it works. But OpenAI’s APIs are incredibly slow. I’ve created this script that functions as intent recognition on Rhasspy 2.5, and to overcome the slowness of the responses, I’ve added the ability to save repetitive ones, such as "turn on, turn off, what’s the weather tonight in “city”. On Assistant, I’ve placed the intents that were already handled by Rhasspy locally, and I’ve created two more to handle unmanageable questions and generic ones (who was so-and-so, give me the recipe for Greek salad). However, I need to address the issue of response slowness: it takes at least 5 seconds to process the information.