Introduction to go-gpt3
go-gpt3 is a versatile and effective tool that serves as an OpenAI GPT-3 API client, specifically designed for Go/Golang programming environments. It allows developers to seamlessly integrate and interact with GPT-3 APIs within their Go applications. The tool is engineered to support completion APIs, both with and without streaming, offering flexibility depending on the application needs.
How to Use go-gpt3
One of the primary functions of go-gpt3 is to facilitate easy interaction with the GPT-3 API, particularly its completion features. Here's a brief example of how to utilize this client in a Go application:
client := gpt3.NewClient(apiKey)
resp, err := client.Completion(ctx, gpt3.CompletionRequest{
Prompt: []string{"2, 3, 5, 7, 11,"},
})
fmt.Print(resp.Choices[0].Text)
// prints " 13, 17, 19, 23, 29, 31", etc
This snippet demonstrates simple usage where the developer creates a new client using an API key, sends a completion request with a sequence of prime numbers, and prints the system's response.
Further Documentation
For those looking to dive deeper into the capabilities of go-gpt3, comprehensive documentation is available. This resource details the various types and methods supported by the tool, aiding developers in effectively leveraging its full potential. More information can be found here.
Running Full Examples
To test out the features of go-gpt3, developers can follow a structured approach:
- Place the example code into a
main.go
file. - Execute
go run main.go
to run the code. - Ensure Go modules are enabled by running
go mod init
within your test repository. - Alternatively, clone the repository and execute the test script with
go run cmd/test/main.go
.
A critical step is the configuration of an .env
file containing the OpenAI API Key:
API_KEY=<openAI API Key>
Here is an example code for a more extensive implementation:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/PullRequestInc/go-gpt3"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
apiKey := os.Getenv("API_KEY")
if apiKey == "" {
log.Fatalln("Missing API KEY")
}
ctx := context.Background()
client := gpt3.NewClient(apiKey)
resp, err := client.Completion(ctx, gpt3.CompletionRequest{
Prompt: []string{"The first thing you should know about javascript is"},
MaxTokens: gpt3.IntPtr(30),
Stop: []string{"."},
Echo: true,
})
if err != nil {
log.Fatalln(err)
}
fmt.Println(resp.Choices[0].Text)
}
Features and Support
go-gpt3 offers robust support for various functions necessary for comprehensive API interaction:
- Listing and retrieving engines with the List Engines and Get Engine APIs.
- Employing the Completion API for main GPT-3 functionality.
- Streaming support for managing real-time data flow during API operations.
- Document Search API for efficient data retrieval.
- Customizable settings such as overriding the default URL, user-agent, and timeout options.
Endorsements
go-gpt3 is powered by PullRequest, a platform dedicated to enhancing code quality through professional code reviews. This affiliation underscores the reliability and performance of go-gpt3 as a trusted tool for developers working in Go environments.
By incorporating go-gpt3, developers can harness the power of OpenAI's GPT-3 model within their applications, facilitating sophisticated language generation tasks and enhancing the functionality of their projects.