Files
langchaingo/examples/perplexity-completion-example/perplexity_completion_example.go
T
2025-05-07 22:56:32 +03:00

46 lines
849 B
Go

package main
import (
"context"
"fmt"
"log"
"os"
"github.com/vxcontrol/langchaingo/llms"
"github.com/vxcontrol/langchaingo/llms/openai"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
apiKey := os.Getenv("PERPLEXITY_API_KEY")
llm, err := openai.New(
// Supported models: https://docs.perplexity.ai/models/model-cards
openai.WithModel("sonar"),
openai.WithBaseURL("https://api.perplexity.ai"),
openai.WithToken(apiKey),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
_, err = llms.GenerateFromSinglePrompt(ctx,
llm,
"What is a prime number?",
llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
fmt.Print(string(chunk))
return nil
}),
)
fmt.Println()
if err != nil {
log.Fatal(err)
}
}