AI DevKit
Glitch9 Inc.Glitch9 DocsDiscordIssues
  • Introduction
    • AI DevKit 3.0
    • Update Logs
    • Troubleshooting
      • ❗Issues After Updating AIDevKit?
      • ❗The type or namespace name 'Newtonsoft' could not be found
      • ❗Build Error: The name 'UnityMenu' does not exist in the current context
      • ❗Model 'modelName' not found
      • ❗The model `model name` does not exist or you do not have access to it
      • ❗The type or namespace name 'AndroidJavaObject' could not be found
      • ❗The type or namaspace name 'Plastic' does not exist
      • ❗Build Error: The name 'Asset Database' does not exist in the current context
      • ❗'ModelData.Create(Provider, string, UnixTime?, string)': not all code paths return a value
      • ⚠️ Timeout Issues
      • ⚠️ Receiving a “HTTP/1.1 400 Bad Request” Error?
    • FAQ
      • My OpenAI API free trial has ended or is inactive.
  • Quick Start
    • Get API Keys
      • OpenAI API Key Guide
      • Google API Key Guide
      • ElevenLabs API Key Guide
    • Text Generation
    • C# Object Generation
    • Image Generation
    • Sound Effect Generation
    • Text to Speech (TTS)
    • Speech to Text (STT)
    • Voice Changer
    • Audio Isolation
  • Pro Features
    • Generation Menu
      • Code Generators
        • C# Script Generator
        • Unity Component Generator
    • Editor Chat
    • Editor Vision (TTI, ITI)
    • Editor Speech (TTS)
    • Management Tools
      • Prompt History Viewer
      • AI Model Manager
      • TTS Voice Manager
      • OpenAI File Manager
      • OpenAI Assistant Manager
      • ElevenLabs Voice Library
  • Assistants API (OpenAI)
    • How it works
    • Creating custom functions
    • Creating assistants API
  • Advanced API Supports
    • OpenAI API
      • 💬Chat completions
      • 🖼️Image operations
      • 🗣️Text to speech
      • 🎙️Speech to text
        • Recording real-time in Unity
      • 💾Files
      • 🔎Embeddings
      • 🛡️Moderations
      • ⚙️Fine-tuning
    • Google API
      • 📝System instructions
      • 💬Text generation
      • ⚙️Fine-tuning
      • ▶️Fucntion calling
      • 🔎Embeddings
      • 🛡️Safety
      • 💻Code execution
    • ElevenLabs API
  • Legacy Documents
    • AI DevKit 1.0 - 2.0
      • AI DevKit 2.0
      • AI DevKit 1.0
      • Preperation
      • Event Handlers
      • Scriptable Toolkits
        • Chat Streamer
        • Image Generator
        • Voice Transcriber
        • Voice Generator
      • Editor Tools
Powered by GitBook
On this page
  • Step 1: Creating a Chat Streamer Instance
  • Step 2: Configuring Chat Streamer
  • Step 3: Initializing Chat Streamer
  • Step 4: Starting a Chat Session
  • Step 5: Handling Chat Responses
  • Step 6: Customizing the Chat Behavior
  • Step 7: Saving Chat Logs
  • Step 8: Finalizing the Chat Session
  • Best Practices
  1. Legacy Documents
  2. AI DevKit 1.0 - 2.0
  3. Scriptable Toolkits

Chat Streamer

PreviousScriptable ToolkitsNextImage Generator

The ChatStreamer toolkit enables real-time chat capabilities within Unity, powered by OpenAI's GPT models. It allows for dynamic text-based interactions that can be integrated into games or applications for a variety of purposes, including non-player character (NPC) dialogue, interactive storytelling, customer service bots, and more.

Step 1: Creating a Chat Streamer Instance

  1. Go to the Unity Editor menu and select Assets > Create > Glitch9/OpenAI/Toolkits > Chat Streamer.

  2. A new instance of ChatStreamer will appear in your Project view. Select it to configure its properties in the Inspector window.

Step 2: Configuring Chat Streamer

  1. Model: Select the GPT model you wish to use for generating responses.

  2. Instruction: Define an initial instruction or prompt that will guide the AI's conversation style or topic.

  3. Frequency Penalty: Adjust this value to discourage the AI from repeating itself.

  4. Max Tokens: Set the maximum length of the generated response.

  5. Temperature: Control the randomness of the response. A lower temperature results in more predictable responses.

Step 3: Initializing Chat Streamer

  1. Attach the ChatStreamer instance to a script or GameObject in your scene where the chat will be managed.

  2. Call the Initialize method from your script to prepare the ChatStreamer for use. You can pass an Action<string> delegate to handle incoming messages.

public ChatStreamer chatStreamer;

void Start() {
    chatStreamer.Initialize(OnMessageReceived);
}

void OnMessageReceived(string message) {
    // Handle the new message, such as updating the UI
}

Step 4: Starting a Chat Session

  1. To begin streaming chat messages, call the EnterChat method with the user's input prompt.

  2. The EnterChat method is asynchronous. Use await or UniTask to wait for the method to complete and to receive the response.

csharpCopy codepublic async void SendMessageToChat(string userInput) {
    ChatCompletionResult result = await chatStreamer.EnterChat(userInput);
    // The result contains the AI's response.
}

Step 5: Handling Chat Responses

  1. Use the OnStream event to update the chat interface with both the user's messages and the AI's responses.

  2. Append each new message to the chat log, ensuring a continuous conversation flow.

Step 6: Customizing the Chat Behavior

  • Adjust the Frequency Penalty, Max Tokens, and Temperature settings to fine-tune the AI's responses based on the context of your application.

  • Utilize the SetFrequencyPenalty, SetMaxTokens, and SetTemperature methods to change the conversation dynamics on the fly.

Step 7: Saving Chat Logs

  • If your application requires saving the chat history, access the Messages property to retrieve the current chat log.

  • Serialize and save the messages to a file or database as needed.

Step 8: Finalizing the Chat Session

  • To end a chat session, you can simply stop sending prompts or provide a user interface option for the player to exit the conversation.

  • Optionally, you can add logic to reset or clear the ChatStreamer instance for a new conversation.

Best Practices

  • Test different configurations to find the ideal chat behavior for your use case.

  • Consider implementing rate-limiting to avoid overloading the user with responses.

  • Always provide a user-friendly way to exit the chat or restart the conversation.