Files
langchaingo/examples/perplexity-completion-example/perplexity_completion_example.go
T
Dmitry Ng 6317685b67 feat: integrate streaming functionality across LLMs and callbacks
- Updated LLMs and callback interfaces to utilize a new streaming package for handling content chunks.
- Changed function signatures to accept streaming.Chunk instead of byte slices for better type safety and clarity.
- Enhanced examples and tests to demonstrate the new streaming capabilities, ensuring compatibility with existing functionality.
- Added reasoning and tool call handling in streaming responses for improved processing of LLM outputs.
2025-06-23 18:52:06 +03:00

47 lines
936 B
Go

package main
import (
"context"
"fmt"
"log"
"os"
"github.com/vxcontrol/langchaingo/llms"
"github.com/vxcontrol/langchaingo/llms/openai"
"github.com/vxcontrol/langchaingo/llms/streaming"
"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/docs/model-cards
openai.WithModel("llama-3.1-sonar-large-128k-online"),
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(_ context.Context, chunk streaming.Chunk) error {
fmt.Println(chunk.String())
return nil
}),
)
fmt.Println()
if err != nil {
log.Fatal(err)
}
}