Skip to content

Commit

Permalink
refactor(vertex_ai): Require http.Client instead of AuthClient (#156
Browse files Browse the repository at this point in the history
)

Currently all VertexAI wrapper require a `AuthHttp` instance from `googleapis_auth` package. However, you may want to use a proxy server instead of calling directly VertexAI. Requiring an `http.Client` instead of  `AuthClient` gives you the flexibility to do that. As you can create your own wrapper of `http.Client` to handle the authentication of your proxy.
  • Loading branch information
davidmigloz authored Sep 8, 2023
1 parent 86034c8 commit 0f7fee7
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main(final List<String> arguments) async {
// Get Vertex AI client
print('\n> Creating client...');
final marchingEngine = VertexAIMatchingEngineClient(
authHttpClient: authClient,
httpClient: authClient,
project: projectId,
location: projectLocation,
);
Expand Down Expand Up @@ -127,7 +127,7 @@ void main(final List<String> arguments) async {
print('You can now use it in LangChain.dart:');
print('''
final vectorStore = VertexAIMatchingEngine(
authHttpClient: authClient,
httpClient: authClient,
project: '$projectId',
location: '$projectLocation',
queryRootUrl: 'http://${indexEndpoint.publicEndpointDomainName}/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() async {
/// The most basic building block of LangChain is calling an LLM on some input.
Future<void> _example1() async {
final openai = VertexAI(
authHttpClient: await _getAuthHttpClient(),
httpClient: await _getAuthHttpClient(),
project: _getProjectId(),
defaultOptions: const VertexAIOptions(
temperature: 0.9,
Expand All @@ -29,7 +29,7 @@ Future<void> _example1() async {
/// This is the most basic one.
Future<void> _example2() async {
final chat = ChatVertexAI(
authHttpClient: await _getAuthHttpClient(),
httpClient: await _getAuthHttpClient(),
project: _getProjectId(),
defaultOptions: const ChatVertexAIOptions(
temperature: 0,
Expand Down
23 changes: 11 additions & 12 deletions packages/langchain_google/lib/src/chat_models/vertex_ai.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart' as http;
import 'package:langchain/langchain.dart';
import 'package:tiktoken/tiktoken.dart';
import 'package:vertex_ai/vertex_ai.dart';
Expand Down Expand Up @@ -30,12 +30,11 @@ import 'models/models.dart';
///
/// ### Authentication
///
/// The `ChatVertexAI` wrapper delegates authentication to the
/// [googleapis_auth](https://pub.dev/packages/googleapis_auth) package.
///
/// To create an instance of `ChatVertexAI` you need to provide an
/// [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
/// instance.
/// HTTP client that handles authentication. The easiest way to do this is to
/// use [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
/// from the [googleapis_auth](https://pub.dev/packages/googleapis_auth)
/// package.
///
/// There are several ways to obtain an `AuthClient` depending on your use case.
/// Check out the [googleapis_auth](https://pub.dev/packages/googleapis_auth)
Expand All @@ -52,7 +51,7 @@ import 'models/models.dart';
/// [ChatVertexAI.cloudPlatformScope],
/// );
/// final chatVertexAi = ChatVertexAI(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// );
/// ```
Expand Down Expand Up @@ -95,7 +94,7 @@ import 'models/models.dart';
/// Example:
/// ```dart
/// final chatModel = ChatVertexAI(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// defaultOptions: ChatVertexAIOptions(
/// temperature: 0.9,
Expand All @@ -112,18 +111,18 @@ import 'models/models.dart';
class ChatVertexAI extends BaseChatModel<ChatVertexAIOptions> {
/// {@macro chat_vertex_ai}
ChatVertexAI({
required final AuthClient authHttpClient,
required final http.Client httpClient,
required final String project,
final String location = 'us-central1',
final String rootUrl = 'https://us-central1-aiplatform.googleapis.com/',
final String? rootUrl,
this.publisher = 'google',
this.model = 'chat-bison',
this.defaultOptions = const ChatVertexAIOptions(),
}) : client = VertexAIGenAIClient(
authHttpClient: authHttpClient,
httpClient: httpClient,
project: project,
location: location,
rootUrl: rootUrl,
rootUrl: rootUrl ?? 'https://$location-aiplatform.googleapis.com/',
);

/// A client for interacting with Vertex AI API.
Expand Down
23 changes: 13 additions & 10 deletions packages/langchain_google/lib/src/embeddings/vertex_ai.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart' as http;
import 'package:langchain/langchain.dart';
import 'package:vertex_ai/vertex_ai.dart';

Expand All @@ -8,7 +8,7 @@ import 'package:vertex_ai/vertex_ai.dart';
/// Example:
/// ```dart
/// final embeddings = VertexAIEmbeddings(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// );
/// final result = await embeddings.embedQuery('Hello world');
Expand All @@ -26,8 +26,11 @@ import 'package:vertex_ai/vertex_ai.dart';
///
/// ### Authentication
///
/// The `VertexAIEmbeddings` wrapper delegates authentication to the
/// [googleapis_auth](https://pub.dev/packages/googleapis_auth) package.
/// To create an instance of `VertexAIEmbeddings` you need to provide an
/// HTTP client that handles authentication. The easiest way to do this is to
/// use [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
/// from the [googleapis_auth](https://pub.dev/packages/googleapis_auth)
/// package.
///
/// To create an instance of `VertexAIEmbeddings` you need to provide an
/// [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
Expand All @@ -48,7 +51,7 @@ import 'package:vertex_ai/vertex_ai.dart';
/// [VertexAIEmbeddings.cloudPlatformScope],
/// );
/// final vertexAi = VertexAIEmbeddings(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// );
/// ```
Expand Down Expand Up @@ -99,7 +102,7 @@ import 'package:vertex_ai/vertex_ai.dart';
/// Example:
/// ```dart
/// final embeddings = VertexAIEmbeddings(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// docTitleKey: 'title',
/// );
Expand All @@ -114,19 +117,19 @@ import 'package:vertex_ai/vertex_ai.dart';
class VertexAIEmbeddings implements Embeddings {
/// {@macro vertex_ai_embeddings}
VertexAIEmbeddings({
required final AuthClient authHttpClient,
required final http.Client httpClient,
required final String project,
final String location = 'us-central1',
final String rootUrl = 'https://us-central1-aiplatform.googleapis.com/',
final String? rootUrl,
this.publisher = 'google',
this.model = 'textembedding-gecko',
this.batchSize = 5,
this.docTitleKey = 'title',
}) : client = VertexAIGenAIClient(
authHttpClient: authHttpClient,
httpClient: httpClient,
project: project,
location: location,
rootUrl: rootUrl,
rootUrl: rootUrl ?? 'https://$location-aiplatform.googleapis.com/',
);

/// A client for interacting with Vertex AI API.
Expand Down
17 changes: 10 additions & 7 deletions packages/langchain_google/lib/src/llms/vertex_ai.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart' as http;
import 'package:langchain/langchain.dart';
import 'package:tiktoken/tiktoken.dart';
import 'package:vertex_ai/vertex_ai.dart';
Expand Down Expand Up @@ -30,8 +30,11 @@ import 'models/models.dart';
///
/// ### Authentication
///
/// The `VertexAI` wrapper delegates authentication to the
/// [googleapis_auth](https://pub.dev/packages/googleapis_auth) package.
/// To create an instance of `VertexAI` you need to provide an
/// HTTP client that handles authentication. The easiest way to do this is to
/// use [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
/// from the [googleapis_auth](https://pub.dev/packages/googleapis_auth)
/// package.
///
/// To create an instance of `VertexAI` you need to provide an
/// [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
Expand All @@ -52,7 +55,7 @@ import 'models/models.dart';
/// [VertexAI.cloudPlatformScope],
/// );
/// final vertexAi = VertexAI(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// );
/// ```
Expand Down Expand Up @@ -93,7 +96,7 @@ import 'models/models.dart';
/// Example:
/// ```dart
/// final llm = VertexAI(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// defaultOptions: VertexAIOptions(
/// temperature: 0.9,
Expand All @@ -110,15 +113,15 @@ import 'models/models.dart';
class VertexAI extends BaseLLM<VertexAIOptions> {
/// {@macro vertex_ai}
VertexAI({
required final AuthClient authHttpClient,
required final http.Client httpClient,
required final String project,
final String location = 'us-central1',
final String? rootUrl,
this.publisher = 'google',
this.model = 'text-bison',
this.defaultOptions = const VertexAIOptions(),
}) : client = VertexAIGenAIClient(
authHttpClient: authHttpClient,
httpClient: httpClient,
project: project,
location: location,
rootUrl: rootUrl ?? 'https://$location-aiplatform.googleapis.com/',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:convert';

import 'package:gcloud/storage.dart';
import 'package:googleapis_auth/googleapis_auth.dart';
import 'package:http/http.dart' as http;
import 'package:langchain/langchain.dart';
import 'package:uuid/uuid.dart';
import 'package:vertex_ai/vertex_ai.dart';
Expand Down Expand Up @@ -46,12 +46,11 @@ import 'models/models.dart';
///
/// ### Authentication
///
/// The `VertexAIMatchingEngine` wrapper delegates authentication to the
/// [googleapis_auth](https://pub.dev/packages/googleapis_auth) package.
///
/// To create an instance of `VertexAIMatchingEngine` you need to provide an
/// [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
/// instance.
/// HTTP client that handles authentication. The easiest way to do this is to
/// use [`AuthClient`](https://pub.dev/documentation/googleapis_auth/latest/googleapis_auth/AuthClient-class.html)
/// from the [googleapis_auth](https://pub.dev/packages/googleapis_auth)
/// package.
///
/// There are several ways to obtain an `AuthClient` depending on your use case.
/// Check out the [googleapis_auth](https://pub.dev/packages/googleapis_auth)
Expand All @@ -68,7 +67,7 @@ import 'models/models.dart';
/// VertexAIMatchingEngine.cloudPlatformScopes,
/// );
/// final vectorStore = VertexAIMatchingEngine(
/// authHttpClient: authClient,
/// httpClient: authClient,
/// project: 'your-project-id',
/// location: 'europe-west1',
/// indexId: 'your-index-id',
Expand Down Expand Up @@ -150,7 +149,7 @@ import 'models/models.dart';
class VertexAIMatchingEngine extends VectorStore {
/// Creates a new Vertex AI Matching Engine vector store.
///
/// - [authHttpClient] An authenticated HTTP client.
/// - [httpClient] An authenticated HTTP client.
/// - [project] The ID of the Google Cloud project to use.
/// - [location] The Google Cloud location to use. Vertex AI and
/// Cloud Storage should have the same location.
Expand All @@ -175,7 +174,7 @@ class VertexAIMatchingEngine extends VectorStore {
/// the index, the previous ones are deleted. If false, the new vectors are
/// added to the index without deleting the previous ones.
VertexAIMatchingEngine({
required final AuthClient authHttpClient,
required final http.Client httpClient,
required this.project,
required this.location,
final String? rootUrl,
Expand All @@ -188,20 +187,20 @@ class VertexAIMatchingEngine extends VectorStore {
this.gcsIndexesFolder = 'indexes',
this.completeOverwriteWhenAdding = false,
required super.embeddings,
}) : _authHttpClient = authHttpClient,
}) : _httpClient = httpClient,
_managementClient = VertexAIMatchingEngineClient(
authHttpClient: authHttpClient,
httpClient: httpClient,
project: project,
location: location,
rootUrl: rootUrl ?? 'https://$location-aiplatform.googleapis.com/',
),
_storageClient = Storage(authHttpClient, project),
_storageClient = Storage(httpClient, project),
_queryRootUrl = queryRootUrl,
_indexEndpointId = indexEndpointId,
_deployedIndexId = deployedIndexId;

/// An authenticated HTTP client.
final AuthClient _authHttpClient;
final http.Client _httpClient;

/// A client for querying the Vertex AI Matching Engine index.
VertexAIMatchingEngineClient? _queryClient;
Expand Down Expand Up @@ -408,7 +407,7 @@ class VertexAIMatchingEngine extends VectorStore {
/// Returns a client for querying the Vertex AI Matching Engine index.
VertexAIMatchingEngineClient _getQueryClient(final String queryRootUrl) {
return _queryClient ??= VertexAIMatchingEngineClient(
authHttpClient: _authHttpClient,
httpClient: _httpClient,
project: project,
location: location,
rootUrl: queryRootUrl,
Expand Down
Loading

0 comments on commit 0f7fee7

Please sign in to comment.