perplexity

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2025 License: MIT Imports: 8 Imported by: 0

README

Go Report Card Maintainability Test Coverage

Perplexity API Go Client

A lightweight Go library for interacting with the Perplexity AI API, focusing on the chat completion endpoint.

Features

Simple and easy-to-use interface for making chat completion requests
Supports all Perplexity models, including online LLMs
Handles authentication and API key management
Provides convenient methods for common operations

Installation

To install the library, use go get:

go get github.com/sgaunet/perplexity-go

Usage

Here's a quick example of how to use the library:

package main

import (
	"fmt"
	"os"

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

func main() {
	client := perplexity.NewClient(os.Getenv("PPLX_API_KEY"))
	res, err := client.CreateCompletion([]perplexity.Message{
		{
			Role:    "user",
			Content: "What's the capital of France?",
		},
	})

	if err != nil {
		fmt.Printf("Error: %v\n", err)
		os.Exit(1)
	}
	fmt.Println(res.GetLastContent())
}

Documentation

For detailed documentation and more examples, please refer to the GoDoc page.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const DefaultEndpoint = "https://api.perplexity.ai/chat/completions"
View Source
const DefautTimeout = 10 * time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type Choice

type Choice struct {
	Index        int     `json:"index"`
	FinishReason string  `json:"finish_reason"`
	Message      Message `json:"message"`
	Delta        Message `json:"delta"`
}

Choice is a choice object for the Perplexity API.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a client for the Perplexity API.

func NewClient

func NewClient(apiKey string) *Client

NewClient creates a new Perplexity API client. The apiKey is the API key to use for authentication. The default model is llama-3-sonar-small-32k-online.

func (*Client) CreateCompletion

func (s *Client) CreateCompletion(messages []Message) (*CompletionResponse, error)

CreateCompletion sends simple text to the Perplexity API and retrieve the response.

func (*Client) GetHTTPTimeout added in v0.2.0

func (s *Client) GetHTTPTimeout() time.Duration

GetHTTPTimeout sets the HTTP timeout.

func (*Client) GetModel

func (s *Client) GetModel() string

GetModel returns the model currently in use.

func (*Client) SetEndpoint

func (s *Client) SetEndpoint(endpoint string)

SetEndpoint sets the API endpoint.

func (*Client) SetHTTPClient

func (s *Client) SetHTTPClient(httpClient *http.Client)

SetHTTPClient sets the HTTP client.

func (*Client) SetHTTPTimeout added in v0.2.0

func (s *Client) SetHTTPTimeout(timeout time.Duration)

SetHTTPTimeout sets the HTTP timeout.

func (*Client) SetModuleLlama31SonarHuge128kOnline added in v1.1.0

func (s *Client) SetModuleLlama31SonarHuge128kOnline()

SetModuleLlama31SonarHuge128kChat sets the model to llama-3.1-sonar-huge-128k-online.

func (*Client) SetModuleLlama31SonarLarge128kOnline added in v1.0.0

func (s *Client) SetModuleLlama31SonarLarge128kOnline()

SetModuleLlama31SonarLarge128kChat sets the model to llama-3.1-sonar-large-128k-online.

func (*Client) SetModuleLlama31SonarSmall128kOnline added in v1.0.0

func (s *Client) SetModuleLlama31SonarSmall128kOnline()

SetModuleLlama31SonarSmall128kOnline sets the model to llama-3.1-sonar-small-128k-online.

type CompletionRequest

type CompletionRequest struct {
	Messages []Message `json:"messages"`
	Model    string    `json:"model"`
}

CompletionRequest is a request object for the Perplexity API.

type CompletionResponse

type CompletionResponse struct {
	ID      string   `json:"id"`
	Model   string   `json:"model"`
	Created int      `json:"created"`
	Usage   Usage    `json:"usage"`
	Object  string   `json:"object"`
	Choices []Choice `json:"choices"`
}

CompletionResponse is a response object for the Perplexity API.

func (*CompletionResponse) GetLastContent

func (r *CompletionResponse) GetLastContent() string

func (*CompletionResponse) String

func (r *CompletionResponse) String() string

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message is a message object for the Perplexity API.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage is a usage object for the Perplexity API.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL