gogent

package module
v0.0.0-...-d8c7f78 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 9 Imported by: 0

README

Gogent - Simple Go(pher) Agents

Define your Gophers (agents) once and re-use them for various AI tasks!

Features

  • Define custom Gophers (agents) with specific roles and goals
  • Integrate with popular AI models like OpenAI's GPT-3.5 or GPT-4o
  • Create tasks with descriptions and expected outputs
  • Handle task dependencies and process tasks in a context

Install

go get github.com/ignoxx/gogent

Use

package main

import (
	"context"
	"fmt"
	"log/slog"

	"github.com/ignoxx/gogent"
)

func main() {
	jokesWriter := gogent.NewGopher().
		WithRole("Professional Jokes Writer").
		WithGoal("Writing exceptional funny and not well-known jokes about programmers").
		WithBackstory("You work at a leading tech think tank. Your expertise lies in identifying trending jokes")

	gpt35 := gogent.NewLLM(
		gogent.WithProvider(gogent.ProviderOpenAI),
		gogent.WithModel(gogent.ModelOpenAIGpt35),
		gogent.WithApiKey("YOUR-API-KEY"),
	)

	task1 := gogent.NewTask().
		WithLLM(gpt35).
		WithGopher(jokesWriter).
		WithCreativity(gogent.ExtremelyCreative).
		WithDescription("Write 10 jokes").
		WithExpectedOutput(`All jokes must be returned in a single valid JSON array like: ["joke1", "joke2", "joke3", ...]`)

	ctx := context.Background()
	if err := task1.Process(ctx); err != nil {
		slog.Error("task failed", slog.String("description", task1.Description), slog.String("err", err.Error()))
	}

	fmt.Println(task1.Output)
}

Check examples/ for more.

Documentation

Index

Constants

View Source
const (
	ModelOpenAIGpt35 = oai.GPT3Dot5Turbo0613
	ModelOpenAIGpt4o = oai.GPT4o
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Creativity

type Creativity float32

Also known as Temperature

const (
	NotCreative       Creativity = 0.0
	SlightlyCreative  Creativity = 0.5
	SomewhatCreative  Creativity = 1.0
	VeryCreative      Creativity = 1.5
	ExtremelyCreative Creativity = 2.0
)

type Gopher

type Gopher struct {
	Role      string
	Goal      string
	Backstory string
}

func NewGopher

func NewGopher() *Gopher

Create a new Agent

func (*Gopher) WithBackstory

func (g *Gopher) WithBackstory(backstory string) *Gopher

func (*Gopher) WithGoal

func (g *Gopher) WithGoal(goal string) *Gopher

func (*Gopher) WithRole

func (g *Gopher) WithRole(role string) *Gopher

type LLM

type LLM struct {
	CompletionTokensUsed int
	PromptTokensUsed     int
	TotalTokensUsed      int
	// contains filtered or unexported fields
}

func NewLLM

func NewLLM(opts ...LLMOpt) *LLM

func (*LLM) Client

func (llm *LLM) Client() clients.AIClient

type LLMOpt

type LLMOpt func(*LLM) *LLM

func WithApiKey

func WithApiKey(apiKey string) LLMOpt

func WithModel

func WithModel(model Model) LLMOpt

func WithProvider

func WithProvider(provider Provider) LLMOpt

type Model

type Model string

type Provider

type Provider int
const (
	ProviderOpenAI Provider = iota + 1
)

type Task

type Task struct {
	Description    string
	ExpectedOutput string
	Output         string
	Creativity     float32
	Dependencies   []*Task
	Gopher         *Gopher
	LLM            *LLM
}

func NewTask

func NewTask() *Task

func (*Task) CanProcess

func (t *Task) CanProcess() bool

func (*Task) IsDone

func (t *Task) IsDone() bool

func (*Task) OutputToJSON

func (t *Task) OutputToJSON() (string, error)

Tries to marshal the output to a json string

func (*Task) OutputToJSONStruct

func (t *Task) OutputToJSONStruct(out any) error

`out` must be a struct with json tags matching the ExpectedOutput

func (*Task) Process

func (t *Task) Process(ctx context.Context) error

func (*Task) Reader

func (t *Task) Reader() io.Reader

func (*Task) WithCreativity

func (t *Task) WithCreativity(c Creativity) *Task

func (*Task) WithDependencies

func (t *Task) WithDependencies(dependencies ...*Task) *Task

func (*Task) WithDescription

func (t *Task) WithDescription(description string) *Task

func (*Task) WithExpectedOutput

func (t *Task) WithExpectedOutput(expectedOutput string) *Task

func (*Task) WithGopher

func (t *Task) WithGopher(gopher *Gopher) *Task

func (*Task) WithLLM

func (t *Task) WithLLM(llm *LLM) *Task

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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