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

goal

package module
v0.0.3 Latest Latest
Warning

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

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

README

goal - Go Abstract Language Model Interface

goal is a Go package designed to simplify and streamline interactions with various Language Model (LLM) providers. It's built for AI engineers and developers who want a unified, flexible, and powerful interface to work with multiple LLM APIs.

Key Features

  • Unified API: Work with multiple LLM providers (OpenAI, Anthropic, Groq) through a single, consistent interface.
  • Easy Provider Switching: Seamlessly switch between different LLM providers or models with minimal code changes.
  • Flexible Configuration: Configure your LLM interactions via environment variables, code, or command-line flags.
  • High-Level AI Functions: Utilize pre-built functions for common AI tasks like question-answering, summarization, and chain-of-thought reasoning.
  • Advanced Prompt Engineering: Create sophisticated prompts with directives, context, and examples.
  • Provider Comparison: Easily compare responses from multiple LLM providers for the same prompt.
  • Extensible Architecture: Add new LLM providers with minimal effort.
  • CLI Tool: Use goal directly from the command line for quick experiments and workflows.

Installation

go get github.com/teilomillet/goal

Quick Start

Basic Usage
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/teilomillet/goal"
)

func main() {
	llm, err := goal.NewLLM(
		goal.SetProvider("openai"),
		goal.SetModel("gpt-3.5-turbo"),
		goal.SetMaxTokens(100),
		goal.SetAPIKey("your-api-key-here"),
	)
	if err != nil {
		log.Fatalf("Failed to create LLM: %v", err)
	}

	ctx := context.Background()

	prompt := goal.NewPrompt("Tell me a short joke about programming.",
		goal.WithMaxLength(50),
	)
	response, _, err := llm.Generate(ctx, prompt.String())
	if err != nil {
		log.Fatalf("Failed to generate text: %v", err)
	}
	fmt.Printf("Response: %s\n", response)
}

Advanced Usage

Prompt Types

goal supports various prompt types to cater to different use cases:

  1. Basic Prompt: Simple text input.
  2. Prompt with Directives: Guide the LLM's response.
  3. Prompt with Context: Provide background information.
  4. Prompt with Max Length: Limit response length.
  5. Prompt with Examples: Provide example inputs/outputs.

Example:

directivePrompt := goal.NewPrompt("Explain the concept of recursion",
	goal.WithDirectives("Use a simple example to illustrate", "Keep it concise"),
	goal.WithOutput("Explanation of recursion:"),
)
Comparing Providers

Easily compare responses from different LLM providers:

providers := []string{"openai", "anthropic"}
llms := make(map[string]goal.LLM)

for _, provider := range providers {
	llm, _ := goal.NewLLM(
		goal.SetProvider(provider),
		goal.SetMaxTokens(100),
		goal.SetAPIKey("your-api-key-here"),
	)
	llms[provider] = llm
}

question := "What is the capital of France?"
for provider, llm := range llms {
	answer, _ := goal.QuestionAnswer(ctx, llm, question)
	fmt.Printf("%s answer: %s\n", provider, answer)
}
Advanced Prompt Templates

Create reusable prompt templates for complex tasks:

advancedPromptTemplate := goal.NewPromptTemplate(
	"AdvancedAnalysis",
	"Analyze a topic from multiple perspectives",
	"Analyze the following topic from multiple perspectives: {{.Topic}}",
	goal.WithPromptOptions(
		goal.WithDirectives(
			"Consider technological, economic, social, and ethical implications",
			"Provide at least one potential positive and one potential negative outcome for each perspective",
			"Conclude with a balanced summary of no more than 3 sentences",
		),
		goal.WithOutput("Multi-perspective Analysis:"),
		goal.WithMaxLength(300),
	),
)

prompt, _ := advancedPromptTemplate.Execute(map[string]interface{}{
	"Topic": "The widespread adoption of artificial intelligence in healthcare",
})

analysis, _, _ := llmClient.Generate(ctx, prompt.String())
fmt.Printf("Analysis:\n%s\n", analysis)

Performance Considerations

While goal adds a thin abstraction layer, its impact on performance is minimal. The main performance factors will be the responsiveness of the chosen LLM provider and the complexity of your prompts.

Streaming Support

Currently, goal does not support streaming responses. This feature is on our roadmap for future development.

Project Status

goal is actively maintained and under continuous development. We welcome contributions and feedback from the community.

Examples and Tutorials

Check out our examples directory for more usage examples, including:

  • Basic usage
  • Different prompt types
  • Comparing providers
  • Advanced prompt templates
  • Combining multiple goal features

Contributing

We welcome contributions! Please see our Contributing Guide for more information on how to get started.

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0) - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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:"),
	),
)

Functions

func ChainOfThought

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

ChainOfThought performs a chain of thought reasoning

func QuestionAnswer

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

QuestionAnswer performs question answering

func Summarize

func Summarize(ctx context.Context, l LLM, text string, opts ...PromptOption) (string, error)

Summarize performs text summarization

Types

type ConfigOption added in v0.0.3

type ConfigOption func(*llm.Config)

ConfigOption is a function type that modifies a Config

func SetAPIKey added in v0.0.3

func SetAPIKey(key string) ConfigOption

SetAPIKey sets the API key for the current provider in the Config

func SetMaxTokens added in v0.0.3

func SetMaxTokens(maxTokens int) ConfigOption

SetMaxTokens sets the max tokens in the Config

func SetModel added in v0.0.3

func SetModel(model string) ConfigOption

SetModel sets the model in the Config

func SetProvider added in v0.0.3

func SetProvider(provider string) ConfigOption

SetProvider sets the provider in the Config

func SetTemperature added in v0.0.3

func SetTemperature(temperature float64) ConfigOption

SetTemperature sets the temperature in the Config

func SetTimeout added in v0.0.3

func SetTimeout(timeout time.Duration) ConfigOption

SetTimeout sets the timeout in the Config

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{})
}

func NewLLM

func NewLLM(opts ...ConfigOption) (LLM, error)

NewLLM creates a new LLM instance with the provided config options

type Prompt

type Prompt struct {
	Input      string
	Output     string
	Directives []string
	Context    string
	MaxLength  int
	Examples   []string
}

Prompt represents a structured prompt for an LLM

func NewPrompt

func NewPrompt(input string, opts ...PromptOption) *Prompt

NewPrompt creates a new Prompt with the given input and applies any provided options

func (*Prompt) Apply added in v0.0.3

func (p *Prompt) Apply(opts ...PromptOption)

Apply applies the given options to the Prompt

func (*Prompt) String

func (p *Prompt) String() string

String returns the formatted prompt as a string

type PromptOption added in v0.0.3

type PromptOption func(*Prompt)

PromptOption is a function type that modifies a Prompt

func WithContext added in v0.0.3

func WithContext(context string) PromptOption

WithContext adds context to the Prompt

func WithDirectives added in v0.0.3

func WithDirectives(directives ...string) PromptOption

WithDirectives adds directives to the Prompt

func WithExamples added in v0.0.3

func WithExamples(examples ...string) PromptOption

WithExamples adds examples to the Prompt

func WithMaxLength added in v0.0.3

func WithMaxLength(length int) PromptOption

WithMaxLength sets the maximum length for the output

func WithOutput added in v0.0.3

func WithOutput(output string) PromptOption

WithOutput adds an output specification to the Prompt

type PromptTemplate added in v0.0.3

type PromptTemplate struct {
	Name        string
	Description string
	Template    string
	Options     []PromptOption
}

PromptTemplate represents a template for generating prompts

func NewPromptTemplate added in v0.0.3

func NewPromptTemplate(name, description, template string, opts ...PromptTemplateOption) *PromptTemplate

NewPromptTemplate creates a new PromptTemplate with the given name, description, and template

func (*PromptTemplate) Execute added in v0.0.3

func (pt *PromptTemplate) Execute(data map[string]interface{}) (*Prompt, error)

Execute generates a Prompt from the PromptTemplate with the given data

type PromptTemplateOption added in v0.0.3

type PromptTemplateOption func(*PromptTemplate)

PromptTemplateOption is a function type that modifies a PromptTemplate

func WithPromptOptions added in v0.0.3

func WithPromptOptions(options ...PromptOption) PromptTemplateOption

WithPromptOptions adds PromptOptions to the PromptTemplate

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