gosk

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: MIT Imports: 9 Imported by: 0

README

gosk - Go Semantic Kernel

⚠️ Please note that gosk is currently in a very early stage of development. As such, there may be bugs or incomplete features. Your understanding and patience are much appreciated. I warmly welcome any contributions, suggestions, or bug reports to help improve the module.

Go Semantic Kernel (gosk) is a Go module inspired by the Microsoft Semantic Kernel, originally designed to facilitate the integration of OpenAI's API and large language models in C# and Python applications. gosk extends this functionality to the Go programming language, thereby empowering Go developers to define and utilize AI skills and functions with ease, bringing the power of OpenAI's advanced language models to a broader programming community.

Using prompt engineering, gosk enables developers to define AI skills and functions, which can then be easily utilized within their applications.

See also: https://github.com/microsoft/semantic-kernel

Installation

To install the gosk module, use the following command:

go get github.com/mfmayer/gosk

Usage

Here's an example of how you can use gosk to import an AI skill and use it in your application:

package main

import (
	"fmt"
	"testing"

	"github.com/mfmayer/gosk"
)

func main() {
	kernel, err := gosk.NewKernel()
	if err != nil {
		t.Fatal(err)
	}
	skill, err := kernel.ImportSkill("FunSkill")
	if err != nil {
		t.Fatal(err)
	}
	sf := skill["Joke"]
	response, err := sf("Engineer", "")
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("%v\n", response)
}

In the example above, a new Semantic Kernel is created, and a skill named "FunSkill" is imported into it. This skill contains a function Joke which takes two arguments, a subject and an additional context (which is left empty in this example). It returns a joke related to the subject, in this case, an "Engineer".

The resulting joke is printed to the console. E.g.:

Why did the engineer install a doorbell on his desk?

Because he wanted to hear when opportunity knocked!

Contributing

Contributions to the gosk project are welcome! If you have a bug to report, a feature to suggest, or a patch to submit, please feel free to use the GitHub issue tracker or submit a pull request.

License

The gosk module is open source software released under the MIT License (like semantic kernel from microsoft). For more details, see the LICENSE file in the repository.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingParameter is returned when a required parameter is missing
	ErrMissingParameter = errors.New("missing parameter")
)

Functions

func NewDefaultSemanticFunctionCall added in v0.1.0

func NewDefaultSemanticFunctionCall(promptTemplate *template.Template, generator llm.Generator) (skillFunc func(input llm.Content) (response llm.Content, err error))

NewDefaultSemanticFunctionCall creates a new semantic skill function with a prompt template and a generator

func ParseSemanticFunctionsFromFS added in v0.1.0

func ParseSemanticFunctionsFromFS(fsys fs.FS, generators map[string]llm.Generator, options ...createSemanticFunctionsOption) (functions map[string]*Function, err error)

func WithCustomCall added in v0.1.0

func WithCustomCall(createSemanticFunctionCall func(promptTemplate *template.Template, generator llm.Generator) (skillFunc func(input llm.Content) (response llm.Content, err error))) (option parseSemanticFunctionFromFSOption)

WithCustomCall allows to create a custom semantic function call while parsing a semantic function with ParseSemanticFunctionFromFS

func WithCustomCallForFunc added in v0.1.0

func WithCustomCallForFunc(funcName string, createSemanticFunctionCall func(promptTemplate *template.Template, generator llm.Generator) (skillFunc func(input llm.Content) (response llm.Content, err error))) (option createSemanticFunctionsOption)

WithCustomCallForFunc allows to create selectively custom semantic function calls while parsing multiple semantic functions with ParseSemanticFunctionsFromFS

Types

type Function

type Function struct {
	// Name of the SkillFunction
	Name string `json:"name,omitempty"`
	// Description what the SkillFunction is doing
	Description string `json:"description"`
	// Plannable indicates whether the skill function can be planned by the semantic kernel
	Plannable bool `json:"plannable,omitempty"`
	// InputProperties map whose keys are the input property names and whose values are the input property definitions
	InputProperties map[string]*Parameter `json:"inputProperties"`
	// call holds the function that is executed when the skill function is called
	Call func(input llm.Content) (output llm.Content, err error) `json:"-"`
}

Function defines and describes a skill's function with its input properties and its actual function call

func ParseSemanticFunctionFromFS

func ParseSemanticFunctionFromFS(fsys fs.FS, generators map[string]llm.Generator, options ...parseSemanticFunctionFromFSOption) (function *Function, err error)

ParseFunctionFromFS finds "config.json" with function comfiguration. Prompt templates will be created from "*.tmpl" files with at least "skprompt.tmpl" is needed

type Parameter

type Parameter struct {
	Name        string      `json:"name,omitempty"`
	Description string      `json:"description"`
	Type        Type        `json:"type,omitempty"`
	Enum        []string    `json:"enum,omitempty"`
	Required    bool        `json:"required,omitempty"`
	Default     interface{} `json:"default,omitempty"`
}

Parameter defines a function's parameter with its name, description and type

type SemanticKernel

type SemanticKernel struct {
	// contains filtered or unexported fields
}

SemanticKernel

func NewKernel

func NewKernel(opts ...newKernelOption) *SemanticKernel

NewKernel creates new kernel and tries to retrieve the OpenAI key from "OPENAI_API_KEY" environment variable or .env file in current working directory

func (*SemanticKernel) AddSkills

func (sk *SemanticKernel) AddSkills(skills ...*Skill) (err error)

AddSkills adds already initialized skills to the kernel with their individual names

func (*SemanticKernel) Call

func (sk *SemanticKernel) Call(skillName string, skillFunction string, input llm.Content) (response llm.Content, err error)

Call a skill function with given name and input. Returns the response and/or an error The kernel also links the input as predecessor to the response

func (*SemanticKernel) ChainCall

func (sk *SemanticKernel) ChainCall(context llm.Content, functions ...*Function) (response llm.Content, err error)

ChainCall to call multiple functions in a row context is passed to all functions and context["data"] is updated with the response of each function

func (*SemanticKernel) FindFunction

func (sk *SemanticKernel) FindFunction(skillName string, skillFunction string) (function *Function, ok bool)

FindFunction finds a function in a skill by name and returns it or an error if not found

func (*SemanticKernel) FindFunctions

func (sk *SemanticKernel) FindFunctions(functionPaths ...string) (functions []*Function, err error)

FindFunctions finds functions with path notation (`skillName.functionName`) and returns them or an error if any function is not found

func (*SemanticKernel) FindSkill

func (sk *SemanticKernel) FindSkill(skillName string) (skill *Skill, ok bool)

FindSkill finds a skill by name and returns it or an error if not found

func (*SemanticKernel) RegisterGeneratorFactories added in v0.1.0

func (sk *SemanticKernel) RegisterGeneratorFactories(factories ...llm.GeneratorFactory)

func (*SemanticKernel) RegisterSkills added in v0.1.0

func (sk *SemanticKernel) RegisterSkills(skillFactories ...SkillFactoryFunc) (err error)

RegisterSkills creates new skills with their factories and adds them to the kernel with their individual names

type Skill

type Skill struct {
	// Name of the skill
	Name string `json:"name,omitempty"`
	// Description of the skill should indicate and give an idea about the functions that are included
	Description string `json:"description"`
	// Plannable indicates whether the skill can be planned by the semantic kernel
	Plannable bool `json:"plannable,omitempty"`
	// Functions that the skill provides
	Functions map[string]*Function `json:"functions"`
	// Generators that might be initialized while parsing a skill from a configuration
	Generators map[string]llm.Generator `json:"-"`
}

Skill defines and holds a collection of Skill Functions that can be planned and called by the semantic kernel

func ParseSemanticSkillFromFS

func ParseSemanticSkillFromFS(fsys fs.FS, generatorFactories llm.GeneratorFactoryMap, options ...createSemanticFunctionsOption) (skill *Skill, err error)

ParseSemanticSkillFromFS parses a skill from fsys file system (see assets/skills for examples). Given generatorFactories are used to create and return generators that are configured for this skill.

func (*Skill) Call

func (s *Skill) Call(functionName string, input llm.Content) (response llm.Content, err error)

Call a skill function with given name and input content

func (*Skill) String added in v0.1.0

func (s *Skill) String() string

type SkillFactoryFunc added in v0.1.0

type SkillFactoryFunc func(generatorFactories llm.GeneratorFactoryMap) (skill *Skill, err error)

SkillFactoryFunc to create a semantic skill with the help of given generators

type Type

type Type string
const (
	TypeString  Type = "string"
	TypeNumber  Type = "number"
	TypeInteger Type = "integer"
	TypeObject  Type = "object"
	TypeArray   Type = "array"
	TypeBoolean Type = "boolean"
	TypeNull    Type = "null"
)

Directories

Path Synopsis
cmd
pkg
gpt
llm

Jump to

Keyboard shortcuts

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