diff --git a/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift b/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift index ef86cfb6..b001dbe9 100644 --- a/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift +++ b/Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift @@ -526,7 +526,7 @@ public struct AnthropicLanguageModel: LanguageModel, ProfileRequestIdentifiedLan } let params = try createMessageParams( model: model, - system: nil, + system: requestContext.transcript.anthropicSystem, messages: try anthropicMessages( from: requestContext.transcript.saying(schemaInstructions), replacing: entries, @@ -725,7 +725,7 @@ public struct AnthropicLanguageModel: LanguageModel, ProfileRequestIdentifiedLan ) var params = try createMessageParams( model: model, - system: nil, + system: requestContext.transcript.anthropicSystem, messages: messages, tools: anthropicTools.isEmpty ? nil : anthropicTools, responseSchema: responseSchema, @@ -1147,6 +1147,26 @@ private func convertToolToAnthropicFormat(_ tool: any Tool) throws -> AnthropicT // MARK: - Supporting Types extension Transcript { + /// The text of the transcript's instructions, for the top-level `system` field, + /// or `nil` if it has none. + /// + /// The Messages API has no `system` role, so instructions don't go in `messages`. + /// The field takes only text, so image segments are left out. + /// Each instructions entry is kept, in order. + fileprivate var anthropicSystem: String? { + let text = compactMap { entry -> String? in + guard case .instructions(let instructions) = entry else { return nil } + return instructions.segments.compactMap { segment -> String? in + switch segment { + case .text(let text): text.content + case .structure(let structure): structure.content.jsonString + case .image: nil + } + }.joined(separator: "\n") + }.filter { !$0.isEmpty }.joined(separator: "\n") + return text.isEmpty ? nil : text + } + fileprivate func toAnthropicMessages() throws -> [AnthropicMessage] { var messages = [AnthropicMessage]() func appendAssistant(_ content: [AnthropicContent]) { @@ -1158,13 +1178,9 @@ extension Transcript { } for item in replayEntries { switch item { - case .instructions(let instructions): - messages.append( - .init( - role: .user, - content: try convertSegmentsToAnthropicContent(instructions.segments) - ) - ) + case .instructions: + // Sent in the top-level `system` field; see `anthropicSystem`. + continue case .prompt(let prompt): messages.append(try anthropicUserMessage(prompt)) case .reasoning(let reasoning): diff --git a/Tests/AnyLanguageModelTests/AnthropicSystemInstructionsTests.swift b/Tests/AnyLanguageModelTests/AnthropicSystemInstructionsTests.swift new file mode 100644 index 00000000..9c690fdc --- /dev/null +++ b/Tests/AnyLanguageModelTests/AnthropicSystemInstructionsTests.swift @@ -0,0 +1,154 @@ +import Foundation +import Testing + +@testable import AnyLanguageModel + +/// Instructions go to Anthropic in the top-level `system` field, +/// because the Messages API has no `system` role. +@Suite("Anthropic system instructions") +struct AnthropicSystemInstructionsTests { + private static let instructions = "You are a helpful assistant. Be concise." + + @Test(arguments: [false, true]) + func instructionsAreSentAsSystem(streaming: Bool) async throws { + let transport = FakeTransport(replies: [try Self.reply(text: "Hi", streaming: streaming)]) + let session = LanguageModelSession(model: Self.model(transport), instructions: Self.instructions) + + try await Self.respond(in: session, streaming: streaming) + + let body = try #require(try transport.bodies.map(Self.json).first) + #expect(body["system"] as? String == Self.instructions) + let messages = try #require(body["messages"] as? [[String: Any]]) + #expect(messages.count == 1) + #expect(messages.first?["role"] as? String == "user") + #expect(Self.text(of: messages.first) == "Hello") + } + + @Test(arguments: [false, true]) + func eachToolRoundSendsInstructionsAsSystem(streaming: Bool) async throws { + let transport = FakeTransport(replies: [ + try Self.reply(toolCallStreaming: streaming), + try Self.reply(text: "Sunny", streaming: streaming), + ]) + let session = LanguageModelSession( + model: Self.model(transport), + tools: [WeatherTool()], + instructions: Self.instructions + ) + + try await Self.respond(in: session, streaming: streaming) + + let bodies = try transport.bodies.map(Self.json) + try #require(bodies.count == 2) + for body in bodies { + #expect(body["system"] as? String == Self.instructions) + let messages = try #require(body["messages"] as? [[String: Any]]) + #expect(messages.first?["role"] as? String == "user") + #expect(Self.text(of: messages.first) == "Hello") + #expect(!messages.contains { Self.text(of: $0) == Self.instructions }) + } + } + + @Test func instructionsEntriesAreJoinedWithoutImages() async throws { + let transport = FakeTransport(replies: [try Self.reply(text: "Hi", streaming: false)]) + let image = Transcript.ImageSegment(data: Data([0x89, 0x50, 0x4E, 0x47]), mimeType: "image/png") + let transcript = Transcript(entries: [ + .instructions( + .init( + segments: [.text(.init(content: "First.")), .image(image), .text(.init(content: "Second."))], + toolDefinitions: [] + ) + ), + .instructions(.init(segments: [.text(.init(content: "Third."))], toolDefinitions: [])), + ]) + let session = LanguageModelSession(model: Self.model(transport), transcript: transcript) + + try await Self.respond(in: session, streaming: false) + + let body = try #require(try transport.bodies.map(Self.json).first) + #expect(body["system"] as? String == "First.\nSecond.\nThird.") + let messages = try #require(body["messages"] as? [[String: Any]]) + #expect(messages.count == 1) + #expect(Self.text(of: messages.first) == "Hello") + } + + @Test func noInstructionsOmitsSystem() async throws { + let transport = FakeTransport(replies: [try Self.reply(text: "Hi", streaming: false)]) + let session = LanguageModelSession(model: Self.model(transport)) + + try await Self.respond(in: session, streaming: false) + + let body = try #require(try transport.bodies.map(Self.json).first) + #expect(body["system"] == nil) + } + + // MARK: Fixtures + + private static func model(_ transport: FakeTransport) -> AnthropicLanguageModel { + AnthropicLanguageModel(apiKey: "test", model: "test", transport: transport) + } + + private static func respond(in session: LanguageModelSession, streaming: Bool) async throws { + if streaming { + _ = try await session.streamResponse(to: "Hello").collect() + } else { + _ = try await session.respond(to: "Hello") + } + } + + private static func json(_ data: Data) throws -> [String: Any] { + try #require(try JSONSerialization.jsonObject(with: data) as? [String: Any]) + } + + /// The joined text blocks of a message. + private static func text(of message: [String: Any]?) -> String? { + guard let content = message?["content"] as? [[String: Any]] else { return nil } + return content.compactMap { $0["type"] as? String == "text" ? $0["text"] as? String : nil }.joined() + } + + private static func encode(_ object: [String: Any]) throws -> String { + String(decoding: try JSONSerialization.data(withJSONObject: object), as: UTF8.self) + } + + private static func message(_ content: [[String: Any]], stopReason: String) -> [String: Any] { + [ + "id": "m", "type": "message", "role": "assistant", "model": "test", "content": content, + "stop_reason": stopReason, "usage": ["input_tokens": 1, "output_tokens": 1], + ] + } + + private static func reply(text: String, streaming: Bool) throws -> FakeTransport.Reply { + guard streaming else { + return .init(chunks: [try encode(message([["type": "text", "text": text]], stopReason: "end_turn"))]) + } + let events: [[String: Any]] = [ + ["type": "message_start", "message": message([], stopReason: "end_turn")], + ["type": "content_block_start", "index": 0, "content_block": ["type": "text", "text": ""]], + ["type": "content_block_delta", "index": 0, "delta": ["type": "text_delta", "text": text]], + ["type": "content_block_stop", "index": 0], + ["type": "message_stop"], + ] + return .init(chunks: try events.map { "data: \(try encode($0))\n\n" }) + } + + private static func reply(toolCallStreaming streaming: Bool) throws -> FakeTransport.Reply { + let call: [String: Any] = [ + "type": "tool_use", "id": "call_1", "name": "getWeather", "input": ["city": "Paris"], + ] + guard streaming else { return .init(chunks: [try encode(message([call], stopReason: "tool_use"))]) } + let events: [[String: Any]] = [ + ["type": "message_start", "message": message([], stopReason: "tool_use")], + [ + "type": "content_block_start", "index": 0, + "content_block": ["type": "tool_use", "id": "call_1", "name": "getWeather", "input": [:]], + ], + [ + "type": "content_block_delta", "index": 0, + "delta": ["type": "input_json_delta", "partial_json": #"{"city":"Paris"}"#], + ], + ["type": "content_block_stop", "index": 0], + ["type": "message_stop"], + ] + return .init(chunks: try events.map { "data: \(try encode($0))\n\n" }) + } +}