{"id":17188263,"url":"https://github.com/jclem/sseparser","last_synced_at":"2025-04-13T19:14:15.559Z","repository":{"id":190466412,"uuid":"682690088","full_name":"jclem/sseparser","owner":"jclem","description":"Server-sent events parser for Go","archived":false,"fork":false,"pushed_at":"2025-02-04T21:08:15.000Z","size":49,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T17:31:12.674Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jclem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-24T18:03:03.000Z","updated_at":"2025-02-04T21:08:03.000Z","dependencies_parsed_at":"2024-01-19T23:30:35.893Z","dependency_job_id":"cc1146b6-5bcc-4d13-9bfe-a96a79abf2bf","html_url":"https://github.com/jclem/sseparser","commit_stats":null,"previous_names":["jclem/sseparser"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsseparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsseparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsseparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Fsseparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jclem","download_url":"https://codeload.github.com/jclem/sseparser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248766751,"owners_count":21158301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T01:08:32.914Z","updated_at":"2025-04-13T19:14:15.536Z","avatar_url":"https://github.com/jclem.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SSE Parser\n\nThis package provides functionality for parsing server-sent event streams,\ndefined by [the SSE\nspecification](https://html.spec.whatwg.org/multipage/server-sent-events.html).\n\n## Usage\n\nIn this example, we make a streaming chat completion request to the OpenAI\nplatform API, and scan for response chunks:\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\n\n\t\"github.com/jclem/sseparser\"\n)\n\nfunc main() {\n\topenaiKey := os.Getenv(\"OPENAI_KEY\")\n\n\tif openaiKey == \"\" {\n\t\tlog.Fatal(\"OPENAI_KEY environment variable must be set\")\n\t}\n\n\tparams := map[string]interface{}{\n\t\t\"model\":  \"gpt-3.5-turbo\",\n\t\t\"stream\": true,\n\t\t\"messages\": []map[string]string{\n\t\t\t{\n\t\t\t\t\"role\":    \"user\",\n\t\t\t\t\"content\": \"Hello, how are you?\",\n\t\t\t},\n\t\t},\n\t}\n\n\tbody, err := json.Marshal(params)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\treq, err := http.NewRequest(\"POST\", \"https://api.openai.com/v1/chat/completions\", bytes.NewReader(body))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\treq.Header.Set(\"Authorization\", \"Bearer \"+openaiKey)\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tresp, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\tif resp.StatusCode != 200 {\n\t\tlog.Fatal(resp.Status)\n\t}\n\n\tdefer resp.Body.Close()\n\n\t// We create a new stream scanner that reads the HTTP response body.\n\tscanner := sseparser.NewStreamScanner(resp.Body)\n\n\tfor {\n\t\t// Then, we call `UnmarshalNext`, and log each completion chunk, until we\n\t\t// encounter an error or reach the end of the stream.\n\t\tvar e event\n\t\t_, err := scanner.UnmarshalNext(\u0026e)\n\t\tif err != nil {\n\t\t\tif errors.Is(err, sseparser.ErrStreamEOF) {\n\t\t\t\tos.Exit(0)\n\t\t\t}\n\n\t\t\tlog.Fatal(err)\n\t\t}\n\n\t\tif len(e.Data.Choices) \u003e 0 {\n\t\t\tfmt.Print(e.Data.Choices[0].Delta.Content)\n\t\t}\n\t}\n}\n\n// The event struct uses tags to specify how to unmarshal the event data.\ntype event struct {\n\tData chunk `sse:\"data\"`\n}\n\ntype chunk struct {\n\tChoices []choice\n}\n\n// The chunk struct implements the `sseparser.UnmarshalerSSEValue` interface,\n// which makes it easy to unmarshal event field values which in this case are\n// complete JSON payloads.\nfunc (c *chunk) UnmarshalSSEValue(v string) error {\n\tif v == \"[DONE]\" {\n\t\treturn nil\n\t}\n\n\treturn json.Unmarshal([]byte(v), c)\n}\n\ntype choice struct {\n\tDelta delta\n}\n\ntype delta struct {\n\tContent string\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Fsseparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjclem%2Fsseparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Fsseparser/lists"}