Skip to content

Commit 64bdabb

Browse files
morganchen12G.Dev.Ssomsak
authored and
G.Dev.Ssomsak
committed
update doc comments (google-gemini#166)
1 parent fed56f4 commit 64bdabb

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

Sources/GoogleAI/Chat.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public class Chat {
3030
/// model. This will be provided to the model for each message sent as context for the discussion.
3131
public var history: [ModelContent]
3232

33-
/// See ``sendMessage(_:)-3ify5``.
33+
/// Sends a message using the existing history of this chat as context. If successful, the message
34+
/// and response will be added to the history. If unsuccessful, history will remain unchanged.
35+
/// - Parameter parts: The new content to send as a single chat message.
36+
/// - Returns: The model's response if no error occurred.
37+
/// - Throws: A ``GenerateContentError`` if an error occurred.
3438
public func sendMessage(_ parts: any ThrowingPartsRepresentable...) async throws
3539
-> GenerateContentResponse {
3640
return try await sendMessage([ModelContent(parts: parts)])
@@ -98,7 +102,10 @@ public class Chat {
98102
return try await sendMessage([functionResponseContent])
99103
}
100104

101-
/// See ``sendMessageStream(_:)-4abs3``.
105+
/// Sends a message using the existing history of this chat as context. If successful, the message
106+
/// and response will be added to the history. If unsuccessful, history will remain unchanged.
107+
/// - Parameter parts: The new content to send as a single chat message.
108+
/// - Returns: A stream containing the model's response or an error if an error occurred.
102109
@available(macOS 12.0, *)
103110
public func sendMessageStream(_ parts: any ThrowingPartsRepresentable...)
104111
-> AsyncThrowingStream<GenerateContentResponse, Error> {

Sources/GoogleAI/GenerativeAISwift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Foundation
2121
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
2222
public enum GenerativeAISwift {
2323
/// String value of the SDK version
24-
public static let version = "0.5.4"
24+
public static let version = "0.5.2"
2525
/// The Google AI backend endpoint URL.
2626
static let baseURL = "https://generativelanguage.googleapis.com"
2727
}

Sources/GoogleAI/GenerativeModel.swift

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,50 @@ public final class GenerativeModel {
3636
/// A list of tools the model may use to generate the next response.
3737
let tools: [Tool]?
3838

39+
/// Tool configuration for any `Tool` specified in the request.
40+
let toolConfig: ToolConfig?
41+
42+
/// Instructions that direct the model to behave a certain way.
43+
let systemInstruction: ModelContent?
44+
3945
/// Configuration parameters for sending requests to the backend.
4046
let requestOptions: RequestOptions
4147

48+
/// Initializes a new remote model with the given parameters.
49+
///
50+
/// - Parameters:
51+
/// - name: The name of the model to use, for example `"gemini-1.5-pro-latest"`; see
52+
/// [Gemini models](https://ai.google.dev/models/gemini) for a list of supported model names.
53+
/// - apiKey: The API key for your project.
54+
/// - generationConfig: The content generation parameters your model should use.
55+
/// - safetySettings: A value describing what types of harmful content your model should allow.
56+
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
57+
/// - systemInstruction: Instructions that direct the model to behave a certain way; currently
58+
/// only text content is supported, for example
59+
/// `ModelContent(role: "system", parts: "You are a cat. Your name is Neko.")`.
60+
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
61+
/// - requestOptions Configuration parameters for sending requests to the backend.
62+
public convenience init(name: String,
63+
apiKey: String,
64+
generationConfig: GenerationConfig? = nil,
65+
safetySettings: [SafetySetting]? = nil,
66+
tools: [Tool]? = nil,
67+
toolConfig: ToolConfig? = nil,
68+
systemInstruction: ModelContent? = nil,
69+
requestOptions: RequestOptions = RequestOptions()) {
70+
self.init(
71+
name: name,
72+
apiKey: apiKey,
73+
generationConfig: generationConfig,
74+
safetySettings: safetySettings,
75+
tools: tools,
76+
toolConfig: toolConfig,
77+
systemInstruction: systemInstruction,
78+
requestOptions: requestOptions,
79+
urlSession: .shared
80+
)
81+
}
82+
4283
/// Initializes a new remote model with the given parameters.
4384
///
4485
/// - Parameters:
@@ -48,19 +89,29 @@ public final class GenerativeModel {
4889
/// - generationConfig: The content generation parameters your model should use.
4990
/// - safetySettings: A value describing what types of harmful content your model should allow.
5091
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
92+
/// - systemInstruction: Instructions that direct the model to behave a certain way; currently
93+
/// only text content is supported, e.g., "You are a cat. Your name is Neko."
94+
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
5195
/// - requestOptions Configuration parameters for sending requests to the backend.
5296
public convenience init(name: String,
5397
apiKey: String,
5498
generationConfig: GenerationConfig? = nil,
5599
safetySettings: [SafetySetting]? = nil,
56100
tools: [Tool]? = nil,
101+
toolConfig: ToolConfig? = nil,
102+
systemInstruction: String...,
57103
requestOptions: RequestOptions = RequestOptions()) {
58104
self.init(
59105
name: name,
60106
apiKey: apiKey,
61107
generationConfig: generationConfig,
62108
safetySettings: safetySettings,
63109
tools: tools,
110+
toolConfig: toolConfig,
111+
systemInstruction: ModelContent(
112+
role: "system",
113+
parts: systemInstruction.map { ModelContent.Part.text($0) }
114+
),
64115
requestOptions: requestOptions,
65116
urlSession: .shared
66117
)
@@ -72,13 +123,17 @@ public final class GenerativeModel {
72123
generationConfig: GenerationConfig? = nil,
73124
safetySettings: [SafetySetting]? = nil,
74125
tools: [Tool]? = nil,
126+
toolConfig: ToolConfig? = nil,
127+
systemInstruction: ModelContent? = nil,
75128
requestOptions: RequestOptions = RequestOptions(),
76129
urlSession: URLSession) {
77130
modelResourceName = GenerativeModel.modelResourceName(name: name)
78131
generativeAIService = GenerativeAIService(apiKey: apiKey, urlSession: urlSession)
79132
self.generationConfig = generationConfig
80133
self.safetySettings = safetySettings
81134
self.tools = tools
135+
self.toolConfig = toolConfig
136+
self.systemInstruction = systemInstruction
82137
self.requestOptions = requestOptions
83138

84139
Logging.default.info("""
@@ -99,7 +154,7 @@ public final class GenerativeModel {
99154
/// [zero-shot](https://developers.google.com/machine-learning/glossary/generative#zero-shot-prompting)
100155
/// or "direct" prompts. For
101156
/// [few-shot](https://developers.google.com/machine-learning/glossary/generative#few-shot-prompting)
102-
/// prompts, see ``generateContent(_:)-58rm0``.
157+
/// prompts, see `generateContent(_ content: @autoclosure () throws -> [ModelContent])`.
103158
///
104159
/// - Parameter content: The input(s) given to the model as a prompt (see
105160
/// ``ThrowingPartsRepresentable``
@@ -125,6 +180,8 @@ public final class GenerativeModel {
125180
generationConfig: generationConfig,
126181
safetySettings: safetySettings,
127182
tools: tools,
183+
toolConfig: toolConfig,
184+
systemInstruction: systemInstruction,
128185
isStreaming: false,
129186
options: requestOptions)
130187
response = try await generativeAIService.loadRequest(request: generateContentRequest)
@@ -156,7 +213,7 @@ public final class GenerativeModel {
156213
/// [zero-shot](https://developers.google.com/machine-learning/glossary/generative#zero-shot-prompting)
157214
/// or "direct" prompts. For
158215
/// [few-shot](https://developers.google.com/machine-learning/glossary/generative#few-shot-prompting)
159-
/// prompts, see ``generateContent(_:)-58rm0``.
216+
/// prompts, see `generateContent(_ content: @autoclosure () throws -> [ModelContent])`.
160217
///
161218
/// - Parameter content: The input(s) given to the model as a prompt (see
162219
/// ``ThrowingPartsRepresentable``
@@ -197,6 +254,8 @@ public final class GenerativeModel {
197254
generationConfig: generationConfig,
198255
safetySettings: safetySettings,
199256
tools: tools,
257+
toolConfig: toolConfig,
258+
systemInstruction: systemInstruction,
200259
isStreaming: true,
201260
options: requestOptions)
202261

@@ -243,7 +302,7 @@ public final class GenerativeModel {
243302
/// [zero-shot](https://developers.google.com/machine-learning/glossary/generative#zero-shot-prompting)
244303
/// or "direct" prompts. For
245304
/// [few-shot](https://developers.google.com/machine-learning/glossary/generative#few-shot-prompting)
246-
/// input, see ``countTokens(_:)-9spwl``.
305+
/// input, see `countTokens(_ content: @autoclosure () throws -> [ModelContent])`.
247306
///
248307
/// - Parameter content: The input(s) given to the model as a prompt (see
249308
/// ``ThrowingPartsRepresentable``
@@ -301,7 +360,7 @@ public final class GenerativeModel {
301360
}
302361
}
303362

304-
/// See ``GenerativeModel/countTokens(_:)-9spwl``.
363+
/// An error thrown in `GenerativeModel.countTokens(_:)`.
305364
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
306365
public enum CountTokensError: Error {
307366
case internalError(underlying: Error)

0 commit comments

Comments
 (0)