Open Akshaykushawaha opened 1 year ago
Hey @Akshaykushawaha, please provide more code so I can get a better idea of what we're dealing with here.
The generateText
function is a static function inside of the GenerativeLanguageAPI
class.
You can use it like so:
const request = GenerateTextRequest(
prompt: TextPrompt(text: 'Write a story about a magic backpack.'),
temperature: 1.0,
candidateCount: 2,
);
final response = await GenerativeLanguageAPI.generateText(
modelName: textModel,
request: request,
apiKey: apiKey,
);
print(const JsonEncoder.withIndent(' ').convert(response));
Please make sure to import the package by adding this line at the top of your dart file like so:
import 'package:google_generative_language_api/google_generative_language_api.dart';
Hey, thankyou for the quick response, as i said, you can use the example code on the website given and reproduce the error, either way heres' my code:
import 'package:flutter/material.dart';
import 'package:google_generative_language_api/google_generative_language_api.dart';
class ChatMessage {
final String text;
final bool isUser;
ChatMessage(this.text, this.isUser);
}
class ChatPage extends StatefulWidget {
@override
State<ChatPage> createState() => _ChatPageState();
}
class _ChatPageState extends State<ChatPage> {
final List<ChatMessage> _chatMessages = [];
final TextEditingController _messageController = TextEditingController();
void _sendMessage(String message) {
if (message.isNotEmpty) {
setState(() {
_chatMessages.add(ChatMessage(message, true));
});
// Simulate sending the user message to the AI backend
_getAIResponse(message);
}
}
Future<void> _getAIResponse(String userMessage) async {
// Replace 'PALM_API_KEY' with your actual Generative Language API key
final request = GenerateTextRequest(
prompt: TextPrompt(text: userMessage),
temperature: 1.0,
candidateCount: 2,
);
// Call the Generative Language API to generate text
final response = await GenerativeLanguageAPI.generateText(
modelName: 'models/text-bison-001',
request: request,
apiKey: 'YOUR_GENERATIVE_LANGUAGE_API_KEY',
);
// Process the AI-generated response
final aiResponse = response.candidates
.map((candidate) => candidate.output)
.join('\n');
setState(() {
_chatMessages.add(ChatMessage(aiResponse, false));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AI Chatbot'),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: _chatMessages.length,
itemBuilder: (context, index) {
final chatMessage = _chatMessages[index];
return ListTile(
title: Text(chatMessage.text),
subtitle: chatMessage.isUser ? Text('User') : Text('AI'),
);
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Expanded(
child: TextField(
controller: _messageController,
decoration: InputDecoration(labelText: 'Enter your message'),
),
),
IconButton(
icon: Icon(Icons.send),
onPressed: () {
_sendMessage(_messageController.text);
_messageController.clear();
},
),
],
),
),
],
),
);
}
}
i am getting the same issue with your code as well! :'(
Error: The method 'GenerateTextRequest' isn't defined for the type '_ChatPageState'.
Error: The method 'TextPrompt' isn't defined for the type '_ChatPageState'.
Error: The method 'generateText' isn't defined for the type 'GenerativeLanguageAPI'.
can you give me some sample code thats working for you?
@Akshaykushawaha Your code works perfectly for me:
Did you run flutter pub get
? Are you seeing any errors in your editor?
The issue is reproducible with the given example on the github repo and the official website for the code to "generate text":
final request = GenerateTextRequest( prompt: TextPrompt(text: userMessage), temperature: 1.0, candidateCount: 2, );
Error: The method 'GenerateTextRequest' isn't defined for the type '_ChatPageState'. The method 'generateText' isn't defined for the type 'GenerativeLanguageAPI'.