Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode model in GenerateContentRequest only when needed #174

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Sources/GoogleAI/CountTokensRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
struct CountTokensRequest {
let model: String
let contents: [ModelContent]
let generateContentRequest: GenerateContentRequest
let options: RequestOptions
}

Expand All @@ -42,7 +42,7 @@ public struct CountTokensResponse {
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension CountTokensRequest: Encodable {
enum CodingKeys: CodingKey {
case contents
case generateContentRequest
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
30 changes: 29 additions & 1 deletion Sources/GoogleAI/GenerateContentRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import Foundation

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
struct GenerateContentRequest {
/// Model name.
// Model name.
let model: String
// If true, the `model` field above is encoded in requests; currently only required when nested in
// a `CountTokensRequest`.
let isModelEncoded: Bool
let contents: [ModelContent]
let generationConfig: GenerationConfig?
let safetySettings: [SafetySetting]?
Expand All @@ -31,13 +34,38 @@ struct GenerateContentRequest {
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
extension GenerateContentRequest: Encodable {
enum CodingKeys: String, CodingKey {
case model
case contents
case generationConfig
case safetySettings
case tools
case toolConfig
case systemInstruction
}

func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

if isModelEncoded {
try container.encode(model, forKey: .model)
}
try container.encode(contents, forKey: .contents)
if let generationConfig {
try container.encode(generationConfig, forKey: .generationConfig)
}
if let safetySettings {
try container.encode(safetySettings, forKey: .safetySettings)
}
if let tools {
try container.encode(tools, forKey: .tools)
}
if let toolConfig {
try container.encode(toolConfig, forKey: .toolConfig)
}
if let systemInstruction {
try container.encode(systemInstruction, forKey: .systemInstruction)
}
}
}

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
Expand Down
56 changes: 36 additions & 20 deletions Sources/GoogleAI/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,18 @@ public final class GenerativeModel {
-> GenerateContentResponse {
let response: GenerateContentResponse
do {
let generateContentRequest = try GenerateContentRequest(model: modelResourceName,
contents: content(),
generationConfig: generationConfig,
safetySettings: safetySettings,
tools: tools,
toolConfig: toolConfig,
systemInstruction: systemInstruction,
isStreaming: false,
options: requestOptions)
let generateContentRequest = try GenerateContentRequest(
model: modelResourceName,
isModelEncoded: false,
contents: content(),
generationConfig: generationConfig,
safetySettings: safetySettings,
tools: tools,
toolConfig: toolConfig,
systemInstruction: systemInstruction,
isStreaming: false,
options: requestOptions
)
response = try await generativeAIService.loadRequest(request: generateContentRequest)
} catch {
if let imageError = error as? ImageConversionError {
Expand Down Expand Up @@ -249,15 +252,18 @@ public final class GenerativeModel {
}
}

let generateContentRequest = GenerateContentRequest(model: modelResourceName,
contents: evaluatedContent,
generationConfig: generationConfig,
safetySettings: safetySettings,
tools: tools,
toolConfig: toolConfig,
systemInstruction: systemInstruction,
isStreaming: true,
options: requestOptions)
let generateContentRequest = GenerateContentRequest(
model: modelResourceName,
isModelEncoded: false,
contents: evaluatedContent,
generationConfig: generationConfig,
safetySettings: safetySettings,
tools: tools,
toolConfig: toolConfig,
systemInstruction: systemInstruction,
isStreaming: true,
options: requestOptions
)

var responseIterator = generativeAIService.loadRequestStream(request: generateContentRequest)
.makeAsyncIterator()
Expand Down Expand Up @@ -325,9 +331,19 @@ public final class GenerativeModel {
public func countTokens(_ content: @autoclosure () throws -> [ModelContent]) async throws
-> CountTokensResponse {
do {
let countTokensRequest = try CountTokensRequest(
let generateContentRequest = try GenerateContentRequest(model: modelResourceName,
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
isModelEncoded: true,
contents: content(),
generationConfig: generationConfig,
safetySettings: safetySettings,
tools: tools,
toolConfig: toolConfig,
systemInstruction: systemInstruction,
isStreaming: false,
options: requestOptions)
let countTokensRequest = CountTokensRequest(
model: modelResourceName,
contents: content(),
generateContentRequest: generateContentRequest,
options: requestOptions
)
return try await generativeAIService.loadRequest(request: countTokensRequest)
Expand Down
180 changes: 180 additions & 0 deletions Tests/GoogleAITests/GenerateContentRequestTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import XCTest

@testable import GoogleGenerativeAI

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
final class GenerateContentRequestTests: XCTestCase {
let encoder = JSONEncoder()
let role = "test-role"
let prompt = "test-prompt"
let modelName = "test-model-name"

override func setUp() {
encoder.outputFormatting = .init(
arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
)
}

// MARK: GenerateContentRequest Encoding

func testEncodeRequest_allFieldsIncluded() throws {
let content = [ModelContent(role: role, parts: prompt)]
let request = GenerateContentRequest(
model: modelName,
isModelEncoded: true,
contents: content,
generationConfig: GenerationConfig(temperature: 0.5),
safetySettings: [SafetySetting(
harmCategory: .dangerousContent,
threshold: .blockLowAndAbove
)],
tools: [Tool(functionDeclarations: [FunctionDeclaration(
name: "test-function-name",
description: "test-function-description",
parameters: nil
)])],
toolConfig: ToolConfig(functionCallingConfig: FunctionCallingConfig(mode: .auto)),
systemInstruction: ModelContent(role: "system", parts: "test-system-instruction"),
isStreaming: false,
options: RequestOptions()
)

let jsonData = try encoder.encode(request)

let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
XCTAssertEqual(json, """
{
"contents" : [
{
"parts" : [
{
"text" : "\(prompt)"
}
],
"role" : "\(role)"
}
],
"generationConfig" : {
"temperature" : 0.5
},
"model" : "\(modelName)",
"safetySettings" : [
{
"category" : "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold" : "BLOCK_LOW_AND_ABOVE"
}
],
"systemInstruction" : {
"parts" : [
{
"text" : "test-system-instruction"
}
],
"role" : "system"
},
"toolConfig" : {
"functionCallingConfig" : {
"mode" : "AUTO"
}
},
"tools" : [
{
"functionDeclarations" : [
{
"description" : "test-function-description",
"name" : "test-function-name",
"parameters" : {
"type" : "OBJECT"
}
}
]
}
]
}
""")
}

func testEncodeRequest_optionalFieldsOmitted_modelNameEncoded() throws {
let content = [ModelContent(role: role, parts: prompt)]
let request = GenerateContentRequest(
model: modelName,
isModelEncoded: true,
contents: content,
generationConfig: nil,
safetySettings: nil,
tools: nil,
toolConfig: nil,
systemInstruction: nil,
isStreaming: false,
options: RequestOptions()
)

let jsonData = try encoder.encode(request)

let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
XCTAssertEqual(json, """
{
"contents" : [
{
"parts" : [
{
"text" : "\(prompt)"
}
],
"role" : "\(role)"
}
],
"model" : "\(modelName)"
}
""")
}

func testEncodeRequest_optionalFieldsOmitted_modelNameNotEncoded() throws {
let content = [ModelContent(role: role, parts: prompt)]
let request = GenerateContentRequest(
model: modelName,
isModelEncoded: false,
contents: content,
generationConfig: nil,
safetySettings: nil,
tools: nil,
toolConfig: nil,
systemInstruction: nil,
isStreaming: false,
options: RequestOptions()
)

let jsonData = try encoder.encode(request)

let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
XCTAssertEqual(json, """
{
"contents" : [
{
"parts" : [
{
"text" : "\(prompt)"
}
],
"role" : "\(role)"
}
]
}
""")
}
}
Loading