Skip to content

Commit

Permalink
Added comments for methods and symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Struki84 committed Aug 24, 2023
1 parent 25e4f05 commit 7dce807
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tools/metaphor/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import (
"github.com/tmc/langchaingo/tools"
)

// Documents defines a tool implementation for the Metaphor Web scrapper.
type Documents struct {
client *metaphor.Client
options []metaphor.ClientOptions
}

var _ tools.Tool = &Documents{}

// NewDocuments creates a new instance of the Documents struct.
//
// The function takes in optional metaphorm.ClientOptions as parameters.
// It returns a pointer to a Documents struct and an error.
func NewDocuments(options ...metaphor.ClientOptions) (*Documents, error) {
apiKey := os.Getenv("METAPHOR_API_KEY")

Expand All @@ -32,14 +37,26 @@ func NewDocuments(options ...metaphor.ClientOptions) (*Documents, error) {
}, nil
}

// SetOptions sets the options for the Documents struct.
//
// It takes in variadic parameter(s) of type `metaphor.ClientOptions`.
func (tool *Documents) SetOptions(options ...metaphor.ClientOptions) {
tool.options = options
}

// Name returns the name of the Documents tool.
//
// It does not take any parameters.
// It returns a string, which is the name of the tool.
func (tool *Documents) Name() string {
return "Metaphor Contents Extractor"
}

// Description returns the contents of web pages based on a list of ID strings.
//
// It is designed to be used with Metaphor Search and/or Metaphor Links Search Tool.
// The expected input format is a list of ID strings obtained from either Metaphor Search or Metaphor Search Links tool.
// The function returns a string.
func (tool *Documents) Description() string {
return `
To be used with Metaphor Search and/or Metaphor Links Search Tool.
Expand All @@ -49,6 +66,11 @@ func (tool *Documents) Description() string {
"8U71IlQ5DUTdsherhhYA,9segZCZGNjjQB2yD2uyK,..."`
}

// Call calls the Documents API with the given input and returns the formatted contents.
//
// The input is a string that contains a comma-separated list of IDs.
//
// It returns a string which represents the formatted contents and an error if any.
func (tool *Documents) Call(ctx context.Context, input string) (string, error) {
ids := strings.Split(input, ",")
for i, id := range ids {
Expand Down
22 changes: 22 additions & 0 deletions tools/metaphor/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import (
"github.com/tmc/langchaingo/tools"
)

// LinksSearch defines a tool implementation for the Metaphor Find Similar Links.
type LinksSearch struct {
client *metaphor.Client
options []metaphor.ClientOptions
}

var _ tools.Tool = &LinksSearch{}

// NewLinksSearch creates a new metaphor Search instance, that
// can be used to find similar links.
//
// It accepts an optional list of ClientOptions as parameters.
// It returns a pointer to a LinksSearch instance and an error.
func NewLinksSearch(options ...metaphor.ClientOptions) (*LinksSearch, error) {
apiKey := os.Getenv("METAPHOR_API_KEY")

Expand All @@ -33,20 +39,36 @@ func NewLinksSearch(options ...metaphor.ClientOptions) (*LinksSearch, error) {
return metaphor, nil
}

// SetOptions sets the options for the LinksSearch tool.
//
// It takes in one or more ClientOptions parameters and assigns them to the tool's options field.
func (tool *LinksSearch) SetOptions(options ...metaphor.ClientOptions) {
tool.options = options
}

// Name returns the name of the LinksSearch tool.
//
// No parameters.
// Returns a string.
func (tool *LinksSearch) Name() string {
return "Metaphor Links Search"
}

// Description returns the description of the LinksSearch tool.
//
// This function does not take any parameters.
// It returns a string that describes the purpose of the LinksSearch tool.
func (tool *LinksSearch) Description() string {
return `
Metaphor Links Search finds similar links to the link provided.
Input should be the url string for which you would like to find similar links`
}

// Call searches for similar links using the LinksSearch tool.
//
// ctx - the context in which the function is called.
// input - the string input used to find similar links, i.e. the url.
// Returns a string containing the formatted links and an error if any occurred.
func (tool *LinksSearch) Call(ctx context.Context, input string) (string, error) {
links, err := tool.client.FindSimilar(ctx, input, tool.options...)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions tools/metaphor/metaphor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ import (

var _ tools.Tool = &API{}

// API defines a tool implementation for the Metaphor API.
type API struct {
client *metaphor.Client
}

// ToolInput defines a struct the tool expects as input.
type ToolInput struct {
Operation string `json:"operation"`
Input string `json:"input"`
ReqOptions metaphor.RequestOptions `json:"reqOptions"`
}

// NewClient initializes a new API client.
//
// It retrieves the API key from the environment variable "METAPHOR_API_KEY"
// and creates a new client using the retrieved API key. If the API key is not
// set or an error occurs during client creation, an error is returned.
//
// Returns a pointer to the created API client and an error, if any.
func NewClient() (*API, error) {
apiKey := os.Getenv("METAPHOR_API_KEY")

Expand All @@ -38,10 +47,20 @@ func NewClient() (*API, error) {
}, nil
}

// Name returns the name of the tool.
//
// No parameters.
// Returns a string.
func (tool *API) Name() string {
return "Metaphor API Tool"
}

// Description returns the Description of the tool.
// Description contains a short instruction how to use the tool
// with the Metaphor API
//
// No parameters.
// Returns a string.
func (tool *API) Description() string {
return `
Metaphor API Tool is a tool to interact with the Metaphor API. Metaphor is a search engine
Expand Down Expand Up @@ -129,6 +148,21 @@ func (tool *API) Description() string {
An array of document IDs obtained from either /search or /findSimilar endpoints.`
}

// Call is a function that takes a context and an input string and returns a string and an error.
//
// The function expects a JSON string as input and unmarshals it into a ToolInput struct.
// It then performs different operations based on the value of the Operation field in the ToolInput struct.
// The supported operations are "Search", "FindSimilar", and "GetContents".
//
// If the Operation is "Search", the function calls the performSearch method passing the
// context and the ToolInput struct.
// If the Operation is "FindSimilar", the function calls the findSimilar method passing the
// context and the ToolInput struct.
// If the Operation is "GetContents", the function calls the getContents method passing the
// context and the ToolInput struct.
//
// The function returns the result of the respective operation or an empty string and nil
// if the Operation is not supported.
func (tool *API) Call(ctx context.Context, input string) (string, error) {
var toolInput ToolInput

Expand Down
20 changes: 20 additions & 0 deletions tools/metaphor/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ import (
"github.com/tmc/langchaingo/tools"
)

// Search defines a tool implementation for the Metaphor Search.
type Search struct {
client *metaphor.Client
options []metaphor.ClientOptions
}

var _ tools.Tool = &Search{}

// NewSearch creates a new Metaphot Search instance.
//
// It accepts an optional variadic parameter of type metaphor.ClientOptions.
// The function returns a pointer to a Search instance and an error.
func NewSearch(options ...metaphor.ClientOptions) (*Search, error) {
apiKey := os.Getenv("METAPHOR_API_KEY")

Expand All @@ -34,14 +39,25 @@ func NewSearch(options ...metaphor.ClientOptions) (*Search, error) {
return metaphor, nil
}

// SetOptions sets the options for the Search tool.
//
// options is a variadic parameter of type metaphor.ClientOptions.
func (tool *Search) SetOptions(options ...metaphor.ClientOptions) {
tool.options = options
}

// Name returns the name of the Search tool.
//
// This function takes no parameters.
// It returns a string.
func (tool *Search) Name() string {
return "Metaphor Search"
}

// Description returns the description of the Search tool.
//
// This function does not take any parameters.
// It returns a string that contains the description of the Search tool.
func (tool *Search) Description() string {
return `
Metaphor Search uses a transformer architecture to predict links given text,
Expand All @@ -53,6 +69,10 @@ func (tool *Search) Description() string {
`
}

// Call performs a search using the Search client.
//
// It takes a context.Context and a search query as string input as parameters.
// It returns a string and an error.
func (tool *Search) Call(ctx context.Context, input string) (string, error) {
response, err := tool.client.Search(ctx, input, tool.options...)
if err != nil {
Expand Down

0 comments on commit 7dce807

Please sign in to comment.