Uralstech / UGemini

A Unity C# wrapper for the Google Gemini API.
https://uralstech.github.io/UGemini/
Apache License 2.0
4 stars 0 forks source link

streamGenerateContent support #1

Closed yosun closed 3 weeks ago

yosun commented 1 month ago

hey! curious what you're building and also is streamGenerateContent on the roadmap

Uralstech commented 1 month ago

Hi! Thanks for checking UGemini out!

I don't really have a roadmap for this right now, as I am working on this in my free time. I'm working on implementing the easiest stuff, like the files and media endpoints, which have been implemented in the preview. You are free to contribute though!

yosun commented 4 weeks ago

the preview link doesn't work!

does this streaming implementation help? https://github.com/RageAgainstThePixel/com.openai.unity

Uralstech commented 3 weeks ago

the preview link doesn't work!

Oh yeah, I merged the preview into the main branch. It's in production now!

does this streaming implementation help? https://github.com/RageAgainstThePixel/com.openai.unity

I have looked into it, but I won't be able to implement it right now as I have some more immediate work to do.

Uralstech commented 3 weeks ago

I have also added the documentation and samples for the File API.

Uralstech commented 3 weeks ago

@yosun I have added streamGenerateContent support in the unstable branch. I have not tested the changes much, or how it affects the other features (I'm working on that), but it does work with plain text responses. I'll merge the branch into master after some more testing.

The code I've been using to test it is a modified version of the JSON response sample:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Uralstech.UGemini.Chat;
using Uralstech.UGemini.Schema;

namespace Uralstech.UGemini.Samples
{
    public class JSONChatManager : MonoBehaviour
    {
        [SerializeField] private InputField _chatInput;
        [SerializeField] private Text _chatResponse;

        public async void OnChat()
        {
            string text = _chatInput.text;
            if (string.IsNullOrWhiteSpace(text))
            {
                Debug.LogError("Chat text should not be null or whitespace!");
                return;
            }

            // Note: It seems GeminiManager.Gemini1_5Flash is not very good at JSON.
            GeminiChatRequest request = new GeminiChatRequest(GeminiManager.Gemini1_5Pro, true)
            {
                Contents = new GeminiContent[]
                {
                    GeminiContent.GetContent(text, GeminiRole.User),
                },
                SystemInstruction = GeminiContent.GetContent("You are a helpful math teacher who teacher their students mathematics in the most helpful way possible."),
                GenerationConfig = new GeminiGenerationConfiguration()
                {
                    ResponseMimeType = GeminiResponseType.Json,
                    ResponseSchema = new GeminiSchema()
                    {
                        Type = GeminiSchemaDataType.Array,
                        Description = "A list of mathematical expressions.",
                        Items = new GeminiSchema()
                        {
                            Type = GeminiSchemaDataType.Object,
                            Properties = new Dictionary<string, GeminiSchema>()
                            {
                                {
                                    "expression", new GeminiSchema()
                                    {
                                        Type = GeminiSchemaDataType.String,
                                    }
                                },
                                {
                                    "explanation", new GeminiSchema()
                                    {
                                        Type = GeminiSchemaDataType.String,
                                    }
                                },
                            },
                            Required = new string[] { "expression", "explanation", },
                        },
                    },
                },
                OnPartialResponseReceived = response =>
                {
                    _chatResponse.text = response.Parts[0].Text;

                    return System.Threading.Tasks.Task.CompletedTask;
                }
            };

            await GeminiManager.Instance.StreamRequest(request);
            //_chatResponse.text = response.Parts[0].Text;
        }
    }
}
Uralstech commented 3 weeks ago

I've updated the branch with a crucial bug fix, so please update to v1.2.0-preview.2. Also, the package requires com.utilities.rest for streamGenerateContent.

Uralstech commented 3 weeks ago

I have merged the unstable branch into master. Please check if streamGenerateContent works for you.