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

Allow for a simpler way to set system instructions #149

Merged
merged 1 commit into from
Apr 29, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Sources/GoogleAI/GenerativeModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ public final class GenerativeModel {
/// Configuration parameters for sending requests to the backend.
let requestOptions: RequestOptions

/// Initializes a new remote model with the given parameters.
///
/// - Parameters:
/// - name: The name of the model to use, e.g., `"gemini-1.5-pro-latest"`; see
/// [Gemini models](https://ai.google.dev/models/gemini) for a list of supported model names.
/// - apiKey: The API key for your project.
/// - generationConfig: The content generation parameters your model should use.
/// - safetySettings: A value describing what types of harmful content your model should allow.
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
/// - systemInstruction: Instructions that direct the model to behave a certain way; currently
/// only text content is supported.
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
/// - requestOptions Configuration parameters for sending requests to the backend.
public convenience init(name: String,
apiKey: String,
generationConfig: GenerationConfig? = nil,
safetySettings: [SafetySetting]? = nil,
tools: [Tool]? = nil,
toolConfig: ToolConfig? = nil,
systemInstruction: any ThrowingPartsRepresentable...,
requestOptions: RequestOptions = RequestOptions()) {
self.init(
name: name,
apiKey: apiKey,
generationConfig: generationConfig,
safetySettings: safetySettings,
tools: tools,
toolConfig: toolConfig,
systemInstruction: try? ModelContent(role: "system", parts: systemInstruction),
requestOptions: requestOptions,
urlSession: .shared
)
}

/// Initializes a new remote model with the given parameters.
///
/// - Parameters:
Expand Down
6 changes: 6 additions & 0 deletions Tests/GoogleAITests/GoogleAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ final class GoogleGenerativeAITests: XCTestCase {
systemInstruction: systemInstruction
)

let _ = GenerativeModel(
name: "gemini-1.5-pro-latest",
apiKey: "API_KEY",
systemInstruction: "Talk like a pirate"
)

// All arguments passed.
let genAI = GenerativeModel(name: "gemini-1.5-pro-latest",
apiKey: "API_KEY",
Expand Down