reactjs - LLM Endpoint hosted in Azure databricks Response 200 but body is locked - Stack Overflow
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
1 Answer
Reset to default 0The 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.
- 微信支付和支付宝口水战开打!
- 《福布斯》:缺乏硬软件支持 NFC已成鸡肋
- 惠普CEO惠特曼访华:我们不走IBM之路
- python - DeltaTable map type - Stack Overflow
- How to solve the error in "Plugin [id: 'org.jetbrains.kotlin.android', version: '1.9.0'
- typescript - Command 'pod install' failed, Cause: binbash -c - Stack Overflow
- c++ - GLOG - flags.cc' is being linked both statically and dynamically into this executable - Stack Overflow
- wpf - Installation of an upgrade version starting at ResumeDlg - Stack Overflow
- rcpp - C++ AVX2 custom functions (e.g., "exp") not working on Windows (but work on Linux) - Stack Overflow
- shell - how to change my CONDA_DEFAULT_ENV in Linux? - Stack Overflow
- c# - While Downloading the Excel file from a location it is not able to download, showing check internet connection - Stack Over
- spring boot - Hibernate search backend elastic search with multiple data bases - Stack Overflow
- javascript - filter out object in array if object key value is null - Stack Overflow
- amazon web services - Python boto3: download files from s3 to local only if there are differences between s3 files and local one
- flutter - How to write LOGs in an APP made in fletpython, which appear in the DDMS? - Stack Overflow
- window - Strawberry Perl doesn't include Win32::shortcut? - Stack Overflow
- c - Is there a systemd-independent function for getting a USB device model? - Stack Overflow