Skip to content

Commit

Permalink
Allow for a simpler way to set system instructions (#149)
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Friese <[email protected]>
  • Loading branch information
peterfriese authored Apr 29, 2024
1 parent 7fdb4c7 commit e195730
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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

0 comments on commit e195730

Please sign in to comment.