Chain of Thought

This example demonstrates how to use the ChainOfThought function in gollm to create a flexible system for complex reasoning tasks. It showcases the ability to provide context, examples, and specific directives to guide the LLM in generating a step-by-step thought process.

The use of prompt templates and options in both the implementation and usage of the ChainOfThought function highlights the flexibility of the gollm package in handling advanced cognitive tasks. This approach is particularly useful for breaking down complex problems, explaining reasoning processes, and generating more transparent and interpretable AI outputs.

Let's start with the chain_of_thought.go file:

  1. Prompt Template Definition: A prompt template for chain of thought reasoning is defined:

    var chainOfThoughtTemplate = NewPromptTemplate(
        "ChainOfThought",
        "Perform a chain of thought reasoning",
        "Perform a chain of thought reasoning for the following question:\n\n{{.Question}}",
        WithPromptOptions(
            WithDirectives(
                "Break down the problem into steps",
                "Show your reasoning for each step",
            ),
            WithOutput("Chain of Thought:"),
        ),
    )

    This template includes default directives to break down the problem and show reasoning, and specifies an output format.

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

    func ChainOfThought(ctx context.Context, l LLM, question string, opts ...PromptOption) (string, error) {
        if ctx == nil {
            ctx = context.Background()
        }
    
        prompt, err := chainOfThoughtTemplate.Execute(map[string]interface{}{
            "Question": question,
        })
        if err != nil {
            return "", fmt.Errorf("failed to execute chain of thought 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 chain of thought template with the provided question

    • Applies any additional options passed to the function

    • Generates a response using the LLM client

    • Returns the generated chain of thought reasoning

Now, let's look at the chain_of_thought_example.go file:

  1. Initializing the LLM Client: The example starts by creating an LLM instance with a maximum token limit:

    llmClient, err := gollm.NewLLM(
        gollm.SetMaxTokens(300),
    )
  2. Defining the Question: A question about climate change's effect on agriculture is defined:

    question := "How might climate change affect global agriculture?"
  3. Using the ChainOfThought Function: The example uses the gollm.ChainOfThought function to generate a chain of thought reasoning:

    response, err := gollm.ChainOfThought(ctx, llmClient, question,
        gollm.WithMaxLength(300),
        gollm.WithContext("Climate change is causing global temperature increases and changing precipitation patterns."),
        gollm.WithExamples("Effect: Shifting growing seasons, Adaptation: Developing heat-resistant crops"),
        gollm.WithDirectives(
            "Break down the problem into steps",
            "Show your reasoning for each step",
        ),
    )

    This call to ChainOfThought includes several options:

    • WithMaxLength: Limits the response to 300 words

    • WithContext: Provides additional context about climate change

    • WithExamples: Gives an example of how to structure the reasoning

    • WithDirectives: Provides specific instructions for the chain of thought process

  4. Displaying the Results: Finally, the example prints the question and the generated chain of thought response.

Last updated