# Go SDK

### Chat Completion

```go
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/joho/godotenv"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		panic(err)
	}
	apiKey := os.Getenv("API_KEY")
	apiUrl := os.Getenv("API_URL")
	client := openai.NewClient(
		option.WithBaseURL(apiUrl),
		option.WithAPIKey(apiKey),
	)
	response, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
		Model: "gpt-3.5-turbo",
		Messages: []openai.ChatCompletionMessageParamUnion{
			openai.UserMessage("how many letters 'r' in the word 'strawberry'"),
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(response.Choices[0].Message.Content)
}
```

### Audio Generation

```go
package main

import (
	"context"
	"io"
	"os"

	"github.com/joho/godotenv"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		panic(err)
	}
	apiKey := os.Getenv("API_KEY")
	apiUrl := os.Getenv("API_URL")
	client := openai.NewClient(
		option.WithBaseURL(apiUrl),
		option.WithAPIKey(apiKey),
	)
	response, err := client.Audio.Speech.New(context.Background(), openai.AudioSpeechNewParams{
		Model: "tts-1",
		Input: "There are three letters 'r' in the word 'strawberry'.",
		Voice: openai.AudioSpeechNewParamsVoiceAsh,
	})
	if err != nil {
		panic(err)
	}
	file, err := os.Create("generated.mp3")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	_, err = io.Copy(file, response.Body)
	if err != nil {
		panic(err)
	}
}
```

### Audio Transcription

```go
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/joho/godotenv"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		panic(err)
	}
	apiKey := os.Getenv("API_KEY")
	apiUrl := os.Getenv("API_URL")
	client := openai.NewClient(
		option.WithBaseURL(apiUrl),
		option.WithAPIKey(apiKey),
	)
	file, err := os.Open("sound.mp3")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	response, err := client.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
		Model: "whisper-1",
		File: file,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(response.Text)
}
```

### Audio Translation

```go
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/joho/godotenv"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		panic(err)
	}
	apiKey := os.Getenv("API_KEY")
	apiUrl := os.Getenv("API_URL")
	client := openai.NewClient(
		option.WithBaseURL(apiUrl),
		option.WithAPIKey(apiKey),
	)
	file, err := os.Open("sound.mp3")
	if err != nil {
		panic(err)
	}
	defer file.Close()
	response, err := client.Audio.Translations.New(context.Background(), openai.AudioTranslationNewParams{
		Model: "whisper-1",
		File: file,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(response.Text)
}
```

### Image Generation

```go
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/joho/godotenv"
	"github.com/openai/openai-go"
	"github.com/openai/openai-go/option"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		panic(err)
	}
	apiKey := os.Getenv("API_KEY")
	apiUrl := os.Getenv("API_URL")
	client := openai.NewClient(
		option.WithBaseURL(apiUrl),
		option.WithAPIKey(apiKey),
	)
	response, err := client.Images.Generate(context.Background(), openai.ImageGenerateParams{
		Model:  "dall-e-3",
		Prompt: "three letters 'r' with strawberry texture",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(response.Data[0].URL)
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nexos.ai/openai-sdks/go-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
