example_test.go
go
package friendli_test
import (
	"context"
	"fmt"
	"io"
	"log"
	"os"
	"hacknight/internal/friendli"
)
// ExampleClient_nonStreaming demonstrates basic non-streaming chat completion
func ExampleClient_nonStreaming() {
	client, err := friendli.NewClient(os.Getenv("FRIENDLI_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	req := friendli.NewChatCompletionRequest(
		"meta-llama-3.1-8b-instruct",
		[]friendli.Message{
			friendli.NewSystemMessage("You are a helpful assistant"),
			friendli.NewUserMessage("What is 2+2?"),
		},
	).WithTemperature(0.7).WithMaxTokens(100)
	resp, err := client.Chat.CreateCompletion(context.Background(), req)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Choices[0].Message.Content)
}
// ExampleClient_streaming demonstrates streaming chat completion
func ExampleClient_streaming() {
	client, err := friendli.NewClient(os.Getenv("FRIENDLI_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	req := friendli.NewChatCompletionRequest(
		"meta-llama-3.1-8b-instruct",
		[]friendli.Message{
			friendli.NewUserMessage("Count from 1 to 5"),
		},
	)
	stream, err := client.Chat.CreateCompletionStream(context.Background(), req)
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Close()
	for {
		chunk, err := stream.Recv()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		if len(chunk.Choices) > 0 {
			fmt.Print(chunk.Choices[0].Delta.Content)
		}
	}
	fmt.Println()
}
// ExampleStream_collectContent demonstrates collecting entire stream
func ExampleStream_collectContent() {
	client, err := friendli.NewClient(os.Getenv("FRIENDLI_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	req := friendli.NewChatCompletionRequest(
		"meta-llama-3.1-8b-instruct",
		[]friendli.Message{
			friendli.NewUserMessage("Say hello!"),
		},
	)
	stream, err := client.Chat.CreateCompletionStream(context.Background(), req)
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Close()
	content, err := stream.CollectContent()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(content)
}
// ExampleStream_streamToChannel demonstrates using channels
func ExampleStream_streamToChannel() {
	client, err := friendli.NewClient(os.Getenv("FRIENDLI_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	req := friendli.NewChatCompletionRequest(
		"meta-llama-3.1-8b-instruct",
		[]friendli.Message{
			friendli.NewUserMessage("Tell me a joke"),
		},
	)
	stream, err := client.Chat.CreateCompletionStream(context.Background(), req)
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Close()
	chunkCh := make(chan *friendli.ChatCompletionChunk, 10)
	errCh := make(chan error, 1)
	go stream.StreamToChannel(chunkCh, errCh)
	for chunk := range chunkCh {
		if len(chunk.Choices) > 0 {
			fmt.Print(chunk.Choices[0].Delta.Content)
		}
	}
	if err := <-errCh; err != nil {
		log.Fatal(err)
	}
	fmt.Println()
}
// ExampleChatCompletionRequest_withTools demonstrates tool calling
func ExampleChatCompletionRequest_withTools() {
	client, err := friendli.NewClient(os.Getenv("FRIENDLI_API_KEY"))
	if err != nil {
		log.Fatal(err)
	}
	tools := []friendli.Tool{
		{
			Type: "function",
			Function: friendli.FunctionDef{
				Name:        "get_weather",
				Description: "Get the current weather for a location",
				Parameters: map[string]interface{}{
					"type": "object",
					"properties": map[string]interface{}{
						"location": map[string]string{
							"type":        "string",
							"description": "The city and state, e.g. San Francisco, CA",
						},
					},
					"required": []string{"location"},
				},
			},
		},
	}
	req := friendli.NewChatCompletionRequest(
		"meta-llama-3.1-8b-instruct",
		[]friendli.Message{
			friendli.NewUserMessage("What's the weather in San Francisco?"),
		},
	).WithTools(tools)
	resp, err := client.Chat.CreateCompletion(context.Background(), req)
	if err != nil {
		log.Fatal(err)
	}
	if len(resp.Choices) > 0 && len(resp.Choices[0].Message.ToolCalls) > 0 {
		toolCall := resp.Choices[0].Message.ToolCalls[0]
		fmt.Printf("Tool: %s\nArguments: %s\n", toolCall.Function.Name, toolCall.Function.Arguments)
	}
}
// ExampleAPIError demonstrates error handling
func ExampleAPIError() {
	client, err := friendli.NewClient("invalid_key")
	if err != nil {
		log.Fatal(err)
	}
	req := friendli.NewChatCompletionRequest(
		"meta-llama-3.1-8b-instruct",
		[]friendli.Message{
			friendli.NewUserMessage("Hello"),
		},
	)
	_, err = client.Chat.CreateCompletion(context.Background(), req)
	if err != nil {
		if apiErr, ok := err.(*friendli.APIError); ok {
			if apiErr.IsAuthenticationError() {
				fmt.Println("Authentication failed - check your API key")
			} else if apiErr.IsRateLimitError() {
				fmt.Println("Rate limit exceeded")
			} else {
				fmt.Printf("API error: %s\n", apiErr.Error())
			}
		} else {
			fmt.Printf("Other error: %s\n", err.Error())
		}
	}
}
No comments yet.