Deep AI API causing lag while downloading response

I am using Deep AI’s text generation API to feed an AI response into a Unity project (I am an artist, not a programmer :roll_eyes:) but every few seconds when the feed is “refreshed”, the whole project lags, presumably because Unity is waiting for the download to finish before resuming. I have been studying up on coroutines, which I can more or less wrap my head around (maybe), but I am unable to get my program to work the way I would like. I THINK the issue is that, as I have the code written, the call and response are tied into the same line, and I cannot figure out how to separate the two to put a yield in the middle. I cannot find the URL that the API is reaching out to, and in the documentation for the Deep AI C# Client, I cannot figure out a way to isolate the “call” and the “response” to try and wedge a yield in the middle. Of course, I could be way off base. Any help would be really appreciated, I have been beating my head against the wall with this for several days!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DeepAI; // Add this line to the top of your file

public class Tester : MonoBehaviour
{
    private string result;
    public Text MyText;
    void Start()
    {
        StartCoroutine("Script");
    }
    IEnumerator Script()
    {
        while (true)
        {
            Debug.Log("Run!");

            DeepAI_API api = new DeepAI_API(apiKey: "(my key is here");

            StandardApiResponse resp = api.callStandardApi("text-generator", new
            {
                text = "I love nature",
            });

            yield return new WaitForSeconds(3);
            result = (api.objectAsJsonString(resp));
            MyText.text = result;
        }
     }
}

So it sounds like your call to api.callStandardApi is blocking the main thread until it completes. You’re not going to be able to fix that with a coroutine unless whatever this is has an alternative async way of doing this. A coroutine will block the main thread just the same as calling something in Start or Update will if it takes too long.

Assuming it doesn’t have any support for doing this async, I’d try moving this to its own thread. So I’d have the main thread tell your background thread it needs to do one of these calls. When the background thread is done, it sends the response back to the main thread. I usually use ConcurrentQueues for the inter-thread communication. This solves the problem because blocking your background thread doesn’t really matter to the main thread. But creating your own threads is a pretty advanced topic for someone who isn’t a programmer.

1 Like

I was worried I was going to get a “you’re getting lost in the weeds” response :smile: But I really appreciate your help!! As long as I have some new avenues to try out, I have hope. Thank you!!

1 Like

The first thing I’d do though is confirm that the callStandardApi call is what is blocking. I’d add Debug.Log statements with the current DateTime.Now.Ticks before and after that line of code. That will tell you how long that specific line is taking. You can’t use Unity’s Time.time unfortunately, because that only gets updated between frames.

2 Likes

Yes, you were correct. Consistent 3 second delay every time that line runs.

1 Like

An update for anybody interested- I was able to solve this problem using Thread Ninja - Multithread Coroutine which is available for free in the Unity Asset Store. Big thanks to Joe-Censored for pointing me in the right direction.

1 Like