Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v5...v6) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: '6' dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> |
||
|---|---|---|
| .github | ||
| .vscode | ||
| cmd | ||
| .gitignore | ||
| .golangci.yml | ||
| .goreleaser.yml | ||
| CODE_OF_CONDUCT.md | ||
| CODEOWNERS | ||
| CONTRIBUTING.md | ||
| go.mod | ||
| go.sum | ||
| LICENSE | ||
| perplexity.go | ||
| perplexity_async.go | ||
| perplexity_async_request.go | ||
| perplexity_async_test.go | ||
| perplexity_async_utils.go | ||
| perplexity_async_utils_test.go | ||
| perplexity_error.go | ||
| perplexity_error_test.go | ||
| perplexity_image.go | ||
| perplexity_image_test.go | ||
| perplexity_msg.go | ||
| perplexity_msg_test.go | ||
| perplexity_request.go | ||
| perplexity_request_test.go | ||
| perplexity_request_validator.go | ||
| perplexity_request_validator_test.go | ||
| perplexity_response.go | ||
| perplexity_response_test.go | ||
| perplexity_search_benchmark_test.go | ||
| perplexity_search_compliance_test.go | ||
| perplexity_search_integration_test.go | ||
| perplexity_search_request.go | ||
| perplexity_search_request_test.go | ||
| perplexity_search_response.go | ||
| perplexity_search_response_test.go | ||
| perplexity_search_validator.go | ||
| perplexity_search_validator_test.go | ||
| perplexity_test.go | ||
| README.md | ||
| SECURITY.md | ||
| SUPPORT.md | ||
| Taskfile.yml | ||
Perplexity API Go Client
A lightweight Go library for interacting with the Perplexity AI API, supporting both chat completions and the Search API.
Features
- Simple and easy-to-use interface for chat completion requests
- Search API support for direct access to Perplexity's real-time web index
- Supports all Perplexity models, including online LLMs
- Handles authentication and API key management
- Comprehensive request validation
- Concurrent request support with thread-safe operations
- Context-aware request handling with cancellation support
If you need a CLI tool to interact with the API, check out the pplx project.
Due to AI models that change regulary, only the default model will be handled for version >=2.5.0. Using the WithModel, you're able to specify the model you want to use. The default model will always be maintained up to date.
Now the library should be stable.
If you have access to the beta version of the API I'm interesred to get some informations to hanle image generation. Please contact me.
Installation
To install the library, use go get:
go get github.com/sgaunet/perplexity-go/v2
Usage
Here's a quick example of how to use the library:
package main
import (
"fmt"
"os"
"github.com/sgaunet/perplexity-go/v2"
)
func main() {
client := perplexity.NewClient(os.Getenv("PPLX_API_KEY"))
msg := []perplexity.Message{
{
Role: "user",
Content: "Wat's the capital of France?",
},
}
req := perplexity.NewCompletionRequest(perplexity.WithMessages(msg), perplexity.WithReturnImages(true))
err := req.Validate()
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
res, err := client.SendCompletionRequest(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Println(res.GetLastContent())
}
Search API
The Perplexity Search API provides direct access to Perplexity's real-time web index without the generative LLM layer, returning raw ranked search results with structured snippets.
Features
- Direct web search without AI generation layer
- Single query or multi-query search support
- Structured results with titles, URLs, snippets, and scores
- Optional image results
- Domain filtering
- Country-specific results
Pricing
The Search API is priced at $5 per 1,000 requests (as of 2024), separate from chat completion pricing.
Basic Search Usage
package main
import (
"fmt"
"os"
"github.com/sgaunet/perplexity-go/v2"
)
func main() {
client := perplexity.NewClient(os.Getenv("PPLX_API_KEY"))
// Simple search
req := perplexity.NewSearchRequest("golang best practices")
resp, err := client.SendSearchRequest(req)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
// Print results
for i, result := range resp.Results {
fmt.Printf("%d. %s\n", i+1, result.Title)
fmt.Printf(" URL: %s\n", result.URL)
if result.Snippet != nil {
fmt.Printf(" Snippet: %s\n", *result.Snippet)
}
}
}
Advanced Search Options
// Search with all options
req := perplexity.NewSearchRequest(
"machine learning papers",
perplexity.WithSearchMaxResults(10),
perplexity.WithSearchReturnImages(true),
perplexity.WithSearchReturnSnippets(true),
perplexity.WithSearchCountry("US"),
perplexity.WithSearchDomains([]string{"arxiv.org", "github.com"}),
)
resp, err := client.SendSearchRequest(req)
Multi-Query Search
// Search multiple queries at once
queries := []string{
"Go programming language",
"Rust programming language",
"Python programming language",
}
req := perplexity.NewSearchRequest(queries)
resp, err := client.SendSearchRequest(req)
Request Validation
// Validate search request before sending
validator := perplexity.NewSearchRequestValidator()
if err := validator.ValidateSearchRequest(req); err != nil {
fmt.Printf("Validation error: %v\n", err)
return
}
Context Support
import "context"
// With timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := client.SendSearchRequestWithContext(ctx, req)
Search vs Chat Completions
| Feature | Search API | Chat Completions |
|---|---|---|
| Purpose | Raw web search results | AI-generated responses |
| Response | Ranked list of URLs with snippets | Natural language text |
| Use Case | Research, data gathering | Q&A, summarization, analysis |
| Pricing | $5 per 1K requests | Variable by model |
| Latency | Lower (no generation) | Higher (includes generation) |
Documentation
For detailed documentation and more examples, please refer to the GoDoc page.
Max Tokens
-
General Use Cases: For most general-purpose applications, setting max_tokens to 4000 is a good starting point. This is because many Perplexity models, like the default sonar model, can generate responses up to this limit (It's the default value in this library).
-
Long-Form Content: If you are working with long-form content or need more extensive responses, you might consider models like sonar-pro, which can handle larger outputs. However, the maximum output tokens for such models might still be capped at 8,000 tokens.
-
Model-Specific Limits: Ensure that your chosen model supports the max_tokens value you set. For example, some models might have a maximum context window or output limit that you should not exceed.
-
Performance Considerations: Higher max_tokens values can increase response time and computational resources. Therefore, balance between the need for detailed responses and performance efficiency.
License
This project is licensed under the MIT License - see the LICENSE file for details.