Skip to content

Commit

Permalink
Added multi-prompt format
Browse files Browse the repository at this point in the history
Fix linter errors

FIx linter comment

Added backwards compatibility
  • Loading branch information
jubeless committed Nov 22, 2024
1 parent 238d1c7 commit 8724ddd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
31 changes: 29 additions & 2 deletions chains/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type LLMChain struct {
Memory schema.Memory
CallbacksHandler callbacks.Handler
OutputParser schema.OutputParser[any]
// When enabled usesMultiplePrompts will not 'flatten' the prompt into a single message.
UseMultiPrompt bool

OutputKey string
}
Expand All @@ -41,6 +43,7 @@ func NewLLMChain(llm llms.Model, prompt prompts.FormatPrompter, opts ...ChainCal
Memory: memory.NewSimple(),
OutputKey: _llmChainDefaultOutputKey,
CallbacksHandler: opt.CallbackHandler,
UseMultiPrompt: false,
}

return chain
Expand All @@ -56,12 +59,17 @@ func (c LLMChain) Call(ctx context.Context, values map[string]any, options ...Ch
return nil, err
}

result, err := llms.GenerateFromSinglePrompt(ctx, c.LLM, promptValue.String(), getLLMCallOptions(options...)...)
var output string
if c.UseMultiPrompt {
output, err = llms.GenerateFromMultiPrompt(ctx, c.LLM, chatMessagesToLLmMessageContent(promptValue.Messages()), getLLMCallOptions(options...)...)
} else {
output, err = llms.GenerateFromSinglePrompt(ctx, c.LLM, promptValue.String(), getLLMCallOptions(options...)...)
}
if err != nil {
return nil, err
}

finalOutput, err := c.OutputParser.ParseWithPrompt(result, promptValue)
finalOutput, err := c.OutputParser.ParseWithPrompt(output, promptValue)
if err != nil {
return nil, err
}
Expand All @@ -87,3 +95,22 @@ func (c LLMChain) GetInputKeys() []string {
func (c LLMChain) GetOutputKeys() []string {
return []string{c.OutputKey}
}

// Convert ChatMessage to MessageContent.
// Each ChatMessage is directly converted to a MessageContent with the same content and type.
func chatMessagesToLLmMessageContent(chatMessages []llms.ChatMessage) []llms.MessageContent {
msgs := make([]llms.MessageContent, len(chatMessages))
for idx, m := range chatMessages {
msgs[idx] = chatMessageToLLm(m)
}
return msgs
}

func chatMessageToLLm(in llms.ChatMessage) llms.MessageContent {
return llms.MessageContent{
Parts: []llms.ContentPart{
llms.TextContent{Text: in.GetContent()},
},
Role: in.GetType(),
}
}
17 changes: 17 additions & 0 deletions llms/llms.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ func GenerateFromSinglePrompt(ctx context.Context, llm Model, prompt string, opt
c1 := choices[0]
return c1.Content, nil
}

// GenerateFromMultiPrompt allows for calling an LLM with
// a multiple prompts.
func GenerateFromMultiPrompt(ctx context.Context, llm Model, msg []MessageContent, options ...CallOption) (string, error) {
resp, err := llm.GenerateContent(ctx, msg, options...)
if err != nil {
return "", err
}
choices := resp.Choices

if len(choices) < 1 {
return "", errors.New("empty response from model")
}

c1 := choices[0]
return c1.Content, nil
}

0 comments on commit 8724ddd

Please sign in to comment.