Skip to content

Latest commit

 

History

History
77 lines (58 loc) · 2.03 KB

README.md

File metadata and controls

77 lines (58 loc) · 2.03 KB

perplexity-go

perplexity-go is a non-official Go client library for the Perplexity AI chat completion API. It provides an easy-to-use interface for interacting with the Perplexity API and generating chat completions using various language models.

Warning

Experimental package, API would probably break.

Features

  • Simple and intuitive API for chat completions
  • Support for multiple language models
  • Customizable request parameters
  • Asynchronous requests with context cancellation

Not supported yet

  • Streaming
  • Images
  • Citations

Installation

To install perplexity-go, use go get:

go get -u github.com/painhardcore/perplexity-go

Example Usage

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/painhardcore/perplexity-go"
)

func main() {
	apiKey := "pplx-xxx" // Replace with your actual API key
	model := perplexity.ModelLlama3SonarSmall32kChat

	// You can omit the opts parameter to use the default 30-second timeout
	opts := &perplexity.ClientOptions{
		RequestTimeoutInSeconds: 100,
    }
	client := perplexity.NewClient(apiKey, model, opts)

	request := perplexity.ChatCompletionRequest{
		Messages: []perplexity.Message{
			{Role: perplexity.RoleSystem, Content: "Be rude to me, but respect me"},
			{Role: perplexity.RoleUser, Content: "Tell me how to be a good dev and solve all tickets in my sprints"},
		},
		MaxTokens:   100,
		Temperature: 0.2,
		TopP:        0.9,
	}

	ctx := context.Background()
	response, err := client.ChatCompletions(ctx, request)
	if err != nil {
		log.Fatalf("Error: %s", err)
	}

	text, err := response.GetCompleteSingleMessage()
	if err != nil {
		fmt.Printf("err: %s,\nfull response %+v\n", err, response)
	}
	fmt.Println(text)
}

Contributing

Contributions to perplexity-go are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you'd like to contribute code, please fork the repository and submit a pull request.