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
: The module definition and dependencies for this Go application.main.go
: The main entry point of the application where user input is handled and the chat completions are streamed.openai_chat_completions_streaming_hello_world.go
: The helper class that interacts with the OpenAI Chat API.
go.mod
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
)
- Read Environment Variables: The application reads the required environment variables and sets default values if they are not provided.
- Initialize Chat Helper: An instance of
OpenAIChatCompletionsStreamingExample
is created using the provided environment variables. - 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
)
- Initialization: The
NewOpenAIChatCompletionsStreamingExample
function initializes the helper class with the provided endpoint, API key, deployment name, and system prompt. - Clear Conversation: The
ClearConversation
method clears the conversation history, retaining only the system prompt. - 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.