langchaingo icon indicating copy to clipboard operation
langchaingo copied to clipboard

Export getLLMCallOptions

Open yangxikun opened this issue 2 years ago • 4 comments

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 avatar Mar 08 '24 12:03 yangxikun

@yangxikun do you could share a example?

devalexandre avatar Mar 08 '24 16:03 devalexandre

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
}

yangxikun avatar Mar 09 '24 04:03 yangxikun

You could create a method for convert types, I don't know if it are a new feature for now.

devalexandre avatar Mar 11 '24 17:03 devalexandre

chains.ChainCallOption is type ChainCallOption func(*chainCallOption), How to convert?

yangxikun avatar Mar 13 '24 11:03 yangxikun