From c6e5f14433832c3aa3b023104560e65b3f5f292b Mon Sep 17 00:00:00 2001 From: michael1986 Date: Tue, 2 Dec 2025 11:21:58 +0100 Subject: [PATCH] Stream response --- finish.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/finish.sh b/finish.sh index df5bcf4..87a9650 100644 --- a/finish.sh +++ b/finish.sh @@ -224,7 +224,7 @@ $prompt" "LMSTUDIO") payload=$(echo "$base_payload" | jq '. + { max_tokens: -1, - stream: false + stream: true }') ;; *) @@ -281,16 +281,58 @@ 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 [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then - response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" --data "$payload") + 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 + response_body="{\"choices\":[{\"message\":{\"content\":\"$stream_content\"}}]}" + rm -f "$temp_file" else - response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \ - -H "Content-Type: application/json" \ - -d "$payload") + # Non-streaming mode (original behavior) + if [[ "${ACSH_PROVIDER^^}" == "OLLAMA" ]]; then + response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" --data "$payload") + else + response=$(\curl -s -m "$timeout" -w "\n%{http_code}" "$endpoint" \ + -H "Content-Type: application/json" \ + -d "$payload") + fi + status_code=$(echo "$response" | tail -n1) + response_body=$(echo "$response" | sed '$d') fi - status_code=$(echo "$response" | tail -n1) - response_body=$(echo "$response" | sed '$d') # Debug logging echo "Response status: $status_code" >> "$debug_log"