This project demonstrates how to use the Prediction Guard API in a Go application to generate text completions. The goal was to interact with the Prediction Guard API to request Go code generation using a language model.
- Project Overview
- Setup
- Issues Faced
- Solution and Fixes
- Running the Program
- Generated Response Example
- Conclusion
The goal of this project was to write a Go program that leverages the Prediction Guard API client to generate code based on input prompts. The initial code aimed to interact with the API and request a code snippet that prints random numbers.
-
Cloning the Repository: This project was cloned from GitHub: https://github.com/mesutoezdil/llms-example2.git.
-
Setting Up the Environment: An environment variable for the API key was used as follows:
export PGKEY="your_api_key_here"
Replace"your_api_key_here"with your actual Prediction Guard API key. -
Go Modules Initialization: The repository was set up with Go modules, and the necessary package
github.com/predictionguard/go-clientwas installed at versionv0.21.0:go mod init github.com/mesutoezdil/llms-example2go get github.com/predictionguard/go-client@v0.21.0
-
Role Type Compatibility: The initial issue was related to the
Roletype expected by thego-client. Earlier attempts to use plain strings for"system"and"user"roles resulted in errors as they didn't match the expected type. -
Version Compatibility: The
go-clientlibrary versionv0.21.0required different handling ofRoleandModelthan later versions, which led to confusion while trying to use roles and model strings correctly.
-
Downgrading the
go-clientVersion: To resolve compatibility issues, thego-clientpackage was downgraded tov0.21.0. This was necessary due to breaking changes in newer versions:go get github.com/predictionguard/go-client@v0.21.0
-
Correct Use of Roles: After exploring the library documentation, it was found that the roles could be accessed using the
client.Rolesvariable:client.Roles.Systemfor the system roleclient.Roles.Userfor the user role
-
Successful API Call: After applying the correct
RoleandModelvalues, the API responded successfully with a code snippet as expected.
- Run the Go Program: After setting up the environment and fixing the compatibility issues, run the program using:
go run main.go- Ensure your environment variable
PGKEYis set with your Prediction Guard API key.
Upon successful execution, the program makes a request to the Prediction Guard API to generate a Go code snippet that prints out 10 random numbers. The API returned the following code example:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
for i := 0; i < 10; i++ {
fmt.Println(rand.Intn(100))
}
}
This code uses the math/rand package to generate random numbers and prints out 10 numbers between 0 and 99.
The main challenges were understanding the Role type expectations of the Prediction Guard client library and ensuring the code was compatible with the v0.21.0 version. By properly using client.Roles and downgrading the client library, the API was successfully accessed, and the desired output was generated.