Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ jobs:
${{ runner.os }}-swift-${{ matrix.swift }}-spm-

- name: Build
run: swift build -v
run: swift build --traits MLX,Llama,CoreML

- name: Test
run: swift test -v
run: swift test

test-linux:
name: Swift ${{ matrix.swift-version }} on Linux
Expand All @@ -66,7 +66,7 @@ jobs:
toolchain: ${{ matrix.swift-version }}

- name: Build
run: swift build -v
run: swift build

- name: Test
run: swift test -v
run: swift test
10 changes: 1 addition & 9 deletions Sources/AnyLanguageModel/Models/CoreMLLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,7 @@
// MARK: - Image Validation

private func validateNoImageSegments(in session: LanguageModelSession) throws {
// Check for image segments in instructions
if let instructions = session.instructions {
for segment in instructions.segments {
if case .image = segment {
throw CoreMLLanguageModelError.unsupportedFeature
}
}
}

// Note: Instructions is a plain text type without segments, so no image check needed there.
// Check for image segments in the most recent prompt
for entry in session.transcript.reversed() {
if case .prompt(let p) = entry {
Expand Down
26 changes: 12 additions & 14 deletions Sources/AnyLanguageModel/Models/MLXLanguageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ import Foundation
let hub = self.hub
let directory = self.directory

let stream: AsyncThrowingStream<LanguageModelSession.ResponseStream<Content>.Snapshot, any Error> = .init { continuation in
let stream: AsyncThrowingStream<LanguageModelSession.ResponseStream<Content>.Snapshot, any Error> = .init {
continuation in
let task = Task { @Sendable in
do {
let context: ModelContext
Expand Down Expand Up @@ -213,7 +214,8 @@ import Foundation
case .chunk(let text):
accumulatedText += text
let raw = GeneratedContent(accumulatedText)
let content: Content.PartiallyGenerated = (accumulatedText as! Content).asPartiallyGenerated()
let content: Content.PartiallyGenerated = (accumulatedText as! Content)
.asPartiallyGenerated()
continuation.yield(.init(content: content, rawContent: raw))
case .info, .toolCall:
break
Expand Down Expand Up @@ -357,28 +359,24 @@ import Foundation
// MARK: - Tool Conversion

private func convertToolToMLXSpec(_ tool: any Tool) -> ToolSpec {
// Convert AnyLanguageModel's GenerationSchema to JSON-compatible dictionary
let parametersDict: [String: Any]
// Convert AnyLanguageModel's GenerationSchema to Sendable dictionary
// using MLXLMCommon.JSONValue which is already Sendable
let parametersValue: JSONValue
do {
let resolvedSchema = tool.parameters.withResolvedRoot() ?? tool.parameters
let encoder = JSONEncoder()
let data = try encoder.encode(resolvedSchema)
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
parametersDict = json
} else {
parametersDict = ["type": "object", "properties": [:], "required": []]
}
let data = try JSONEncoder().encode(resolvedSchema)
parametersValue = try JSONDecoder().decode(JSONValue.self, from: data)
} catch {
parametersDict = ["type": "object", "properties": [:], "required": []]
parametersValue = .object(["type": .string("object"), "properties": .object([:]), "required": .array([])])
}

return [
"type": "function",
"function": [
"name": tool.name,
"description": tool.description,
"parameters": parametersDict,
],
"parameters": parametersValue,
] as [String: any Sendable],
]
}

Expand Down