Skip to content

Commit

Permalink
feat: added chat functionality, user will get answers based on old co…
Browse files Browse the repository at this point in the history
…nversation
  • Loading branch information
vineyrawat committed Feb 28, 2024
1 parent 875763f commit 525b914
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
20 changes: 12 additions & 8 deletions lib/providers/gemini_chat_provider.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_gemini/flutter_gemini.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:convogen/providers/app_settings_provider.dart';
import 'package:google_gemini/google_gemini.dart';

var geminiChatProvider =
StateNotifierProvider<GeminiChatProvider, GeminiChatState>(
Expand All @@ -19,6 +17,7 @@ class GeminiChatState {
types.User(id: '1', firstName: 'Gemini', role: types.Role.user),
types.User(id: '2', firstName: 'User', role: types.Role.user)
];

const GeminiChatState(
{required this.isLoading,
required this.messages,
Expand Down Expand Up @@ -82,14 +81,19 @@ class GeminiChatProvider extends StateNotifier<GeminiChatState> {
text: prompt,
createdAt: DateTime.now().millisecondsSinceEpoch));
state = state.copyWith(isTyping: true);
var gemini =
GoogleGemini(apiKey: ref.read(appSettingsProvider).geminiApiKey);
var res = await gemini.generateFromText(prompt);
log(res.toString());

var chats = state.messages.map((dynamic e) => Content(
parts: [Parts(text: e.text)],
role: e.author == state.users[1] ? 'model' : 'user'));
var flutterGemini =
Gemini.init(apiKey: ref.read(appSettingsProvider).geminiApiKey);

var res = await flutterGemini.chat(chats.toList().reversed.toList());
addMessage(types.TextMessage(
author: state.users[1],
id: DateTime.now().toString(),
text: res.text,
// text: res!.content!.parts!.map((e) => e.text).join("\n"),
text: res?.output ?? "Unable to proceed",
createdAt: DateTime.now().millisecondsSinceEpoch));
state = state.copyWith(isTyping: false);
}
Expand Down
24 changes: 16 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
dio:
dependency: transitive
description:
name: dio
sha256: "49af28382aefc53562459104f64d16b9dfd1e8ef68c862d5af436cc8356ce5a8"
url: "https://pub.dev"
source: hosted
version: "5.4.1"
equatable:
dependency: transitive
description:
Expand Down Expand Up @@ -342,6 +350,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.0"
flutter_gemini:
dependency: "direct main"
description:
name: flutter_gemini
sha256: "1e106f4644d1bc3b1806c9fbf0b6399873b16041d61ea12e3fc229f23f3aa067"
url: "https://pub.dev"
source: hosted
version: "2.0.3"
flutter_link_previewer:
dependency: transitive
description:
Expand Down Expand Up @@ -445,14 +461,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.1.0"
google_gemini:
dependency: "direct main"
description:
name: google_gemini
sha256: "8cf19c909e5bfb0518391f4d538251f29e3283caa021249b76afcd69b2963e26"
url: "https://pub.dev"
source: hosted
version: "0.0.9"
graphs:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: convogen
description: "A new Flutter project."
description: "A AI Chatbot based on Google's Gemini."
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: "none" # Remove this line if you wish to publish to pub.dev
Expand All @@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+2
version: 1.0.1

environment:
sdk: ">=3.2.3 <4.0.0"
Expand All @@ -37,7 +37,6 @@ dependencies:
flutter_chat_ui: ^1.6.10
flutter_chat_types: ^3.6.2
provider: ^6.1.1
google_gemini: ^0.0.9
flutter_riverpod: ^2.4.9
riverpod_annotation: ^2.3.3
shared_preferences: ^2.2.2
Expand All @@ -49,6 +48,7 @@ dependencies:
google_fonts: ^6.1.0
flutter_svg: ^2.0.9
go_router: ^13.0.0
flutter_gemini: ^2.0.3

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 525b914

Please sign in to comment.