Sumarize

The Summarize function demonstrates how to use prompt templates and options in gollm to create a flexible, reusable function for text summarization. It allows users to customize the summarization process by passing additional options, as shown in the example where a maximum length and specific directives are provided.

This example showcases how gollm can be used to create high-level AI functions that encapsulate common NLP tasks, making it easy for developers to integrate these capabilities into their applications.

First, let's look at the summarize_example.go file:

  1. Initializing the LLM Client: The example starts by creating an LLM instance:

    llmClient, err := gollm.NewLLM()
    if err != nil {
        log.Fatalf("Failed to create LLM client: %v", err)
    }

    This creates a new LLM client with default settings.

  2. Defining the Text to Summarize: A sample text about artificial intelligence is defined:

    text := `Artificial intelligence (AI) is transforming various sectors of society, including healthcare, 
    finance, and transportation. While AI offers numerous benefits such as improved efficiency and 
    decision-making, it also raises concerns about privacy, job displacement, and ethical considerations. 
    As AI continues to advance, it's crucial to address these challenges and ensure responsible development 
    and deployment of AI technologies.`
  3. Using the Summarize Function: The example uses the gollm.Summarize function to generate a summary:

    summary, err := gollm.Summarize(ctx, llmClient, text,
        gollm.WithMaxLength(50),
        gollm.WithDirectives(
            "Provide a concise summary",
            "Capture the main points and key details",
            "Focus on the main impacts and challenges",
        ),
    )

    This call to Summarize includes options to limit the summary to 50 words and provides specific directives for the summarization process.

Now, let's look at the implementation of the Summarize function in the summarize.go file:

  1. Prompt Template Definition: A prompt template for summarization is defined:

    var summarizeTemplate = NewPromptTemplate(
        "Summarize",
        "Summarize the given text",
        "Summarize the following text:\n\n{{.Text}}",
        WithPromptOptions(
            WithDirectives(
                "Provide a concise summary",
                "Capture the main points and key details",
            ),
            WithOutput("Summary:"),
        ),
    )

    This template includes default directives and specifies an output format.

  2. Summarize Function: The Summarize function is implemented as follows:

    func Summarize(ctx context.Context, l LLM, text string, opts ...PromptOption) (string, error) {
        if ctx == nil {
            ctx = context.Background()
        }
    
        prompt, err := summarizeTemplate.Execute(map[string]interface{}{
            "Text": text,
        })
        if err != nil {
            return "", fmt.Errorf("failed to execute summarize template: %w", err)
        }
    
        prompt.Apply(opts...)
    
        response, err := l.Generate(ctx, prompt)
        if err != nil {
            return "", fmt.Errorf("failed to generate ...: %w", err)
        }
    
        return response, nil
    }

    This function:

    • Executes the summarize template with the provided text

    • Applies any additional options passed to the function

    • Generates a response using the LLM client

    • Returns the generated summary

Last updated