-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | ||
} |