conversation now shown in gui when manually refreshed.

This commit is contained in:
Joshua
2024-07-08 14:23:01 -06:00
parent ff4a54fbbc
commit 7547fe110f
2 changed files with 39 additions and 30 deletions
@@ -235,8 +235,7 @@ class _PromptBoxWidgetState extends State<PromptBoxWidget> {
} }
void updateFromLatestInput(String input) { void updateFromLatestInput(String input) {
Server s = Server(); Server().getResponseWhenReady(input);
s.setPrompt(input); // TODO: Refresh the page with the prompt & response.
print(s.getResponse());
} }
} }
+35 -25
View File
@@ -5,17 +5,33 @@ import 'package:app/components/conversation_thread/chat.dart';
import 'package:app/util/speech_to_text.dart'; import 'package:app/util/speech_to_text.dart';
// Uses Singleton design pattern to ensure that any server references across the app are using the same instance.
class Server { class Server {
// STT variables
static final SpeechToText stt = SpeechToText();
static late bool _speechEnabled;
static final Server _single_server = Server._internal();
// TODO: Verify all different components of STT, TTS, LLM, etc have been initialized.
// TODO: Handshake w/ server to validate identity
factory Server() {
// Speech-To-Text engine activation.
_speechEnabled = stt.isSpeechEnabled();
return _single_server;
}
Server._internal();
// TODO: One-time process to establish server connection is required for easy setup/maintenance. // TODO: One-time process to establish server connection is required for easy setup/maintenance.
// Server variables // Server variables
final url = "http://10.0.2.2:11434"; final url = "http://10.0.2.2:11434/api/";
String endpoint = "";
Map<String, String> headers = {'Content-Type': 'application/json'}; Map<String, String> headers = {'Content-Type': 'application/json'};
// Model variables
final SpeechToText stt = SpeechToText();
// Status variables // Status variables
late bool _speechEnabled;
bool _historyEnabled = true; bool _historyEnabled = true;
// Context variables // Context variables
@@ -29,14 +45,6 @@ class Server {
String response = ""; String response = "";
int conversationID = 0; int conversationID = 0;
Server() {
// Speech-To-Text engine activation.
_speechEnabled = stt.isSpeechEnabled();
// TODO: Initialize all different components of STT, TTS, LLM, etc.
// TODO: Handshake w/ server to validate identity
}
List<Chat> getConversationByID(conversationID) { List<Chat> getConversationByID(conversationID) {
// TODO: Implement conversation IDs & retrieval from server // TODO: Implement conversation IDs & retrieval from server
this.conversationID = conversationID; this.conversationID = conversationID;
@@ -61,7 +69,7 @@ class Server {
// TODO: In case on-device STT is unavailable, use server-side STT service. // TODO: In case on-device STT is unavailable, use server-side STT service.
if (!_speechEnabled) "Unable to process Speech-To-Text on-device."; if (!_speechEnabled) "Unable to process Speech-To-Text on-device.";
setPrompt(stt.getTextWhenReady() as String); prompt = stt.getTextWhenReady() as String;
httpSendRequest(); httpSendRequest();
return response; return response;
@@ -73,9 +81,6 @@ class Server {
return response; return response;
} }
// Arbitrary prompt, used for standard text interaction rather than STT.
void setPrompt(String p) => prompt = p;
// Get the last response or request a response from the server. // Get the last response or request a response from the server.
String getResponse() { String getResponse() {
httpSendRequest(); httpSendRequest();
@@ -94,6 +99,7 @@ class Server {
if(_historyEnabled) { if(_historyEnabled) {
chats = [...chats, Chat(prompt, true)]; chats = [...chats, Chat(prompt, true)];
endpoint = "chat";
List<Map<String, String>> messageList = convertMessages(); List<Map<String, String>> messageList = convertMessages();
@@ -104,6 +110,7 @@ class Server {
}; };
} }
else { else {
endpoint = "generate";
data = { data = {
"model": "llama3", "model": "llama3",
"prompt": prompt, "prompt": prompt,
@@ -118,11 +125,7 @@ class Server {
// Send the HTTP request to the server. // Send the HTTP request to the server.
void httpSendRequest() async { void httpSendRequest() async {
String json = constructPrompt(); String json = constructPrompt();
print("\n\n$json\n\n"); print("\n\nJSON:\n$json\n\n");
String endpoint = "/api/";
if(_historyEnabled) endpoint += "chat";
else endpoint += "generate";
// Send the HTTP POST request // Send the HTTP POST request
final serverResponse = await http.post( final serverResponse = await http.post(
@@ -133,10 +136,17 @@ class Server {
if (serverResponse.statusCode == 200) { if (serverResponse.statusCode == 200) {
Map<String, dynamic> re = jsonDecode(serverResponse.body); Map<String, dynamic> re = jsonDecode(serverResponse.body);
response = re["response"]!; if(_historyEnabled) {
response = re["message"]["content"];
chats = [...chats, Chat(response, false)];
} else { } else {
response = ('Error: ${serverResponse.statusCode}'); response = re["response"];
} }
chats.add(Chat(response, false)); }
print("\n\nresponse:\n$response");
print("\nRaw chats variable:\n");
for(Chat c in chats) {
print(c.content);
}
} }
} }