plz-event-source

Next:   [Contents]

plz-event-source

plz-event-source provides a plz-media-type, a parser and an event source implementation for the Server Sent Event (SSE) protocol. It is used in the LLM library to handle server sent events.

Table of Contents


Next: , Previous: , Up: plz-event-source   [Contents]

1 Installation


1.1 GNU ELPA

plz-event-source is available in GNU ELPA. It may be installed in Emacs using the package-install command.


Next: , Previous: , Up: plz-event-source   [Contents]

2 Usage

This library provides the plz-event-source:text/event-stream class, which is a media type implementation for server sent events. It should be used with the plz-media-type-request function of the plz-media-type library.


Up: Usage   [Contents]

2.1 Examples

The following example shows how to request chat completions from a large language model.

(plz-media-type-request
  'post "https://api.openai.com/v1/chat/completions"
  :as `(media-types ((text/event-stream
                      . ,(plz-event-source:text/event-stream
                          :events `((open . (lambda (event)
                                              (message "open: %s" event)))
                                    (message . (lambda (event)
                                                 (message "message: %s" event)))
                                    (close . (lambda (event)
                                               (message "close: %s" event))))))))
  :body (json-encode
         '(("model" . "gpt-3.5-turbo")
           ("messages" . [(("role" . "system")
                           ("content" . "You are an assistant."))
                          (("role" . "user")
                           ("content" . "Hello"))])
           ("stream" . t)))
  :headers `(("Authorization" . ,(format "Bearer %s"
                                         (auth-source-pick-first-password
                                          :host "api.openai.com"
                                          :user "plz-event-source")))
             ("Content-Type" . "application/json")))

Next: , Previous: , Up: plz-event-source   [Contents]

3 Credits


Previous: , Up: plz-event-source   [Contents]

4 License

GPLv3