admin管理员组文章数量:1336632
Environment: web browser, javascript.
I want to use post method to call sse api (Server-Send Events) like
curl \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"prompt": "Say this is a test",
"max_tokens": 7,
"steam": true,
"temperature": 0
}'
I did some research online, and found I couldn't use EventSource , because it doesn't support send post data.
So I write the code like this in the browser try to read text in steam.
I don't know if that's a good idea:
const response = await axios({
method: 'post',
url: '/my-service/chat',
data: data,
headers: {
'SESSION_ID': 'Header-Value',
"Content-Type": "application/json",
"Accept": "text/event-stream",
}
, responseType: 'steam',
});
//TODO: read steam and convert to text
I appreciate any help you could provide. Thank you in advance!
Environment: web browser, javascript.
I want to use post method to call sse api (Server-Send Events) like
https://platform.openai./docs/api-reference/pletions/create
curl https://api.openai./v1/pletions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"prompt": "Say this is a test",
"max_tokens": 7,
"steam": true,
"temperature": 0
}'
I did some research online, and found I couldn't use EventSource , because it doesn't support send post data.
So I write the code like this in the browser try to read text in steam.
I don't know if that's a good idea:
const response = await axios({
method: 'post',
url: '/my-service/chat',
data: data,
headers: {
'SESSION_ID': 'Header-Value',
"Content-Type": "application/json",
"Accept": "text/event-stream",
}
, responseType: 'steam',
});
//TODO: read steam and convert to text
I appreciate any help you could provide. Thank you in advance!
Share Improve this question asked May 25, 2023 at 17:00 zy_sunzy_sun 4531 gold badge6 silver badges18 bronze badges 2- I doubt this is possible. – Evert Commented May 25, 2023 at 17:19
- See github./shafkevi/lambda-bedrock-s3-streaming-rag/issues/… - SSE events can at least be streamed using fetch, not sure about axios but maybe fetch is a possible alternative? – Motin Commented Apr 3, 2024 at 5:20
1 Answer
Reset to default 5My solution chose the fetch-event-source
framework.
https://github./Azure/fetch-event-source
Here are some other alternatives:
JavaScript+axios:
axios({
url: 'https://api',
data: {
prompt: 'json data'
},
headers: {
'accept': '*',
'content-type': 'application/json'
},
method: 'POST',
onDownloadProgress: progressEvent => {
const xhr = progressEvent.event.target
const { responseText } = xhr
console.log("=====responseText======")
console.log(responseText)
}
}).then(({ data }) => Promise.resolve(data));
Server+Java code:
spring-server-sent-events: https://www.baeldung./spring-server-sent-events
public void consumeServerSentEvent() {
WebClient client = WebClient.create("http://localhost:8080/sse-server");
ParameterizedTypeReference<ServerSentEvent<String>> type
= new ParameterizedTypeReference<ServerSentEvent<String>>() {};
Flux<ServerSentEvent<String>> eventStream = client.get()
.uri("/stream-sse")
.retrieve()
.bodyToFlux(type);
eventStream.subscribe(
content -> logger.info("Time: {} - event: name[{}], id [{}], content[{}] ",
LocalTime.now(), content.event(), content.id(), content.data()),
error -> logger.error("Error receiving SSE: {}", error),
() -> logger.info("Completed!!!"));
}
本文标签: javascripthow get response in axios when api is steam like SSE (ServerSend Events)Stack Overflow
版权声明:本文标题:javascript - how get response in axios when api is steam like SSE (Server-Send Events) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243139a2438958.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论