Evaluation API Response Issue

I am getting an error message when trying out the example prompt from [Evaluation API | Documentation]

{"ERROR": "Error in evaluating the given conversation 400 Bad Request: Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)", "Reference ID": "5b14d9d1-c8f8-44bd-abfb-d78c917b58b1"}

Hello @Dave_Flores,

Welcome to the Convai Developer Forum!

Could you please share the code? And which Convai plan do you have?

Hello @K3 , Here’s what I’ve used and am using a solutions partner account.

public class ExecuteEvaluation : MonoBehaviour
{
    public static ExecuteEvaluation instance;
    public ConvaiNPC npc;
    [Multiline]
    public string prompt;
    public string sessionID;

    void Start()
    {
        npc = GetComponent<ConvaiNPC>();
        ExecuteEvaluation.instance = this;
    }
    private void Update()
    {
            sessionID = npc.sessionID;
    }
    public IEnumerator Evaluate()
    {
        UnityWebRequest request = new UnityWebRequest("https://api.convai.com/character/evaluate_conversation", "POST");
     
        WWWForm form = new WWWForm();
        form.AddField("CONVAI-API-KEY", "*****");
        form.AddField("session_id", sessionID);
        form.AddField("character_id", npc.characterID);
        form.AddField("prompt", prompt);

        request = UnityWebRequest.Post("https://api.convai.com/character/evaluate_conversation", form);
        request.SetRequestHeader("CONVAI-API-KEY", "*****");
        request.SetRequestHeader("Content-Type", "application/json");

        yield return request.SendWebRequest();

        if (request.result != UnityWebRequest.Result.Success)
        {
            Debug.LogError(request.error);
            Debug.Log(request.downloadHandler.text);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
    }
}
1 Like

Hello @Dave_Flores,

Here is a sample script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json; // Make sure to install Newtonsoft.Json from the Unity Package Manager

public class EvaluateConversation : MonoBehaviour
{
    // API endpoint for evaluation
    private const string EVALUATION_URL = "https://api.convai.com/character/evaluate_conversation";

    // Replace these with your actual API key and conversation data
    public string sessionId = "<SESSION ID>";
    public string characterId = "<CHARACTER ID>";
    public string customPrompt = "<YOUR CUSTOM PROMPT>";

    private string _apiKey;

    void Start()
    {
        if (ConvaiAPIKeySetup.GetAPIKey(out string apiKey))
        {
            _apiKey = apiKey;
        }
        else return;

        StartCoroutine(Evaluate());
    }

    IEnumerator Evaluate()
    {
        // Define item details as a list of dictionaries
        List<Dictionary<string, object>> items = new List<Dictionary<string, object>>()
        {
            new Dictionary<string, object> { {"item", "Burger"}, {"price", "$4"}, {"quantity", 3} },
            new Dictionary<string, object> { {"item", "Fries"}, {"price", "$2"}, {"quantity", 2} }
        };

        // Create the variables dictionary
        var variables = new Dictionary<string, object>
        {
            { "customer_name", "Suzie Denver" },
            { "customer_age", "54 years" },
            { "item_details", JsonConvert.SerializeObject(items) }
        };

        // Create the payload
        var payload = new Dictionary<string, object>
        {
            { "session_id", sessionId },
            { "character_id", characterId },
            { "prompt", customPrompt },
            { "variables", variables }
        };

        string jsonPayload = JsonConvert.SerializeObject(payload);

        // Create the request
        UnityWebRequest request = new UnityWebRequest(EVALUATION_URL, "POST");
        byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonPayload);
        request.uploadHandler = new UploadHandlerRaw(bodyRaw);
        request.downloadHandler = new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("CONVAI-API-KEY", _apiKey);

        // Send the request and wait for a response
        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.Success)
        {
            try
            {
                var response = JsonConvert.DeserializeObject<Dictionary<string, object>>(request.downloadHandler.text);
                if (response.ContainsKey("evaluation"))
                {
                    Debug.Log("Evaluation: " + response["evaluation"]);
                }
                else
                {
                    Debug.LogError("Evaluation key not found in response: " + request.downloadHandler.text);
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError("Error parsing response: " + e.Message);
            }
        }
        else
        {
            Debug.LogError("Request failed: " + request.error);
        }
    }
}

Thank you for the response @K3. This works with some changes to fit my needs. However, when I remove the values for variables, I am getting a Request failed: HTTP/1.1 500 Internal Server Error Error. Based on Evaluation API | Documentation, this is optional.

Could you please share the code?

This is the same code you shared but I changed { "variables", variables } to { "variables", ""}.

1 Like

Hy @Dave, sorry for the inconvenience. The documentation for the evaluation was not properly updated with the requirements. We have updated the details.
If you refer to the sample prompt in the doc, you will see a [[conversation_history]] variable which is compulsory to be sent. Not sending that , will return a 500 Server Error with an error message.

If you can refer to the updated notes, in the doc here, just below the sample prompt, you will get an idea for the requirement. Let me know if that helps.