Skip to content

Sample Overview: OpenAI Chat Streaming (Go)

This page was automatically generated by AI; not yet reviewed for accuracy...

The content and code samples on this page were generated by using the ai CLI with customized prompts in this repository.

It's cool, but, it's experimental. 😁

Please review the content and code before using it in your application.

This sample demonstrates how to use the OpenAI Chat API with streaming in a Go application. The sample covers the following aspects:

  • Getting connection information and secrets from environment variables
  • Using a helper class to encapsulate the OpenAI API calls
  • Getting input from the user
  • Sending the input to the helper class
  • Getting the response from the helper class
  • Deeper dive into the helper class

Files in this sample

go.mod

title="go.mod"
{{ read_file('samples/openai-chat-streaming-go/go.mod') }}

main.go

title="main.go"
{{ read_file('samples/openai-chat-streaming-go/main.go') }}

openai_chat_completions_streaming_hello_world.go

title="openai_chat_completions_streaming_hello_world.go"
{{ read_file('samples/openai-chat-streaming-go/openai_chat_completions_streaming_hello_world.go') }}

How It Works

Environment Variables

The application expects the following environment variables to be set:

  • AZURE_OPENAI_API_KEY: The API key for the OpenAI service.
  • AZURE_OPENAI_ENDPOINT: The endpoint URL for the OpenAI service.
  • AZURE_OPENAI_CHAT_DEPLOYMENT: The deployment name for the OpenAI chat service.
  • AZURE_OPENAI_SYSTEM_PROMPT: The system prompt to use for the chat completions.

Main Application Flow (main.go)

  1. Read Environment Variables: The application reads the required environment variables and sets default values if they are not provided.
  2. Initialize Chat Helper: An instance of OpenAIChatCompletionsStreamingExample is created using the provided environment variables.
  3. User Interaction Loop: The application enters a loop where it reads user input, sends it to the chat completion helper, and prints the response.

Helper Class (openai_chat_completions_streaming_hello_world.go)

  1. Initialization: The NewOpenAIChatCompletionsStreamingExample function initializes the helper class with the provided endpoint, API key, deployment name, and system prompt.
  2. Clear Conversation: The ClearConversation method clears the conversation history, retaining only the system prompt.
  3. Get Chat Stream: The GetChatCompletionsStream method sends the user input to the OpenAI service and streams the response back, invoking a callback function for each received message.