-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from swiftde/openai-summary
Summarize messages and images using gpt-4o-mini
- Loading branch information
Showing
5 changed files
with
242 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
/*.xcodeproj | ||
xcuserdata/ | ||
.env.local | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import AsyncHTTPClient | ||
import Foundation | ||
import NIO | ||
|
||
extension HTTPClient { | ||
func get<Response>( | ||
_ url: String, | ||
headers: [String: String]? = nil, | ||
response: Response.Type | ||
) async throws -> Response where Response: Decodable { | ||
var request = HTTPClientRequest(url: url) | ||
if let headers { | ||
request.headers = .init(headers.map{ ($0.key, $0.value) }) | ||
} | ||
let response = try await self.execute(request, timeout: .seconds(60)) | ||
if response.status == .ok { | ||
let body = try await response.body.collect(upTo: 50 * 1024 * 1024) // 50 MB | ||
return try JSONDecoder().decode(Response.self, from: body) | ||
} else { | ||
throw response.status.description | ||
} | ||
} | ||
|
||
func post<Response>( | ||
_ url: String, | ||
headers: [String: String]? = nil, | ||
body: (any Encodable)?, | ||
response: Response.Type | ||
) async throws -> Response where Response: Decodable { | ||
var request = HTTPClientRequest(url: url) | ||
request.method = .POST | ||
if let headers { | ||
request.headers = .init(headers.map{ ($0.key, $0.value) }) | ||
} | ||
request.headers.add(name: "Content-Type", value: "application/json") | ||
if let body { | ||
let data = try JSONEncoder().encode(body) | ||
print(String(data: data, encoding: .utf8)!) | ||
var buffer = ByteBufferAllocator().buffer(capacity: data.count) | ||
buffer.writeBytes(data) | ||
request.body = .bytes(buffer) | ||
} | ||
let response = try await self.execute(request, timeout: .seconds(60)) | ||
if response.status == .ok { | ||
let body = try await response.body.collect(upTo: 50 * 1024 * 1024) // 50 MB | ||
return try JSONDecoder().decode(Response.self, from: body) | ||
} else { | ||
throw response.status.description | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters