OhMyGPT
SDK Integrations

Go

Use OhMyGPT with Go.

You can use OhMyGPT with Go using the official OpenAI Go client or community clients.

Using sashabaranov/go-openai

The most popular Go client for OpenAI-compatible APIs.

Installation

go get github.com/sashabaranov/go-openai

Configuration

package main

import (
    "context"
    "fmt"
    openai "github.com/sashabaranov/go-openai"
)

func main() {
    config := openai.DefaultConfig("<OHMYGPT_API_KEY>")
    config.BaseURL = "https://api.ohmygpt.com/v1"

    client := openai.NewClientWithConfig(config)

    resp, err := client.CreateChatCompletion(
        context.Background(),
        openai.ChatCompletionRequest{
            Model: "gpt-4o",
            Messages: []openai.ChatCompletionMessage{
                {
                    Role:    openai.ChatMessageRoleUser,
                    Content: "Hello!",
                },
            },
        },
    )

    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    fmt.Println(resp.Choices[0].Message.Content)
}

Streaming

stream, err := client.CreateChatCompletionStream(
    context.Background(),
    openai.ChatCompletionRequest{
        Model: "gpt-4o",
        Messages: []openai.ChatCompletionMessage{
            {Role: openai.ChatMessageRoleUser, Content: "Write a poem"},
        },
        Stream: true,
    },
)
if err != nil {
    return
}
defer stream.Close()

for {
    response, err := stream.Recv()
    if err != nil {
        break
    }
    fmt.Print(response.Choices[0].Delta.Content)
}

For more details, see the go-openai documentation.

How is this guide?

Last updated on

On this page