Question Answering

This example demonstrates how to use the QuestionAnswer function in gollm to create a flexible question-answering system. It showcases the ability to provide context, examples, and specific directives to guide the LLM in generating a relevant and concise answer.

The use of prompt templates and options in both the implementation and usage of the QuestionAnswer function highlights the flexibility and power of the gollm package in handling various NLP tasks.

Let's start with the question_answer.go file:

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

    var QuestionAnswerTemplate = NewPromptTemplate(
        "QuestionAnswer",
        "Answer the given question",
        "Answer the following question:\n\n{{.Question}}",
        WithPromptOptions(
            WithDirectives("Provide a clear and concise answer"),
            WithOutput("Answer:"),
        ),
    )

    This template includes a default directive to provide a clear and concise answer, and specifies an output format.

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

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

    • Applies any additional options passed to the function

    • Generates a response using the LLM client

    • Returns the generated answer

Now, let's look at the question_answer_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(500),
    )
  2. Defining the Question and Context: A question about quantum computing challenges is defined, along with some context information:

    question := "What are the main challenges in quantum computing?"
    contextInfo := `Quantum computing is an emerging field that uses quantum-mechanical phenomena such as 
    superposition and entanglement to perform computation. It has the potential to solve certain problems 
    much faster than classical computers, particularly in areas like cryptography, optimization, and 
    simulation of quantum systems.`
  3. Using the QuestionAnswer Function: The example uses the gollm.QuestionAnswer function to generate an answer:

    response, err := gollm.QuestionAnswer(ctx, llmClient, question,
        gollm.WithContext(contextInfo),
        gollm.WithExamples("Challenge: Decoherence, Solution: Error correction techniques"),
        gollm.WithMaxLength(200),
        gollm.WithDirectives(
            "Provide a concise answer",
            "Address the main challenges mentioned in the question",
        ),
    )

    This call to QuestionAnswer includes several options:

    • WithContext: Provides additional context about quantum computing

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

    • WithMaxLength: Limits the answer to 200 words

    • WithDirectives: Provides specific instructions for answering the question

  4. Displaying the Results: Finally, the example prints the question, context, and the generated answer.

Last updated