Rest API for Gemini with Unity

hi. I am testing out integrating Gemini api with Unity 3d for an app.
I have tested it on Postman and am able to generate the output based on the prompt for the following url
https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro-latest:generateContent?key=XXX

In Unity, it gives an error 400 …

But if I were to use https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=x , it works fine and can extract the text and display on Unity project.

the json body looks the same to me e.g “contents”:[{“parts”:[{"text

The code that I am using is as follows;

private async void PostRequest(string prompt)
{
string url = “https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent?key=” + apiKey;
string jsonBody = “{"prompt": {"text": "” + prompt + “"}}”;
StringContent content = new StringContent(jsonBody, Encoding.UTF8, “application/json”);
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
// Capture the HTTP status code
var statusCode = response.StatusCode;
Debug.Log("Status Code: {statusCode}"); // Check if the response is successful if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); textMeshProPanel.text = responseBody; } else { // Handle non-success status here Debug.LogError(“Error: {response.ReasonPhrase}”);
}
}

Appreciate it if someone can advise if it is the C# code issue or something else?

400 = bad request

Could be a malformed json or invalid parameters, to name some. Verify that the API key is correct.
Log the resulting json that you are sending. The problem could be in the ‘prompt’ because this would have to be properly escaped too!

Ideally don’t use string formatting, use an actual json writer on an object that contains these parameters to generate the json for you. Even JsonUtility should be okay to use for such simple json.

1 Like

Hi, I use gemini to control some things through commands in my game,

and this is an example script u can use:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class YourScript : MonoBehaviour
{
    private string apiKey = "YOUR_API_KEY";
    private string apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=";

    void Start()
    {
        StartCoroutine(SendGenerateContentRequest());
    }

    IEnumerator SendGenerateContentRequest()
    {
        string url = apiUrl + apiKey;
        string jsonRequestBody = @"{
            ""contents"": [
                {
                    ""role"": ""user"",
                    ""parts"": [
                        {
                            ""text"": ""Hello!""
                        }
                    ]
                },
                {
                    ""role"": ""model"",
                    ""parts"": [
                        {
                            ""text"": ""Hello! How can I assist you today?""
                        }
                    ]
                }
            ]
        }";

        using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
        {
            byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonRequestBody);
            request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");
            yield return request.SendWebRequest();
            if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
            {
                Debug.LogError("Error: " + request.error);
            }
            else
            {
                string responseText = request.downloadHandler.text;
                Debug.Log("Response: " + responseText);
                if (responseText.Contains("\"text\""))
                {
                    string[] lines = responseText.Split('\n');
                    foreach (string line in lines)
                    {
                        if (line.Contains("\"text\""))
                        {
                            Debug.Log("Generated Text: " + line.Trim());
                        }
                    }
                }
            }
        }
    }
}
3 Likes

It’s a bitch to set up because how c# is dealing wtih json
Use newtonsoft.json to serialize and deserializ json objects especially for function calls
However what I found out was function calls and system instruction are not compatible, when you have system instruction, the response only provides the standarad text object.

For those who want to use more Gemini API features, I’ve made an OpenUPM package called UGemini, with support for all these endpoints:

  • models endpoint

    • batchEmbedContents method
    • countTokens method
    • embedContent method
    • generateAnswer method :test_tube:
    • generateContent method
    • get method
    • list method
    • streamGenerateContent method
  • cachedContents endpoint :test_tube:

    • create method
    • delete method
    • get method
    • list method
    • patch method
  • corpora endpoint :test_tube:

  • files endpoint :test_tube:

    • delete method
    • get method
    • list method
  • media endpoint :test_tube:

    • upload method
  • tunedModels endpoint :test_tube:

    • create method
    • delete method
    • generateContent method
    • get method
    • list method
    • patch method
    • transferOwnership method
  • tunedModels.operations endpoint*

    • cancel method
    • get method
    • list method
  • operations endpoint*

    • list method

:warning: - Not all methods/features are supported

:test_tube: - Using the v1beta API

*Through package dependency UCloud.Operations.

1 Like