langchaingo
langchaingo copied to clipboard
Export getLLMCallOptions
chains.LLMChain.Call use llm.GenerateFromSinglePrompt, it just send a human message to LLM.
I want send multi messages to LLM with custom chain.
In custom chain, I call Model.GenerateContent directly, but I can not get llms.CallOption from chains.ChainCallOption.
@yangxikun do you could share a example?
type ConversationalWriting struct {
llmModel llms.Model
memory schema.Memory
}
func (i ConversationalWriting) Call(ctx context.Context,
inputs map[string]any, options ...chains.ChainCallOption) (map[string]any, error) {
chatHistory, ok := inputs[i.memory.GetMemoryKey(ctx)].([]schema.ChatMessage)
if !ok {
return nil, fmt.Errorf("%w: %w", chains.ErrMissingMemoryKeyValues, chains.ErrMemoryValuesWrongType)
}
messages := []llms.MessageContent{{
Role: schema.ChatMessageTypeSystem,
Parts: []llms.ContentPart{llms.TextPart(_conversationalWritingSystemPrompt)},
}}
for _, message := range chatHistory {
messages = append(messages, llms.MessageContent{
Role: message.GetType(),
Parts: []llms.ContentPart{llms.TextPart(message.GetContent())},
})
}
messages = append(messages, llms.MessageContent{
Role: schema.ChatMessageTypeHuman,
Parts: []llms.ContentPart{llms.TextPart(inputs["context"].(string))},
})
var callOptions []llms.CallOption
if ch := lxcontext.GetStreamMessageChannel(ctx); ch != nil {
completionID := lxcontext.GetChainCompletion(ctx).ID
callOptions = append(callOptions, llms.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
ch <- entity.StreamMessage{
DeltaContent: string(chunk),
CompletionID: completionID,
}
}
return nil
}))
}
response, err := i.llmModel.GenerateContent(ctx, messages, callOptions...)
if err != nil {
return nil, err
}
return map[string]any{_llmChainDefaultOutputKey: response.Choices[0].Content}, nil
}
You could create a method for convert types, I don't know if it are a new feature for now.
chains.ChainCallOption is type ChainCallOption func(*chainCallOption), How to convert?