Skip to content

Commit

Permalink
refactor(vertexai): Split into separate libraries (#12794)
Browse files Browse the repository at this point in the history
* Split into separate libraries

The current approach uses `part` files which cannot be imported
separately. One drawback of this approach is that any members which need
to be referenced by test code must be public to the library. Using
`src/` libraries, as opposed to `src/` part files, allows making APIs
that are "package private" by convention - they are public to the
library, but only exported through the `src/` import.

One potential downside (alternatively seen as a benefit) of using
separate libraries is that private members are truly private to their
library, and we cannot make a member of a class private to the package.
The convention for package imports only allows top level declarations to
be considered non-public implementation details.

- Remove imports from the top level library. Replace `part` lines with
  `export` of the same files.
- Add explicit `show` clauses to the exports. This is what allows an API
  to be public when imported from the `src/` library, but not exposed
  through the package public library.
- Change some private `_toGoogleAI<TypeName>` methods to extension
  methods. Use the name `toGoogleAI` since the specific type names are
  clear from context.
- Change some private `_fromGoogleAI<TypeName>` factory constructors to
  extension methods on the google AI SDk types. Name `toVertex()`.
- Remove some unused private methods that were filled in for
  consistency. More extension members can be added as needed.
- Add an extension to expose the private field `_gooleAiModel` from
  `GenerativeModel`.
- Add a top level method to forward to the private `GenerativeModel`
  constructor.
- Add TODO comments to stop exporting some methods that don't need to be
  public. These could have been private in the `part` pattern. For now
  they are exported for backwards compatibility, and we can remove them
  when we are ready for a major version bump.

* Fix typo

* Add (redundant) docs

* dart format

* Add missing auth argument forwarding

* Bad merge?

* More bad merge

* Unnecessary change

* Update packages/firebase_vertexai/firebase_vertexai/lib/src/vertex_api.dart

Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>

---------

Co-authored-by: Cynthia J <cynthiajoan@users.noreply.github.com>
Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>
  • Loading branch information
3 people committed May 22, 2024
1 parent 76b4a54 commit 85a517f
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,58 @@
// See the License for the specific language governing permissions and
// limitations under the License.

library firebase_vertexai;

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'
show FirebasePluginPlatform;
import 'package:google_generative_ai/google_generative_ai.dart' as google_ai;
// ignore: implementation_imports, tightly coupled packages
import 'package:google_generative_ai/src/vertex_hooks.dart';

import 'src/vertex_version.dart';

part 'src/firebase_vertexai.dart';
part 'src/vertex_api.dart';
part 'src/vertex_chat.dart';
part 'src/vertex_content.dart';
part 'src/vertex_function_calling.dart';
part 'src/vertex_model.dart';
export 'src/firebase_vertexai.dart'
show
// TODO(next breaking): Remove defaultTimeout
defaultTimeout,
FirebaseVertexAI,
RequestOptions;
export 'src/vertex_api.dart'
show
BatchEmbedContentsResponse,
BlockReason,
Candidate,
CitationMetadata,
CitationSource,
ContentEmbedding,
CountTokensResponse,
// TODO(next breaking): Remove CountTokensResponseFields
CountTokensResponseFields,
EmbedContentRequest,
EmbedContentResponse,
FinishReason,
GenerateContentResponse,
GenerationConfig,
HarmBlockThreshold,
HarmCategory,
HarmProbability,
PromptFeedback,
SafetyRating,
SafetySetting,
TaskType,
// TODO(next breaking): Remove parse* methods
parseCountTokensResponse,
parseEmbedContentResponse,
parseGenerateContentResponse;
export 'src/vertex_chat.dart' show ChatSession, StartChatExtension;
export 'src/vertex_content.dart'
show
Content,
DataPart,
FileData,
FunctionCall,
FunctionResponse,
Part,
TextPart,
// TODO(next breaking): Remove parseContent
parseContent;
export 'src/vertex_function_calling.dart'
show
FunctionCallingConfig,
FunctionCallingMode,
FunctionDeclaration,
Schema,
SchemaType,
Tool,
ToolConfig;
export 'src/vertex_model.dart' show GenerativeModel;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

part of firebase_vertexai;
import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'
show FirebasePluginPlatform;

import 'vertex_api.dart';
import 'vertex_content.dart';
import 'vertex_function_calling.dart';
import 'vertex_model.dart';

const _defaultLocation = 'us-central1';

Expand Down Expand Up @@ -105,7 +114,7 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
Content? systemInstruction,
List<Tool>? tools,
ToolConfig? toolConfig}) {
return GenerativeModel._(
return createGenerativeModel(
model: model,
app: app,
appCheck: appCheck,
Expand Down
Loading

0 comments on commit 85a517f

Please sign in to comment.