Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ac343050a8 | |||
| c6e5f14433 |
59
finish.sh
59
finish.sh
@@ -224,7 +224,7 @@ $prompt"
|
||||
"LMSTUDIO")
|
||||
payload=$(echo "$base_payload" | jq '. + {
|
||||
max_tokens: -1,
|
||||
stream: false
|
||||
stream: true
|
||||
}')
|
||||
;;
|
||||
*)
|
||||
@@ -281,16 +281,59 @@ 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
|
||||
# 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
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user