-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from FluffyKebab/callbacks
callbacks: add standard interface and logger
- Loading branch information
Showing
62 changed files
with
489 additions
and
122 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
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,28 @@ | ||
package callbacks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tmc/langchaingo/llms" | ||
"github.com/tmc/langchaingo/schema" | ||
) | ||
|
||
// Handler is the interface that allows for hooking into specific parts of an | ||
// LLM application. | ||
type Handler interface { | ||
HandleText(ctx context.Context, text string) | ||
HandleLLMStart(ctx context.Context, prompts []string) | ||
HandleLLMEnd(ctx context.Context, output llms.LLMResult) | ||
HandleChainStart(ctx context.Context, inputs map[string]any) | ||
HandleChainEnd(ctx context.Context, outputs map[string]any) | ||
HandleToolStart(ctx context.Context, input string) | ||
HandleToolEnd(ctx context.Context, output string) | ||
HandleAgentAction(ctx context.Context, action schema.AgentAction) | ||
HandleRetrieverStart(ctx context.Context, query string) | ||
HandleRetrieverEnd(ctx context.Context, documents []schema.Document) | ||
} | ||
|
||
// HandlerHaver is an interface used to get callbacks handler. | ||
type HandlerHaver interface { | ||
GetCallbackHandler() Handler | ||
} |
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,4 @@ | ||
// Package callbacks includes a standard interface for hooking into various | ||
// stages of your LLM application. The package contains an implementation of | ||
// this interface that prints to the standard output. | ||
package callbacks |
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,84 @@ | ||
//nolint:forbidigo | ||
package callbacks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/tmc/langchaingo/llms" | ||
"github.com/tmc/langchaingo/schema" | ||
) | ||
|
||
// LogHandler is a callback handler that prints to the standard output. | ||
type LogHandler struct{} | ||
|
||
var _ Handler = LogHandler{} | ||
|
||
func (l LogHandler) HandleText(_ context.Context, text string) { | ||
fmt.Println(text) | ||
} | ||
|
||
func (l LogHandler) HandleLLMStart(_ context.Context, prompts []string) { | ||
fmt.Println("Entering LLM with prompts:", prompts) | ||
} | ||
|
||
func (l LogHandler) HandleLLMEnd(_ context.Context, output llms.LLMResult) { | ||
fmt.Println("Exiting LLM with results:", formatLLMResult(output)) | ||
} | ||
|
||
func (l LogHandler) HandleChainStart(_ context.Context, inputs map[string]any) { | ||
fmt.Println("Entering chain with inputs:", formatChainValues(inputs)) | ||
} | ||
|
||
func (l LogHandler) HandleChainEnd(_ context.Context, outputs map[string]any) { | ||
fmt.Println("Exiting chain with outputs:", formatChainValues(outputs)) | ||
} | ||
|
||
func (l LogHandler) HandleToolStart(_ context.Context, input string) { | ||
fmt.Println("Entering tool with input:", removeNewLines(input)) | ||
} | ||
|
||
func (l LogHandler) HandleToolEnd(_ context.Context, output string) { | ||
fmt.Println("Exiting tool with output:", removeNewLines(output)) | ||
} | ||
|
||
func (l LogHandler) HandleAgentAction(_ context.Context, action schema.AgentAction) { | ||
fmt.Println("Agent selected action:", formatAgentAction(action)) | ||
} | ||
|
||
func (l LogHandler) HandleRetrieverStart(_ context.Context, query string) { | ||
fmt.Println("Entering retriever with query:", removeNewLines(query)) | ||
} | ||
|
||
func (l LogHandler) HandleRetrieverEnd(_ context.Context, documents []schema.Document) { | ||
fmt.Println("Exiting retirer with documents:", documents) | ||
} | ||
|
||
func formatChainValues(values map[string]any) string { | ||
output := "" | ||
for key, value := range values { | ||
output += fmt.Sprintf("\"%s\" : \"%s\", ", removeNewLines(key), removeNewLines(value)) | ||
} | ||
|
||
return output | ||
} | ||
|
||
func formatLLMResult(output llms.LLMResult) string { | ||
results := "[ " | ||
for i := 0; i < len(output.Generations); i++ { | ||
for j := 0; j < len(output.Generations[i]); j++ { | ||
results += output.Generations[i][j].Text | ||
} | ||
} | ||
|
||
return results + " ]" | ||
} | ||
|
||
func formatAgentAction(action schema.AgentAction) string { | ||
return fmt.Sprintf("\"%s\" with input \"%s\"", removeNewLines(action.Tool), removeNewLines(action.ToolInput)) | ||
} | ||
|
||
func removeNewLines(s any) string { | ||
return strings.ReplaceAll(fmt.Sprint(s), "\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
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
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
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
Oops, something went wrong.