Update
This commit is contained in:
@@ -1,44 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.Networking;
|
||||
using System.Collections;
|
||||
using Hallucinate.Audio;
|
||||
|
||||
[Serializable]
|
||||
public class Part
|
||||
{
|
||||
public string text;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Content
|
||||
{
|
||||
public Part[] parts;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Candidate
|
||||
{
|
||||
public Content content;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class GeminiResponse
|
||||
{
|
||||
public Candidate[] candidates;
|
||||
}
|
||||
using Hallucinate.AI;
|
||||
|
||||
public class GerminiNPC : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private string apiKey = "AQ.Ab8RN6I2hU_p8yHiPNNHtWzYBiLugbPP22gC6lzTWaYEWj4v0g";
|
||||
[SerializeField]
|
||||
private string germiniURL =
|
||||
"https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent";
|
||||
|
||||
public string npcPersona =
|
||||
private string npcPersona =
|
||||
"Ngươi là một lão thợ rèn cọc cằn tên là Tom, ngươi rất ghét những kẻ mang phế liệu đến tiệm của mình. Chỉ trả lời ngắn gọn trong 2 câu, theo phong cách trung cổ.";
|
||||
|
||||
public string playerHeldItem = "Thanh kiếm rỉ sét";
|
||||
@@ -95,46 +64,17 @@ public class GerminiNPC : MonoBehaviour
|
||||
|
||||
private IEnumerator GetGerminiReponse()
|
||||
{
|
||||
var jsonBody = $@"{{
|
||||
""systemInstruction"": {{""parts"": [{{ ""text"": ""{npcPersona}"" }}]}},
|
||||
""contents"": [{{""parts"": [{{ ""text"": ""Ta muốn bán cho ông món đồ này: {playerHeldItem}""}}]}}]
|
||||
}}";
|
||||
string prompt = $"Ta muốn bán cho ông món đồ này: {playerHeldItem}";
|
||||
|
||||
Hallucinate.AI.GeminiService.Instance.GetResponse(npcPersona, prompt, (response) => {
|
||||
Debug.Log($"<color=green>Tom:</color> {response}");
|
||||
AudioManager.Instance?.Play(responseSound, position: transform.position);
|
||||
|
||||
// Nếu có ChatBubble gắn kèm thì hiển thị luôn
|
||||
var bubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
|
||||
if (bubble != null) bubble.Show(response);
|
||||
});
|
||||
|
||||
// 1. Sửa tham số thành ?key= (trước đó là ?ket=)
|
||||
var requestURL = $"{germiniURL}?key={apiKey}";
|
||||
|
||||
// 2. Sử dụng requestURL (có chứa key) thay vì germiniURL gốc
|
||||
using (var request = new UnityWebRequest(requestURL, "POST"))
|
||||
{
|
||||
var bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
|
||||
{
|
||||
Debug.LogError($"[Gemini Error] {request.error} - Response: {request.downloadHandler.text}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var responseTEXT = request.downloadHandler.text;
|
||||
try
|
||||
{
|
||||
var geminiResponse = JsonUtility.FromJson<GeminiResponse>(responseTEXT);
|
||||
if (geminiResponse != null && geminiResponse.candidates != null && geminiResponse.candidates.Length > 0)
|
||||
{
|
||||
var npcResponse = geminiResponse.candidates[0].content.parts[0].text;
|
||||
Debug.Log($"<color=green>Tom:</color> {npcResponse}");
|
||||
AudioManager.Instance?.Play(responseSound, position: transform.position);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"[JSON Parse Error] {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user