You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+49-45Lines changed: 49 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,19 +55,21 @@ Using EventSource is easy. Simply create a new data task from an instance of Eve
55
55
```swift
56
56
importEventSource
57
57
58
-
let eventSource =EventSource()
59
-
let dataTask = eventSource.dataTask(for: urlRequest)
60
-
61
-
forawait event in dataTask.events() {
62
-
switch event {
63
-
case .open:
64
-
print("Connection was opened.")
65
-
case .error(let error):
66
-
print("Received an error:", error.localizedDescription)
67
-
case .event(let event):
68
-
print("Received an event", event.data??"")
69
-
case .closed:
70
-
print("Connection was closed.")
58
+
Task {
59
+
let eventSource =EventSource()
60
+
let dataTask = eventSource.dataTask(for: urlRequest)
61
+
62
+
forawait event in dataTask.events() {
63
+
switch event {
64
+
case .open:
65
+
print("Connection was opened.")
66
+
case .error(let error):
67
+
print("Received an error:", error.localizedDescription)
68
+
case .event(let event):
69
+
print("Received an event", event.data??"")
70
+
case .closed:
71
+
print("Connection was closed.")
72
+
}
71
73
}
72
74
}
73
75
```
@@ -78,41 +80,43 @@ Use `dataTask.cancel()` to explicitly close the connection. However, in that cas
78
80
79
81
EventSource can be used in data-only mode, making it suitable for popular APIs like [OpenAI](https://platform.openai.com/docs/overview). Below is an example using OpenAI's [completions](https://platform.openai.com/docs/guides/text-generation) API:
80
82
```swift
81
-
var urlRequest =URLRequest(url: URL(string: "https://api.openai.com/v1/chat/completions")!)
82
-
urlRequest.allHTTPHeaderFields= [
83
-
"Content-Type":"application/json",
84
-
"Authorization":"Bearer \(accessToken)"
85
-
]
86
-
urlRequest.httpMethod="POST"
87
-
urlRequest.httpBody="""
88
-
{
89
-
"model": "gpt-4o-mini",
90
-
"messages": [
91
-
{"role": "user", "content": "Why is the sky blue?"}
92
-
],
93
-
"stream": true
94
-
}
95
-
""".data(using: .utf8)!
96
-
97
-
let eventSource =EventSource(mode: .dataOnly)
98
-
let dataTask = eventSource.dataTask(for: urlRequest)
99
-
100
-
var response: String=""
101
-
102
-
forawait event in dataTask.events() {
103
-
switch event {
104
-
case .event(let event):
105
-
iflet data = eventDevent.data?.data(using: .utf8) {
106
-
let chunk =try?JSONDecoder().decode(ChatCompletionChunk.self, from: data)
107
-
let string = chunk?.choices.first?.delta.content??""
108
-
response += string
83
+
Task {
84
+
var urlRequest =URLRequest(url: URL(string: "https://api.openai.com/v1/chat/completions")!)
85
+
urlRequest.allHTTPHeaderFields= [
86
+
"Content-Type":"application/json",
87
+
"Authorization":"Bearer \(accessToken)"
88
+
]
89
+
urlRequest.httpMethod="POST"
90
+
urlRequest.httpBody="""
91
+
{
92
+
"model": "gpt-4o-mini",
93
+
"messages": [
94
+
{"role": "user", "content": "Why is the sky blue?"}
95
+
],
96
+
"stream": true
97
+
}
98
+
""".data(using: .utf8)!
99
+
100
+
let eventSource =EventSource(mode: .dataOnly)
101
+
let dataTask = eventSource.dataTask(for: urlRequest)
102
+
103
+
var response: String=""
104
+
105
+
forawait event in dataTask.events() {
106
+
switch event {
107
+
case .event(let event):
108
+
iflet data = eventDevent.data?.data(using: .utf8) {
109
+
let chunk =try?JSONDecoder().decode(ChatCompletionChunk.self, from: data)
110
+
let string = chunk?.choices.first?.delta.content??""
0 commit comments