Skip to content

Commit

Permalink
chore: add tools
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed May 22, 2024
1 parent c8cf9b3 commit 66a58f5
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tools/duckduckgo/duckduckgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Tool struct {
}

type Input struct {
Query string `json:"python_code" jsonschema:"description=the query to search for"`
Query string `json:"query" jsonschema:"description=the query to search for"`
}

type Output struct {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (t *Tool) Name() string {
}

func (t *Tool) Description() string {
return "A tool that searches DuckDuckGo for a query."
return "A tool that searches on duckduckgo internet search engine for a query."
}

func (t *Tool) Fn() any {
Expand Down
60 changes: 60 additions & 0 deletions tools/llm/llm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package llm

import (
"context"

"github.com/henomis/lingoose/thread"
)

type LLM interface {
Generate(context.Context, *thread.Thread) error
}

type Tool struct {
llm LLM
}

func New(llm LLM) *Tool {
return &Tool{
llm: llm,
}
}

type Input struct {
Query string `json:"query" jsonschema:"description=user query"`
}

type Output struct {
Error string `json:"error,omitempty"`
Result string `json:"result,omitempty"`
}

type FnPrototype func(Input) Output

func (t *Tool) Name() string {
return "llm"
}

func (t *Tool) Description() string {
return "A tool that uses a language model to generate a response to a user query."
}

func (t *Tool) Fn() any {
return t.fn
}

//nolint:gosec
func (t *Tool) fn(i Input) Output {
th := thread.New().AddMessage(
thread.NewUserMessage().AddContent(
thread.NewTextContent(i.Query),
),
)

err := t.llm.Generate(context.Background(), th)
if err != nil {
return Output{Error: err.Error()}
}

return Output{Result: th.LastMessage().Contents[0].AsString()}
}
54 changes: 54 additions & 0 deletions tools/rag/rag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rag

import (
"context"
"strings"

"github.com/henomis/lingoose/rag"
)

type Tool struct {
rag *rag.RAG
topic string
}

func New(rag *rag.RAG, topic string) *Tool {
return &Tool{
rag: rag,
topic: topic,
}
}

type Input struct {
Query string `json:"rag_query" jsonschema:"description=search query"`
}

type Output struct {
Error string `json:"error,omitempty"`
Result string `json:"result,omitempty"`
}

type FnPrototype func(Input) Output

func (t *Tool) Name() string {
return "rag"
}

func (t *Tool) Description() string {
return "A tool that searches information ONLY for this topic: " + t.topic + ". DO NOT use this tool for other topics."
}

func (t *Tool) Fn() any {
return t.fn
}

//nolint:gosec
func (t *Tool) fn(i Input) Output {
results, err := t.rag.Retrieve(context.Background(), i.Query)
if err != nil {
return Output{Error: err.Error()}
}

// Return the output as a string.
return Output{Result: strings.Join(results, "\n")}
}
75 changes: 75 additions & 0 deletions tools/tool_router/tool_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package toolrouter

import (
"context"

"github.com/henomis/lingoose/thread"
)

type TTool interface {
Description() string
Name() string
Fn() any
}

type Tool struct {
llm LLM
tools []TTool
}

type LLM interface {
Generate(context.Context, *thread.Thread) error
}

func New(llm LLM, tools ...TTool) *Tool {
return &Tool{
tools: tools,
llm: llm,
}
}

type Input struct {
Query string `json:"query" jsonschema:"description=user query"`
}

type Output struct {
Error string `json:"error,omitempty"`
Result any `json:"result,omitempty"`
}

type FnPrototype func(Input) Output

func (t *Tool) Name() string {
return "query_router"
}

func (t *Tool) Description() string {
return "A tool that select the right tool to answer to an user query."
}

func (t *Tool) Fn() any {
return t.fn
}

//nolint:gosec
func (t *Tool) fn(i Input) Output {
query := "Here's a list of available tools:\n\n"
for _, tool := range t.tools {
query += "Name: " + tool.Name() + "\nDescription: " + tool.Description() + "\n\n"
}

query += "\nPlease select the right tool that can better answer the query '" + i.Query + "'. Give me only the name of the tool, nothing else."

th := thread.New().AddMessage(
thread.NewUserMessage().AddContent(
thread.NewTextContent(query),
),
)

err := t.llm.Generate(context.Background(), th)
if err != nil {
return Output{Error: err.Error()}
}

return Output{Result: th.LastMessage().Contents[0].AsString()}
}

0 comments on commit 66a58f5

Please sign in to comment.