Skip to content

Commit

Permalink
support callback handler to llm chain (tmc#461)
Browse files Browse the repository at this point in the history
* support callback handler to llm chain

* fix golint

---------

Co-authored-by: lvzhong <[email protected]>
  • Loading branch information
2 people authored and t0mpl committed Dec 29, 2023
1 parent bf59430 commit 9887348
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion agents/conversational.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func NewConversationalAgent(llm llms.LanguageModel, tools []tools.Tool, opts ...
}

return &ConversationalAgent{
Chain: chains.NewLLMChain(llm, options.getConversationalPrompt(tools)),
Chain: chains.NewLLMChain(
llm,
options.getConversationalPrompt(tools),
chains.WithCallback(options.callbacksHandler),
),
Tools: tools,
OutputKey: options.outputKey,
CallbacksHandler: options.callbacksHandler,
Expand Down
6 changes: 5 additions & 1 deletion agents/mrkl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func NewOneShotAgent(llm llms.LanguageModel, tools []tools.Tool, opts ...Creatio
}

return &OneShotZeroAgent{
Chain: chains.NewLLMChain(llm, options.getMrklPrompt(tools)),
Chain: chains.NewLLMChain(
llm,
options.getMrklPrompt(tools),
chains.WithCallback(options.callbacksHandler),
),
Tools: tools,
OutputKey: options.outputKey,
CallbacksHandler: options.callbacksHandler,
Expand Down
18 changes: 11 additions & 7 deletions chains/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ var (
)

// NewLLMChain creates a new LLMChain with an LLM and a prompt.
func NewLLMChain(llm llms.LanguageModel, prompt prompts.FormatPrompter) *LLMChain {
func NewLLMChain(llm llms.LanguageModel, prompt prompts.FormatPrompter, opts ...ChainCallOption) *LLMChain {
opt := &chainCallOption{}
for _, o := range opts {
o(opt)
}
chain := &LLMChain{
Prompt: prompt,
LLM: llm,
OutputParser: outputparser.NewSimple(),
Memory: memory.NewSimple(),

OutputKey: _llmChainDefaultOutputKey,
Prompt: prompt,
LLM: llm,
OutputParser: outputparser.NewSimple(),
Memory: memory.NewSimple(),
OutputKey: _llmChainDefaultOutputKey,
CallbacksHandler: opt.CallbackHandler,
}

return chain
Expand Down

0 comments on commit 9887348

Please sign in to comment.