Compare commits

2 Commits
1.0 ... main

Author SHA1 Message Date
ac343050a8 typo fix
Some checks failed
Docker Build and Push / build-and-push (push) Failing after 12s
Tests / test (bash) (push) Failing after 17s
Tests / test (zsh) (push) Failing after 16s
Tests / lint (push) Successful in 9s
Tests / docker (push) Successful in 7s
2025-12-02 11:40:38 +01:00
c6e5f14433 Stream response
Some checks failed
Docker Build and Push / build-and-push (push) Failing after 12s
Tests / test (bash) (push) Failing after 17s
Tests / test (zsh) (push) Failing after 16s
Tests / lint (push) Successful in 9s
Tests / docker (push) Successful in 7s
2025-12-02 11:21:58 +01:00

View File

@@ -224,7 +224,7 @@ $prompt"
"LMSTUDIO")
payload=$(echo "$base_payload" | jq '. + {
max_tokens: -1,
stream: false
stream: true
}')
;;
*)
@@ -281,7 +281,49 @@ openai_completion() {
max_attempts=2
attempt=1
local stream_enabled=false
# Check if streaming is enabled in payload
if echo "$payload" | jq -e '.stream == true' > /dev/null 2>&1; then
stream_enabled=true
fi
while [ $attempt -le $max_attempts ]; do
if [[ "$stream_enabled" == true ]]; then
# Streaming mode - collect chunks and display in real-time
local temp_file=$(mktemp)
local stream_content=""
if [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then
\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" --data "$payload" -N > "$temp_file"
else
\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \
-H "Content-Type: application/json" \
-d "$payload" -N > "$temp_file"
fi
status_code=$(tail -n1 "$temp_file")
response_body=$(sed '$d' "$temp_file")
# Parse SSE stream and extract content
while IFS= read -r line; do
if [[ "$line" =~ ^data:\ (.+)$ ]]; then
chunk_data="${BASH_REMATCH[1]}"
if [[ "$chunk_data" != "[DONE]" ]]; then
chunk_content=$(echo "$chunk_data" | jq -r '.choices[0].delta.content // empty' 2>/dev/null)
if [[ -n "$chunk_content" && "$chunk_content" != "null" ]]; then
stream_content+="$chunk_content"
fi
fi
fi
done < <(echo "$response_body")
# Store accumulated content as response_body for later processing
# Use jq to properly escape the content string
response_body=$(jq -n --arg content "$stream_content" '{choices: [{message: {content: $content}}]}')
rm -f "$temp_file"
else
# Non-streaming mode (original behavior)
if [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then
response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" --data "$payload")
else
@@ -291,6 +333,7 @@ openai_completion() {
fi
status_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')
fi
# Debug logging
echo "Response status: $status_code" >> "$debug_log"