npc talk
This commit is contained in:
@@ -40,11 +40,16 @@ namespace Hallucinate.AI
|
||||
{
|
||||
var jsonBody = $@"{{
|
||||
""systemInstruction"": {{""parts"": [{{ ""text"": ""{persona}"" }}]}},
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}]
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}],
|
||||
""generationConfig"": {{
|
||||
""maxOutputTokens"": 60,
|
||||
""temperature"": 0.7
|
||||
}}
|
||||
}}";
|
||||
|
||||
var requestURL = $"{geminiURL}?key={apiKey}";
|
||||
|
||||
|
||||
using (var request = new UnityWebRequest(requestURL, "POST"))
|
||||
{
|
||||
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
|
||||
@@ -56,15 +61,28 @@ namespace Hallucinate.AI
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
string rawResponse = request.downloadHandler.text;
|
||||
try
|
||||
{
|
||||
var response = JsonUtility.FromJson<GeminiResponse>(request.downloadHandler.text);
|
||||
if (response?.candidates != null && response.candidates.Length > 0)
|
||||
var response = JsonUtility.FromJson<GeminiResponse>(rawResponse);
|
||||
if (response != null &&
|
||||
response.candidates != null &&
|
||||
response.candidates.Length > 0 &&
|
||||
response.candidates[0].content != null &&
|
||||
response.candidates[0].content.parts != null &&
|
||||
response.candidates[0].content.parts.Length > 0)
|
||||
{
|
||||
onComplete?.Invoke(response.candidates[0].content.parts[0].text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[Gemini] Response structure invalid or blocked. Raw: {rawResponse}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}\nRaw Response: {rawResponse}");
|
||||
}
|
||||
catch (Exception e) { Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}"); }
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user