// 将输入翻译为特定语言chain1 := chains.NewLLMChain(llm,prompts.NewPromptTemplate("请将输入的原始文本:{{.originText}}翻译为{{.language}},直接输出翻译文本",[]string{"originText", "language"}))chain1.OutputKey = "transText"// 总结翻译后的文本概要chain2 := chains.NewLLMChain(llm, prompts.NewPromptTemplate("请将输入的原始文本:<{{.transText}}>总结50字以内概要文本。严格使用JSON序列化输出结果,不要带有```json序列化标识。其中originText为原始文本,summaryText为概要文本",[]string{"transText"}))chain2.OutputKey = "summary_json"chain, err := chains.NewSequentialChain([]chains.Chain{chain1, chain2}, []string{"originText", "language"}, []string{"summary_json"})if err != nil {log.Fatal(err)}resp, err := chain.Call(ctx, map[string]any{"originText": "langchain is a good llm frameworks","language": "中文",})
// NewLLMChain creates a new LLMChain with an LLM and a prompt.func NewLLMChain(llm llms.Model, 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,CallbacksHandler: opt.CallbackHandler,}return chain}
type LLMChain struct {Prompt prompts.FormatPrompterLLM llms.ModelMemory schema.MemoryCallbacksHandler callbacks.HandlerOutputParser schema.OutputParser[any]OutputKey string}
func NewPromptTemplate(template string, inputVars []string) PromptTemplate {return PromptTemplate{Template: template,InputVariables: inputVars,TemplateFormat: TemplateFormatGoTemplate,}}
// PromptTemplate contains common fields for all prompt templates.type PromptTemplate struct {// Template is the prompt template.Template string// A list of variable names the prompt template expects.InputVariables []string// TemplateFormat is the format of the prompt template.TemplateFormat TemplateFormat// OutputParser is a function that parses the output of the prompt template.OutputParser schema.OutputParser[any]// PartialVariables represents a map of variable names to values or functions// that return values. If the value is a function, it will be called when the// prompt template is rendered.PartialVariables map[string]any}
func NewSequentialChain(chains []Chain, inputKeys []string, outputKeys []string, opts ...SequentialChainOption) (*SequentialChain, error) { //nolint:llls := &SequentialChain{chains: chains,inputKeys: inputKeys,outputKeys: outputKeys,memory: memory.NewSimple(),}for _, opt := range opts {opt(s)}if err := s.validateSeqChain(); err != nil {return nil, err}return s, nil}
type SequentialChain struct {chains []ChaininputKeys []stringoutputKeys []stringmemory schema.Memory}
func (c *SequentialChain) validateSeqChain() error {knownKeys := setutil.ToSet(c.inputKeys)// Make sure memory keys don't collide with input keysmemoryKeys := c.memory.MemoryVariables(context.Background())overlappingKeys := setutil.Intersection(memoryKeys, knownKeys)
for i, c := range c.chains {// Check that chain has input keys that are in knownKeysmissingKeys := setutil.Difference(c.GetInputKeys(), knownKeys)if len(missingKeys) > 0 {overlappingKeys := setutil.Intersection(c.GetOutputKeys(), knownKeys)if len(overlappingKeys) > 0 {
// Check that outputKeys are in knownKeysfor _, key := range c.outputKeys {if _, ok := knownKeys[key]; !ok {
func (c *SequentialChain) Call(ctx context.Context, inputs map[string]any, options ...ChainCallOption) (map[string]any, error) { //nolint:lllvar outputs map[string]anyvar err errorfor _, chain := range c.chains {outputs, err = Call(ctx, chain, inputs, options...)if err != nil {return nil, err}// Set the input for the next chain to the output of the current chaininputs = outputs}return outputs, nil}
func Call(ctx context.Context, c Chain, inputValues map[string]any, options ...ChainCallOption) (map[string]any, error) { // nolint: lllfullValues := make(map[string]any, 0)for key, value := range inputValues {fullValues[key] = value}newValues, err := c.GetMemory().LoadMemoryVariables(ctx, inputValues)if err != nil {return nil, err}for key, value := range newValues {fullValues[key] = value}callbacksHandler := getChainCallbackHandler(c)if callbacksHandler != nil {callbacksHandler.HandleChainStart(ctx, inputValues)}outputValues, err := callChain(ctx, c, fullValues, options...)if err != nil {if callbacksHandler != nil {callbacksHandler.HandleChainError(ctx, err)}return outputValues, err}if callbacksHandler != nil {callbacksHandler.HandleChainEnd(ctx, outputValues)}if err = c.GetMemory().SaveContext(ctx, inputValues, outputValues); err != nil {return outputValues, err}return outputValues, nil}


文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




