postaStreamEventsGemma method
- BuildContext context, {
- required String input,
Sends a POST request to the Gemma API's RAG endpoint to generate events in a stream and yields the results as a stream of responses.
context
: The BuildContext used for localization.
input
: The input string to be sent to the API.
Yields different types of messages and results as the API processes the request in a stream mode.
Implementation
Stream<dynamic> postaStreamEventsGemma(BuildContext context,
{required String input}) async* {
final http.Client _client = http.Client();
final rag_url = Uri.parse('$baseUrl/rag/stream_events');
final rag_headers = {
'Content-Type': 'application/json',
'accept': 'text/event-stream',
};
final rag_body = json.encode({'input': input});
final request = http.Request('POST', rag_url)
..headers.addAll(rag_headers)
..body = rag_body;
if (!await isServerAvailable()) {
yield {
'type': 'error',
// 'data': 'The server is currently unavailable. Please try again later.'
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_serverNotAvailable
};
return;
}
final rag_response = await _client.send(request);
final stream = rag_response.stream;
Map<String, dynamic> outputStrJson;
List<PlacesModel> pois = [];
try {
await for (var event
in stream.transform(utf8.decoder).transform(LineSplitter())) {
if (event.startsWith('data: ')) {
String jsonData = event.substring(6);
Map<String, dynamic> jsonMap = jsonDecode(jsonData);
String chunk = '';
if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_chain_start') {
yield {
'type': 'message',
// 'data': "RAG (Retrieval Augmented Generation) Chain Starting ..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages1
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_retriever_start') {
yield {
'type': 'message',
// 'data': "Getting Retrieval ready..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages2
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_retriever_end') {
yield {
'type': 'message',
// 'data': "Retrieval Initialized ..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages3
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_prompt_start') {
yield {
'type': 'message',
// 'data': "Preparing Prompt ..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages4
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_prompt_end') {
yield {
'type': 'message',
// 'data': "Prompt Ready ..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages5
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_llm_start') {
yield {
'type': 'message',
// 'data': "Getting Gemma LLM Model ready..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages6
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_llm_stream') {
chunk = jsonMap['data']['chunk'];
yield {'type': 'chunk', 'data': chunk};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_llm_end') {
yield {
'type': 'message',
// 'data': "End of Chain ..."
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_progressMessages7
};
} else if (jsonMap.containsKey('event') &&
jsonMap['event'] == 'on_chain_end' &&
jsonMap['name'] == '/rag') {
// the output
outputStrJson = jsonMap['data']['output'];
List<dynamic> places = outputStrJson['places'];
for (int i = 0; i < places.length; i++) {
Map<String, dynamic> place = places[i];
String location =
'${place['name']}, ${place['address']}, ${place['city']}, ${place['country']}';
MyLatLng latlng =
await GeocodingService().getCoordinates(location);
PlacesModel poi = PlacesModel(
id: i + 1,
name: place['name'],
description: place['description'],
address: place['address'],
city: place['city'],
country: place['country'],
ratings: place['ratings'],
amenities: place['reviews'],
price: place['price'],
sourceLink: place['sourceLink'],
latitude: latlng.latitude,
longitude: latlng.longitude,
);
pois.add(poi);
}
yield {'type': 'result', 'data': pois};
}
}
}
} catch (e) {
yield {
'type': 'error',
// 'data': 'An error occurred while generating the response: $e'
'data': AppLocalizations.of(context)!
.aiGenerationAPIGemma_errorresponsegeneration(e.toString())
};
} finally {
_client.close();
}
}