reactjs - LLM Endpoint hosted in Azure databricks Response 200 but body is locked - Stack Overflow

时间: 2025-01-06 admin 业界

Im having this issue from a POC I created with ReactJS and Flask and hosted in azure, I was given an LLM endpoint that is hosted in azure databricks. The endpoint works fine when I try it at postman but when Im trying to call it in my app the response is 200 but the body is locked. I've never encountered this issue in my previous project and I cant see if there is any permission or restrictions options to see the body in azure. Below is a snippet of my code and the endpoint response.

I've omitted the endpoint paths but im sure that it is correct

#Flask app.py
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)

CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}})

@app.route("my-endpoint", methods=["POST"])
def handle_invocations(endpoint_name):
    try:
        payload = request.json
        base_url = "https:{my-url}.azuredatabricks"
    
        target_url = f"{my-target URL}"
        

        headers = {
        "Content-Type": "application/json",
        "Authorization": "{my token}"
        }
    
        response = requests.post(target_url, json=payload, headers=headers)
    
        return jsonify(response.json()), response.status_code
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(debug=True)

# Reactjs Homepage.jsx
const onCardClick = async (question) => {
    const payload = {
        messages: [
            {
                role: "user",
                content: String(question),
            },
        ],
    };

    try {
        const response = await fetch({my-endpoint}, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Authorization": {my-token}
            },
            
            body: JSON.stringify(payload),
        });

        if (!response.ok) {
          let errorDetails = null;
  
          
          try {
              errorDetails = await response.json();
          } catch (jsonError) {
              console.error("Failed to parse error response as JSON.");
          }
  
          console.error("HTTP Error:", {
              status: response.status,
              statusText: response.statusText,
              details: errorDetails
          });
  
          throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`);
      }

        const result = await response.json();

        
        setTextFieldContent(result.content || "No response content available.");
    } catch (error) {
        console.error("Error:", error);
        setTextFieldContent("An error occurred while fetching the response.");
    }
};

Im having this issue from a POC I created with ReactJS and Flask and hosted in azure, I was given an LLM endpoint that is hosted in azure databricks. The endpoint works fine when I try it at postman but when Im trying to call it in my app the response is 200 but the body is locked. I've never encountered this issue in my previous project and I cant see if there is any permission or restrictions options to see the body in azure. Below is a snippet of my code and the endpoint response.

I've omitted the endpoint paths but im sure that it is correct

#Flask app.py
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)

CORS(app, resources={r"/api/*": {"origins": "http://localhost:5173"}})

@app.route("my-endpoint", methods=["POST"])
def handle_invocations(endpoint_name):
    try:
        payload = request.json
        base_url = "https:{my-url}.azuredatabricks.net"
    
        target_url = f"{my-target URL}"
        

        headers = {
        "Content-Type": "application/json",
        "Authorization": "{my token}"
        }
    
        response = requests.post(target_url, json=payload, headers=headers)
    
        return jsonify(response.json()), response.status_code
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(debug=True)

# Reactjs Homepage.jsx
const onCardClick = async (question) => {
    const payload = {
        messages: [
            {
                role: "user",
                content: String(question),
            },
        ],
    };

    try {
        const response = await fetch({my-endpoint}, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Authorization": {my-token}
            },
            
            body: JSON.stringify(payload),
        });

        if (!response.ok) {
          let errorDetails = null;
  
          
          try {
              errorDetails = await response.json();
          } catch (jsonError) {
              console.error("Failed to parse error response as JSON.");
          }
  
          console.error("HTTP Error:", {
              status: response.status,
              statusText: response.statusText,
              details: errorDetails
          });
  
          throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`);
      }

        const result = await response.json();

        
        setTextFieldContent(result.content || "No response content available.");
    } catch (error) {
        console.error("Error:", error);
        setTextFieldContent("An error occurred while fetching the response.");
    }
};

Share Improve this question asked Dec 16, 2024 at 2:38 maximmaxim 231 silver badge5 bronze badges 2
  • Did you read it as a stream – JayashankarGS Commented Dec 16, 2024 at 3:09
  • Does below solution helped you? @maxim – JayashankarGS Commented Dec 17, 2024 at 4:36
Add a comment  | 

1 Answer 1

Reset to default 0

The response what you are getting is correct and valid, access the content like below.

const  content = result.choices[0].message.content || "No response content available.";

The response json data from endpoint will be something like below based on the arguments and body you pass, check the endpoint or LLM model documentation whichever you are using.

{
  "id": "chatcmpl_cec8b3cd-856d-417b-a629-907e15a55762",
  "object": "chat.completion",
  "created": 1734327739,
  "model": "dbrx-instruct-071224",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Databricks is a data and analytics company that provides a unified analytics platform for data science teams to collaborate on large-scale data engineering, data science, and machine learning workloads. The company was founded by the original creators of Apache Spark, Delta Lake, and MLflow. Databricks provides a managed platform for these open-source technologies, as well as additional proprietary features, to help organizations simplify and scale their data-driven initiatives. The platform enables data teams to process and analyze large volumes of data, build and deploy machine learning models, and share insights across the organization."
      },
      "finish_reason": "stop",
      "logprobs": null
    }
  ],
  "usage": {
    "prompt_tokens": 251,
    "completion_tokens": 115,
    "total_tokens": 366
  }
}

Next, the body is locked because, read only once.

I got the below error when i tried.

code snippet

console.log(response)
const bd = await response.body.getReader().read();
console.log(bd)
const result = await response.json();
const content = result.choices[0].message.content || "No response content available.";

Error

TypeError: Failed to execute 'json' on 'Response': body stream already read

you can see in below screenshot, the values fetched for reading body using reader object and failed for .json() function.

Only once you can read for locked body, so read it as json and store in the variable.