Introduction to GPT-Go
GPT-Go is a Go SDK client developed to facilitate interaction with OpenAI's powerful GPT-4 and GPT-3 APIs. This project serves as an essential bridge for developers to integrate advanced natural language processing capabilities provided by OpenAI into their Go applications smoothly.
Project Overview
GPT-Go offers a robust and efficient way to interact with the OpenAI models by providing an interface for various API functions. Designed with ease of use in mind, it supports multiple engine options, including completions, streaming interactions, document search, and image generation through DALL-E 2.
Getting Started
Starting with GPT-Go is straightforward. By following a few simple steps, developers can clone the repository, set up their environment, and make their first API call:
-
Clone the Project: Begin by cloning the GPT-Go repository from GitHub to get hold of the source code.
git clone https://github.com/hanyuancheung/gpt-go.git
-
Navigate to the Project Directory: Move into the cloned directory to access the files.
cd gpt-go
-
Set the API Key: Define your OpenAI API key as an environment variable. This key is essential for authenticating API requests.
export API_KEY={YOUR_API_KEY}
Obtain your API key from the official OpenAI platform here.
-
Build and Run an Example: Compile and execute an example binary to test the setup.
make chatgpt-example ./chatgpt
Features and Support
GPT-Go extends support for a comprehensive range of OpenAI API functionalities, ensuring a seamless developer experience:
- Engine Management: Easily list and access different API engines for versatile usage.
- Completion API: The core feature for generating text based on user input.
- Streaming: Real-time streaming capabilities with the Completion API.
- Document Search API: Efficiently search through documents.
- Image Generation: Use DALL-E 2 for creating images from textual descriptions.
- Customizable Options: Adapt the default URL, user-agent, and set timeouts as per the needs.
Usage Examples
Here are a few sample cases demonstrating various capabilities of GPT-Go:
ChatGPT Streaming Completion
This example shows how to set up a streaming chat session with ChatGPT:
func main() {
client := gpt.NewClient("API_KEY")
err := client.ChatCompletionStream(context.Background(), &gpt.ChatCompletionRequest{
Model: gpt.GPT3Dot5Turbo,
Messages: []gpt.ChatCompletionRequestMessage{
{ Role: "user", Content: "Hello!" },
},
}, func(response *gpt.ChatCompletionStreamResponse) {
fmt.Print(response.Choices[0].Delta.Content)
})
if err != nil {
fmt.Printf("ChatCompletionStream error: %v\n", err)
}
}
GPT-3 Completion
Utilize the standard completion API with a specific engine:
func main() {
client := gpt.NewClient("API_KEY")
rsp, err := client.CompletionWithEngine(context.Background(), &gpt.CompletionRequest{
Model: gpt.TextDavinci003Engine,
Prompt: []string{"Hello!"},
})
if err != nil {
fmt.Printf("Completion error: %v\n", err)
return
}
fmt.Print(rsp.Choices[0].Text)
}
Image Generation with DALL-E 2
Generate an image using a text prompt:
func main() {
client := gpt.NewClient("API_KEY")
rsp, err := client.Image(context.Background(), &gpt.ImageRequest{
Prompt: "Chicken",
})
if err != nil {
fmt.Printf("Image generation error: %v\n", err)
return
}
fmt.Print(rsp.Data[0].URL)
}
Contribution and Support
GPT-Go encourages community contributions. Prospective contributors should open an issue on GitHub to discuss prospective changes before making substantial pull requests. It's vital to ensure that all code is formatted using gofmt
prior to submission.
For more detailed documentation and support, consult the Go documentation linked to GPT-Go.
Licensing
This project is released under the MIT License, allowing for broad usage and adoption across projects. The details can be found in the project's LICENSE file.
Show Your Support
If GPT-Go proves helpful in your projects, consider giving it a ⭐ on GitHub to show your support and appreciation.