Skip to content

Conversation

@StephenHodgson
Copy link
Member

@StephenHodgson StephenHodgson commented Nov 14, 2025

  • Optimize image texture loading
  • Allow setting output_text for messages from Role.Assistant

StephenHodgson and others added 5 commits November 14, 2025 18:24
- Optimize image texture loading
Use case: In games, we often want a mixture of both AI generated
content, as well as pre-scripted content. This change allows us to
insert Assistant messages into a Response conversation, and to let the
AI continue the conversation.

Example:
```C#
using OpenAI;
using OpenAI.Responses;
using System;
using System.Collections.Generic;
using UnityEngine;

public class ConversationExample : MonoBehaviour {
    private OpenAIClient api = null;
    private List<IResponseItem> conversation = new List<IResponseItem>();

    private void Start() {
        api = new OpenAIClient(GetOpenAIAuthentication()) { EnableDebug = true };

        // Example use case: We want to insert fake AI messages into the conversation, and let the AI continue the conversation afterwards.
        // For example, the player just entered a certain area of a game level, and meets this NPC for the first time.
        conversation.Add(new Message(Role.Assistant, "Welcome to the Krusty Krab. My name is Squidward, how can I help you?"));

        // User continues the conversation after being prompted.
        conversation.Add(new Message(Role.User, "What's your favourite musical instrument?"));

        // Now we want the AI to continue the conversation.
        GetResponse();
    }

    private void Update() { }

    private async void GetResponse() {
        try {
            // Request a response from OpenAI.
            CreateResponseRequest request = new CreateResponseRequest(input: conversation, model: OpenAI.Models.Model.GPT5);
            OpenAI.Responses.Response response = await api.ResponsesEndpoint.CreateModelResponseAsync(request, cancellationToken: destroyCancellationToken);

            // Get response from OpenAI.
            for (int i = 0; i < response.Output.Count; ++i) {
                IResponseItem responseItem = response.Output[i];
                switch (responseItem) {
                    case OpenAI.Responses.Message message:
                        conversation.Add(message);
                        Debug.Log("AI Response: " + message.ToString());
                        break;
                    default: break;
                }
            }
        } catch (Exception e) {
            Debug.Log(e);
        }
    }

    private OpenAIAuthentication GetOpenAIAuthentication() {
        string apiKey = "Your API Key";
        string orgKey = "Your Organisation Key";
        string projKey = "Your Project Key";
        return new OpenAIAuthentication(apiKey, orgKey, projKey);
    }
}
```

AI Response:
<img width="1081" height="50" alt="image"
src="https://github.com/user-attachments/assets/e08e77b3-80b9-48b6-99b7-8ec29ac4d1f0"
/>

---------

Co-authored-by: Stephen Hodgson <rage.against.the.pixel@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants