Created
February 6, 2025 18:05
-
-
Save Pipe38/f32738cacf4fa84a17a2013b4f006faa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LLMUnity; | |
using System.Collections; | |
using System.Collections.Generic; | |
using TMPro; | |
using UnityEngine; | |
using UnityEngine.Events; | |
public class TestCallFunction : MonoBehaviour | |
{ | |
[Header("UI References:")] | |
public LLMCharacter MyCharacter; | |
public TMP_InputField MyInputField; | |
public TMP_Text TextResponse; | |
[Header("AI Configurations:")] | |
public AiMultipleChoiceEvent[] MultipleChoiceEvents; | |
[Space(20)] | |
public UnityEvent OffTopicEvent; | |
string aiResponseString; | |
private void Start() | |
{ | |
// Linking the prompt computation method to the end edit ui event of the input field | |
MyInputField.onEndEdit.AddListener(TriggerLLM); | |
// Updating the vocabulary of the LLM, to answer only with the possible choices. | |
MyCharacter.grammarString = MultipleChoiceGrammar(); | |
} | |
public void TriggerLLM(string inputString) | |
{ | |
_ = MyCharacter.Chat(ConstructPrompt(inputString), AiResponse, ResponseCompleted); | |
MyInputField.text = string.Empty; | |
} | |
void AiResponse(string inResponse) | |
{ | |
TextResponse.text = inResponse; | |
aiResponseString = inResponse; | |
} | |
void ResponseCompleted() | |
{ | |
print("Risposta Completa, risultato: " + aiResponseString); | |
GetUnityEventFromTopic(aiResponseString).Invoke(); | |
} | |
UnityEvent GetUnityEventFromTopic(string inTopic) | |
{ | |
for (int i = 0; i < MultipleChoiceEvents.Length; i++) | |
{ | |
if (inTopic == MultipleChoiceEvents[i].TopicString) | |
{ | |
return MultipleChoiceEvents[i].AiTriggeredEvent; | |
break; | |
} | |
else if (inTopic == "OffTopic") | |
{ | |
return OffTopicEvent; | |
} | |
} | |
return null; | |
} | |
string ConstructPrompt(string message) | |
{ | |
string prompt = "Which of the following choices matches best the input?\n\n"; | |
prompt += "Input:" + message + "\n\n"; | |
prompt += "Choices:\n"; | |
for (int i = 0; i < MultipleChoiceEvents.Length; i++) | |
{ | |
prompt += "- "; | |
prompt += MultipleChoiceEvents[i].TopicString; | |
prompt += "\n"; | |
} | |
prompt += "\n"; | |
prompt += "If none of the previous choices matches enough the input, the answer would be:\n"; | |
prompt += "- OffTopic"; | |
prompt += "\nAnswer directly with the choice"; | |
return prompt; | |
} | |
string MultipleChoiceGrammar() | |
{ | |
return "root ::= (\"" + string.Join("\" | \"", GetFunctionNames()) + "\")"; | |
} | |
string[] GetFunctionNames() | |
{ | |
// Create an array of names, adding a space for the off-topic | |
string[] multipleChoiceNames = new string[MultipleChoiceEvents.Length + 1]; | |
// Add all the topics to the array, leaving the space empty for the offtopic, so all choices - 1 | |
for (int i = 0; i < multipleChoiceNames.Length - 1 ; i++) | |
{ | |
multipleChoiceNames[i] = MultipleChoiceEvents[i].TopicString; | |
} | |
// Adding also the offtopic choice at the end of the array | |
multipleChoiceNames[multipleChoiceNames.Length - 1] = "OffTopic"; | |
return multipleChoiceNames; | |
} | |
} | |
[System.Serializable] | |
public class AiMultipleChoiceEvent | |
{ | |
public string TopicString; | |
public UnityEvent AiTriggeredEvent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment