Skip to content

Commit

Permalink
remove an experimental 'gemini' call from the backend (#3091)
Browse files Browse the repository at this point in the history
* remove an experiemntal 'gemini' call from the backend

* update pub deps for the main channel

* check in logging code

* remove logging code
  • Loading branch information
devoncarew authored Nov 15, 2024
1 parent 7e4d3e2 commit f1f0748
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 114 deletions.
3 changes: 3 additions & 0 deletions pkgs/dart_services/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ config.properties
.vscode/

bin/server

# Any local environment variable configuration
.env
49 changes: 0 additions & 49 deletions pkgs/dart_services/lib/src/common_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:dartpad_shared/model.dart' as api;
import 'package:google_generative_ai/google_generative_ai.dart' as google_ai;
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart';
Expand Down Expand Up @@ -232,50 +231,6 @@ class CommonServerApi {
}
}

static final String? geminiApiKey = Platform.environment['GEMINI_API_KEY'];
http.Client? geminiHttpClient;

@Route.post('$apiPrefix/_gemini')
Future<Response> gemini(Request request, String apiVersion) async {
if (apiVersion != api3) return unhandledVersion(apiVersion);

// Read the api key from env variables (populated on the server).
final apiKey = geminiApiKey;
if (apiKey == null) {
return Response.internalServerError(
body: 'gemini key not configured on server');
}

// Only allow the call from dartpad.dev.
final origin = request.origin;
if (origin != 'https://dartpad.dev') {
return Response.badRequest(
body: 'Gemini calls only allowed from the DartPad front-end');
}

final sourceRequest =
api.SourceRequest.fromJson(await request.readAsJson());

geminiHttpClient ??= http.Client();

final model = google_ai.GenerativeModel(
model: 'models/gemini-1.5-flash-latest',
apiKey: apiKey,
httpClient: geminiHttpClient,
);

final result = await serialize(() async {
// call gemini
final result = await model.generateContent([
google_ai.Content.text(sourceRequest.source),
]);

return api.GeminiResponse(response: result.text!);
});

return ok(result.toJson());
}

Response ok(Map<String, dynamic> json) {
return Response.ok(
_jsonEncoder.convert(json),
Expand Down Expand Up @@ -403,7 +358,3 @@ String _formatMessage(

return message;
}

extension on Request {
String? get origin => headers['origin'];
}
5 changes: 0 additions & 5 deletions pkgs/dart_services/lib/src/common_server.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkgs/dart_services/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies:
bazel_worker: ^1.1.1
dartpad_shared: ^0.0.0
encrypt: ^5.0.3
google_generative_ai: ^0.4.6
http: ^1.2.1
json_annotation: ^4.9.0
logging: ^1.2.0
Expand Down
44 changes: 22 additions & 22 deletions pkgs/dart_services/tool/dependencies/pub_dependencies_main.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
"animations": "2.0.11",
"archive": "3.6.1",
"args": "2.6.0",
"async": "2.11.0",
"async": "2.12.0",
"basics": "0.10.0",
"bloc": "8.1.4",
"boolean_selector": "2.1.1",
"boolean_selector": "2.1.2",
"characters": "1.3.0",
"clock": "1.1.1",
"clock": "1.1.2",
"collection": "1.19.0",
"convert": "3.1.2",
"coverage": "1.10.0",
"coverage": "1.11.0",
"creator": "0.3.3",
"creator_core": "0.3.3",
"cross_file": "0.3.4+2",
"crypto": "3.0.6",
"csslib": "1.0.0",
"csslib": "1.0.2",
"dart_earcut": "1.1.0",
"dartz": "0.10.1",
"english_words": "4.0.0",
"equatable": "2.0.5",
"fake_async": "1.3.1",
"fast_immutable_collections": "10.2.4",
"fake_async": "1.3.2",
"fast_immutable_collections": "11.0.0",
"ffi": "2.1.3",
"file": "7.0.1",
"flame": "1.21.0",
Expand All @@ -37,15 +37,15 @@
"flutter_map": "7.0.2",
"flutter_markdown": "0.7.4+1",
"flutter_riverpod": "2.6.1",
"flutter_svg": "2.0.10+1",
"flutter_svg": "2.0.14",
"forge2d": "0.13.1",
"frontend_server_client": "4.0.0",
"glob": "2.1.2",
"go_router": "14.3.0",
"go_router": "14.6.0",
"google_fonts": "6.2.1",
"google_generative_ai": "0.4.6",
"hooks_riverpod": "2.6.1",
"html": "0.15.4",
"html": "0.15.5",
"http": "1.2.2",
"http_multi_server": "3.2.1",
"http_parser": "4.1.1",
Expand Down Expand Up @@ -73,9 +73,9 @@
"node_preamble": "2.0.2",
"ordered_set": "6.1.1",
"package_config": "2.1.0",
"path": "1.9.0",
"path_parsing": "1.0.2",
"path_provider": "2.1.4",
"path": "1.9.1",
"path_parsing": "1.1.0",
"path_provider": "2.1.5",
"path_provider_android": "2.2.12",
"path_provider_foundation": "2.4.0",
"path_provider_linux": "2.2.1",
Expand All @@ -94,7 +94,7 @@
"rohd": "0.5.3",
"rohd_vf": "0.5.0",
"rxdart": "0.28.0",
"shared_preferences": "2.3.2",
"shared_preferences": "2.3.3",
"shared_preferences_android": "2.3.3",
"shared_preferences_foundation": "2.5.3",
"shared_preferences_linux": "2.4.1",
Expand All @@ -114,33 +114,33 @@
"stack_trace": "1.12.0",
"state_notifier": "1.0.0",
"stream_channel": "2.1.2",
"string_scanner": "1.3.0",
"string_scanner": "1.4.0",
"term_glyph": "1.2.1",
"test": "1.25.8",
"test_api": "0.7.3",
"test_core": "0.6.5",
"tiled": "0.10.2",
"timezone": "0.9.4",
"timezone": "0.10.0",
"typed_data": "1.4.0",
"unicode": "0.3.1",
"url_launcher": "6.3.1",
"url_launcher_android": "6.3.13",
"url_launcher_android": "6.3.14",
"url_launcher_ios": "6.3.1",
"url_launcher_linux": "3.2.0",
"url_launcher_linux": "3.2.1",
"url_launcher_macos": "3.2.1",
"url_launcher_platform_interface": "2.3.2",
"url_launcher_web": "2.3.3",
"url_launcher_windows": "3.1.3",
"vector_graphics": "1.1.11+1",
"vector_graphics_codec": "1.1.11+1",
"vector_graphics_compiler": "1.1.11+1",
"vector_graphics": "1.1.14",
"vector_graphics_codec": "1.1.12",
"vector_graphics_compiler": "1.1.15",
"vector_math": "2.1.4",
"video_player": "2.9.2",
"video_player_android": "2.7.16",
"video_player_avfoundation": "2.6.2",
"video_player_platform_interface": "6.2.3",
"video_player_web": "2.3.3",
"vm_service": "14.3.0",
"vm_service": "14.3.1",
"watcher": "1.1.0",
"web": "1.1.0",
"web_socket": "0.1.6",
Expand Down
17 changes: 0 additions & 17 deletions pkgs/dartpad_shared/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,6 @@ class VersionResponse {
Map<String, Object?> toJson() => _$VersionResponseToJson(this);
}

@JsonSerializable()
class GeminiResponse {
final String response;

GeminiResponse({
required this.response,
});

factory GeminiResponse.fromJson(Map<String, Object?> json) =>
_$GeminiResponseFromJson(json);

Map<String, Object?> toJson() => _$GeminiResponseToJson(this);

@override
String toString() => 'GeminiResponse[response=$response]';
}

@JsonSerializable()
class OpenInIdxRequest {
final String code;
Expand Down
10 changes: 0 additions & 10 deletions pkgs/dartpad_shared/lib/model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions pkgs/dartpad_shared/lib/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:meta/meta.dart';

import 'model.dart';

Expand Down Expand Up @@ -44,11 +43,6 @@ class ServicesClient {
Future<OpenInIdxResponse> openInIdx(OpenInIdxRequest request) =>
_requestPost('openInIDX', request.toJson(), OpenInIdxResponse.fromJson);

/// Note: this API is experimental and could change or be removed at any time.
@experimental
Future<GeminiResponse> gemini(SourceRequest request) =>
_requestPost('_gemini', request.toJson(), GeminiResponse.fromJson);

void dispose() => client.close();

Future<T> _requestGet<T>(
Expand Down
4 changes: 0 additions & 4 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,6 @@ class AppServices {
return await services.document(request);
}

Future<GeminiResponse> gemini(SourceRequest request) async {
return await services.gemini(request);
}

Future<CompileResponse> compile(CompileRequest request) async {
try {
appModel.compilingBusy.value = true;
Expand Down

0 comments on commit f1f0748

Please sign in to comment.