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
Add a ment  | 

1 Answer 1

Reset to default 5

My 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