tools

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-openai-tools

Go Reference CI

This is a library to make tool definition and tool calls easy. The library is intended to be used with sashabaranov/go-openai.

Installation

go get -u github.com/southball/go-openai-tools

Example

package main

import (
	"context"
	"fmt"
	"os"
	"slices"

	openai "github.com/sashabaranov/go-openai"
	tools "github.com/southball/go-openai-tools"
)

type GetWeatherRequest struct {
	Location string `json:"location" required:"true" description:"City and country e.g. Bogotá, Colombia"`
}

type GetWeatherResponse struct {
	Weather string `json:"weather"`
}

func GetWeather(_ GetWeatherRequest) (GetWeatherResponse, error) {
	return GetWeatherResponse{Weather: "sunny"}, nil
}

const MODEL = "gpt-4o"

func main() {
	toolset, err := tools.NewToolSet(
		tools.F("GetWeather", "Get the current weather for a location.", GetWeather),
	)
	if err != nil {
		panic(fmt.Errorf("failed to create toolset: %w", err))
	}

	client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
	initialMessages := []openai.ChatCompletionMessage{
		{Role: openai.ChatMessageRoleSystem, Content: "You are a helpful assistant."},
		{Role: openai.ChatMessageRoleUser, Content: "What is the weather like in Bogotá, Colombia?"},
	}

	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model:    MODEL,
			Messages: initialMessages,
			Tools:    toolset.OpenAITools(),
		},
	)
	if err != nil {
		panic(fmt.Errorf("failed to create chat completion: %w", err))
	}

	answer := resp.Choices[0].Message
	toolCallMessages, err := toolset.HandleToolCalls(context.Background(), answer.ToolCalls)
	if err != nil {
		panic(fmt.Errorf("failed to handle tool calls: %w", err))
	}

	resp, err = client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model:    MODEL,
			Messages: slices.Concat(initialMessages, []openai.ChatCompletionMessage{answer}, toolCallMessages),
		},
	)
	if err != nil {
		panic(fmt.Errorf("failed to create chat completion: %w", err))
	}

	fmt.Printf("Final response: %s\n", resp.Choices[0].Message.Content)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleToolCall

func HandleToolCall(tool Tool, toolCall openai.ToolCall) (openai.ChatCompletionMessage, error)

Handles a single openai.ToolCall using the provided Tool.

func OpenAITool

func OpenAITool(tool Tool) (openai.Tool, error)

Convert a Tool to an openai.Tool.

Types

type Tool

type Tool interface {
	// Name of the tool.
	Name() string
	// Description of the tool.
	Description() string
	// Type of the request.
	// The type should have struct tags as specified in https://pkg.go.dev/github.com/swaggest/jsonschema-go#Reflector.Reflect.
	RequestType() reflect.Type
	// Accepts argument of the type returned by RequestType(). Returns any value or an error.
	CallFunction(args any) (any, error)
}

A Tool is an interface that can be used to derive an openai.Tool and also handle openai.ToolCall.

func F

func F[U, V any](name string, description string, callFunc func(args U) (V, error)) Tool

Create a Tool with the given name, description, and function.

type ToolSet

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

A collection of Tool.

func NewToolSet

func NewToolSet(toolset ...Tool) (*ToolSet, error)

Construct a ToolSet from the given list of Tool.

func (ToolSet) HandleToolCalls

func (t ToolSet) HandleToolCalls(
	ctx context.Context,
	toolCalls []openai.ToolCall,
) ([]openai.ChatCompletionMessage, error)

Process a list of openai.ToolCall and return a list of openai.ChatCompletionMessage corresponding to the results of those calls.

func (ToolSet) OpenAITools

func (t ToolSet) OpenAITools() []openai.Tool

Get a list of openai.Tool from the ToolSet.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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