mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-08-24 21:31:28 -04:00
6317685b67
- 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.
47 lines
936 B
Go
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)
|
|
}
|
|
}
|