Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
vmanot committed Mar 24, 2024
1 parent ef1be23 commit 96011e0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
xcode: ['15.2']
xcode: ['latest']
macos: ['macos-13']
scheme: ['AI']
command: ['build']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extension OpenAI.APISpecification.ResponseBodies {
self.usage = try container.decode(forKey: .usage)

try super.init(from: decoder)

self.data = self.data.sorted(by: \.index)
}

public override func encode(to encoder: Encoder) throws {
Expand Down
3 changes: 2 additions & 1 deletion Sources/OpenAI/Intramodular/Models/OpenAI.List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ extension OpenAI {
case lastID = "lastId"
}

public let data: [T]
public package(set) var data: [T]

public let hasMore: Bool?
public let firstID: String?
public let lastID: String?
Expand Down
33 changes: 0 additions & 33 deletions Sources/OpenAI/Intramodular/OpenAI+LLMRequestHandling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,39 +295,6 @@ extension OpenAI.APIClient {
}
}

extension OpenAI.APIClient: TextEmbeddingsRequestHandling {
public func fulfill(
_ request: TextEmbeddingsRequest
) async throws -> TextEmbeddings {
guard !request.input.isEmpty else {
return TextEmbeddings(
model: .init(from: OpenAI.Model.Embedding.ada),
data: []
)
}

let model = request.model ?? _MLModelIdentifier(from: OpenAI.Model.Embedding.ada)
let embeddingModel = try OpenAI.Model.Embedding(rawValue: model.name).unwrap()

let embeddings = try await createEmbeddings(
model: embeddingModel,
for: request.input
).data

try _tryAssert(request.input.count == embeddings.count)

return TextEmbeddings(
model: .init(from: OpenAI.Model.Embedding.ada),
data: request.input.zip(embeddings).map {
TextEmbeddings.Element(
text: $0,
embedding: $1.embedding
)
}
)
}
}

// MARK: - Auxiliary

extension _MLModelIdentifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Copyright (c) Vatsal Manot
//

import CoreMI
import CorePersistence

extension OpenAI.APIClient: TextEmbeddingsRequestHandling {
public func fulfill(
_ request: TextEmbeddingsRequest
) async throws -> TextEmbeddings {
guard !request.input.isEmpty else {
return TextEmbeddings(
model: .init(from: OpenAI.Model.Embedding.ada),
data: []
)
}

let model = request.model ?? _MLModelIdentifier(from: OpenAI.Model.Embedding.ada)
let embeddingModel = try OpenAI.Model.Embedding(rawValue: model.name).unwrap()

let embeddings = try await createEmbeddings(
model: embeddingModel,
for: request.input
).data

try _tryAssert(request.input.count == embeddings.count)

return TextEmbeddings(
model: .init(from: OpenAI.Model.Embedding.ada),
data: request.input.zip(embeddings).map {
TextEmbeddings.Element(
text: $0,
embedding: $1.embedding
)
}
)
}
}

0 comments on commit 96011e0

Please sign in to comment.