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 ![]()
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:
- the first time I run it it brings me a response. The subsequent ones, no.
- 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 ![]()
Thank you!