Endpoints supported:
- Chat completion
As a library
go get github.com/kfatehi/go-openaiHere is an example you can use.
-
Create a directory for your project and
cdinto it -
Setup your go.mod as main:
go mod init main -
Get this library:
go get github.com/kfatehi/go-openai -
Put the following code in
main.goand set your API key:
package main import ( "log" openai "github.com/kfatehi/go-openai" ) func main() { apiKey := "YOUR API KEY" req := openai.ChatRequest{ Model: "gpt-3.5-turbo", Messages: []openai.ChatMessage{ { Role: "user", Content: "Hello!", }, }, } resp, err := openai.ChatCompletion(apiKey, req) if err != nil { panic(err) } log.Println(resp.Choices[0].Content) } - Run it:
go run main.go
The test will make a real API call.
-
Create a .env file
-
Run the test
go test -modfile go_test.mod You can also run a specific unit:
go test -modfile go_test.mod -run '^TestChatCompletionSadPath$' this was created by taking the curl request and response from this page https://platform.openai.com/docs/api-reference/chat/create and using a prompt like this:
write a Go wrapper for the following HTTP API. I will be providing the curl request and response:
followed by:
it should take the api key and request as parameters and return a tuple of error and response as is the standard idiom in go
then a bit of manual cleanup. i also had it generate a test which i cleaned up.