Help us shape the future of Go! Take the 2026 Go Developer Survey

goal

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

README

goal - Go Abstract Language Model Interface

goal is a Go package that provides a simple, unified interface for interacting with various Language Model (LLM) providers. It abstracts away the differences between different LLM APIs, allowing you to easily switch between providers or use multiple providers in your application.

Features

  • Unified interface for multiple LLM providers (Anthropic, OpenAI, etc.)
  • Flexible and powerful prompt creation and management
  • High-level functions for common tasks (QuestionAnswer, Summarize, etc.)
  • Easy configuration management
  • Support for advanced prompt engineering techniques

Installation

To install the goal package, use go get:

go get github.com/teilomillet/goal

Quick Start

Here's a simple example to get you started:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/teilomillet/goal"
)

func main() {
	// Create a new LLM client
	llm, err := goal.NewLLM("")
	if err != nil {
		log.Fatalf("Failed to create LLM client: %v", err)
	}

	// Create a prompt
	prompt := goal.NewPrompt("Explain the concept of recursion").
		Directive("Use a simple example").
		Output("Explanation:")

	// Generate a response
	ctx := context.Background()
	response, _, err := llm.Generate(ctx, prompt.String())
	if err != nil {
		log.Fatalf("Failed to generate response: %v", err)
	}

	fmt.Printf("Response: %s\n", response)
}

Advanced Prompt Usage

The Prompt struct in goal provides a powerful way to create sophisticated prompts. Here are some of the key features:

Adding Directives

Directives guide the LLM on how to approach the task:

prompt := goal.NewPrompt("Analyze the impact of AI on healthcare").
	Directive("Consider both positive and negative impacts").
	Directive("Provide specific examples where possible")
Specifying Output Format

You can specify the desired output format:

prompt := goal.NewPrompt("List the top 5 programming languages of 2023").
	Output("Numbered list:")
Adding Context

Provide additional context to inform the LLM's response:

prompt := goal.NewPrompt("Summarize the main points").
	Context("The following text is from a research paper on climate change:").
	Input("...") // Your input text here
Setting Maximum Length

Limit the length of the LLM's response:

prompt := goal.NewPrompt("Describe the process of photosynthesis").
	MaxLength(100) // Limit to approximately 100 words
Including Examples

You can include examples to guide the LLM's output format or style:

prompt := goal.NewPrompt("Generate a creative name for a tech startup").
	Examples("path/to/examples.txt", 3, "random")

Note: The Examples method reads examples from a file. Make sure to prepare this file beforehand.

Combining Features

You can combine these features to create highly specific prompts:

prompt := goal.NewPrompt("Analyze the future of electric vehicles").
	Directive("Consider technological, economic, and environmental factors").
	Directive("Provide both optimistic and pessimistic scenarios").
	Context("Recent advancements in battery technology have led to increased range and decreased charging times for electric vehicles.").
	Output("Analysis:").
	MaxLength(200)

Using Prompts with LLM

Once you've created a prompt, you can use it with the LLM client:

response, _, err := llm.Generate(ctx, prompt.String())
if err != nil {
	log.Fatalf("Failed to generate response: %v", err)
}
fmt.Printf("Response: %s\n", response)

Configuration

Create a YAML configuration file in ~/.goal/configs/:

provider: anthropic
model: claude-3-opus-20240229
temperature: 0.7
max_tokens: 100
log_level: info

Then, use it when creating an LLM client:

llm, err := goal.NewLLM("path/to/config.yaml")

Contributing

Contributions are welcome! Please read our Contributing Guide for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChainOfThought

func ChainOfThought(ctx context.Context, l LLM, question string) (string, error)

ChainOfThought performs a chain of thought reasoning

func QuestionAnswer

func QuestionAnswer(ctx context.Context, l LLM, question string) (string, error)

QuestionAnswer performs question answering

func Summarize

func Summarize(ctx context.Context, l LLM, text string, maxLength int) (string, error)

Summarize performs text summarization

Types

type Example

type Example struct {
	Content string `json:"content"`
}

type LLM

type LLM interface {
	Generate(ctx context.Context, prompt string) (response string, fullPrompt string, err error)
	SetOption(key string, value interface{})
}

LLM represents the main interface for interacting with language models

func NewLLM

func NewLLM(configPath string, logLevel ...string) (LLM, error)

NewLLM creates a new LLM instance from a config file

type Prompt

type Prompt struct {
	*llm.Prompt
	// contains filtered or unexported fields
}

Prompt represents a structured prompt for an LLM

func NewPrompt

func NewPrompt(input string) *Prompt

NewPrompt creates a new Prompt

func (*Prompt) Context

func (p *Prompt) Context(context string) *Prompt

Context adds context to the Prompt

func (*Prompt) Directive

func (p *Prompt) Directive(directive string) *Prompt

Directive adds a directive to the Prompt

func (*Prompt) Examples

func (p *Prompt) Examples(filePath string, n int, order string) *Prompt

Examples adds examples to the Prompt

func (*Prompt) Input

func (p *Prompt) Input(input string) *Prompt

Input adds or updates the input of the Prompt

func (*Prompt) MaxLength

func (p *Prompt) MaxLength(length int) *Prompt

MaxLength sets the maximum length for the output

func (*Prompt) Output

func (p *Prompt) Output(output string) *Prompt

Output adds an output specification to the Prompt

func (*Prompt) String

func (p *Prompt) String() string

String returns the formatted prompt as a string

Directories

Path Synopsis
cmd
goal command
internal
llm

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL