Skip to content

Commit

Permalink
Simplify back to a single URI
Browse files Browse the repository at this point in the history
  • Loading branch information
natebosch committed Apr 9, 2024
1 parent ccb1a65 commit c42cf7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 56 deletions.
3 changes: 2 additions & 1 deletion pkgs/google_generative_ai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.2.4-wip

- Allow specifying an API version.
- Allow specifying an API version in a `requestOptions` argument when
constructing a model.

## 0.2.3

Expand Down
73 changes: 19 additions & 54 deletions pkgs/google_generative_ai/lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ final class GenerativeModel {
final List<SafetySetting> _safetySettings;
final GenerationConfig? _generationConfig;
final ApiClient _client;
final RequestOptions? _requestOptions;
final Uri Function(RequestOptions?) _baseUri;
final Uri _baseUri;

/// Create a [GenerativeModel] backed by the generative model named [model].
///
Expand Down Expand Up @@ -86,23 +85,20 @@ final class GenerativeModel {
model: model,
safetySettings: safetySettings,
generationConfig: generationConfig,
baseUri: _googleAIBaseUri,
requestOptions: requestOptions,
baseUri: _googleAIBaseUri(requestOptions),
);

GenerativeModel._withClient({
required ApiClient client,
required String model,
required List<SafetySetting> safetySettings,
required GenerationConfig? generationConfig,
required Uri Function(RequestOptions?) baseUri,
required RequestOptions? requestOptions,
required Uri baseUri,
}) : _model = _normalizeModelName(model),
_baseUri = baseUri,
_safetySettings = safetySettings,
_generationConfig = generationConfig,
_client = client,
_requestOptions = requestOptions;
_client = client;

/// Returns the model code for a user friendly model name.
///
Expand All @@ -114,12 +110,9 @@ final class GenerativeModel {
return (prefix: parts.first, name: parts.skip(1).join('/'));
}

Uri _taskUri(Task task) {
final baseUri = _baseUri(_requestOptions);
return baseUri.replace(
pathSegments: baseUri.pathSegments
.followedBy([_model.prefix, '${_model.name}:${task.name}']));
}
Uri _taskUri(Task task) => _baseUri.replace(
pathSegments: _baseUri.pathSegments
.followedBy([_model.prefix, '${_model.name}:${task.name}']));

/// Generates content responding to [prompt].
///
Expand Down Expand Up @@ -268,8 +261,7 @@ GenerativeModel createModelWithClient({
model: model,
safetySettings: safetySettings,
generationConfig: generationConfig,
baseUri: _googleAIBaseUri,
requestOptions: requestOptions,
baseUri: _googleAIBaseUri(requestOptions),
);

/// Creates a model with an overridden base URL to communicate with a different
Expand All @@ -278,42 +270,15 @@ GenerativeModel createModelWithClient({
/// Used from a `src/` import in the Vertex AI SDK.
// TODO: https://github.com/google/generative-ai-dart/issues/111 - Changes to
// this API need to be coordinated with the vertex AI SDK.
GenerativeModel createModelWithBaseUri({
required String model,
required String apiKey,
required Uri baseUri,
List<SafetySetting> safetySettings = const [],
GenerationConfig? generationConfig,
RequestOptions? requestOptions,
}) =>
GenerativeModel._withClient(
client: HttpApiClient(apiKey: apiKey),
model: model,
safetySettings: safetySettings,
generationConfig: generationConfig,
baseUri: (_) => baseUri,
requestOptions: requestOptions,
);

/// Creates a model with an overridden base URL callback to communicate with a
/// different backend.
///
/// Used from a `src/` import in the Vertex AI SDK.
// TODO: https://github.com/google/generative-ai-dart/issues/111 - Changes to
// this API need to be coordinated with the vertex AI SDK.
GenerativeModel createModelWithVersionedBaseUri({
required String model,
required String apiKey,
required Uri Function(RequestOptions?) baseUri,
List<SafetySetting> safetySettings = const [],
GenerationConfig? generationConfig,
RequestOptions? requestOptions,
}) =>
GenerativeModel createModelWithBaseUri(
{required String model,
required String apiKey,
required Uri baseUri,
List<SafetySetting> safetySettings = const [],
GenerationConfig? generationConfig}) =>
GenerativeModel._withClient(
client: HttpApiClient(apiKey: apiKey),
model: model,
safetySettings: safetySettings,
generationConfig: generationConfig,
baseUri: baseUri,
requestOptions: requestOptions,
);
client: HttpApiClient(apiKey: apiKey),
model: model,
safetySettings: safetySettings,
generationConfig: generationConfig,
baseUri: baseUri);
2 changes: 1 addition & 1 deletion pkgs/google_generative_ai/lib/src/version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const packageVersion = '0.2.3';
const packageVersion = '0.2.4-wip';

0 comments on commit c42cf7f

Please sign in to comment.