导入
LangChain 简介
了解 LangChain 框架及其核心组件。
...
...
langchaingo 中的组件
模型接口
1type Model interface { 2 GenerateContent(ctx context.Context, messages []MessageContent, options ...CallOption) (*ContentResponse, error) 3}
嵌入器接口
1type Embedder interface { 2 EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error) 3 // EmbedQuery 4 EmbedQuery(ctx context.Context, text string) ([]float32, error) 5}
加载器接口
1type Loader interface { 2 Load(ctx context.Context) ([]schema.Document, error) 3 LoadAndSplit(ctx context.Context, splitter textsplitter.TextSplitter) ([]schema.Document, error) 4}
代码示例
1txtFile, _ := os.Open("/Users/home/myfile.txt") 2loader := documentloaders.NewText(txtFile) 3docs, _ := loader.Load(context.Background())
链式调用接口
1type Chain interface { 2 Call(ctx context.Context, inputs map[string]any, options ...ChainCallOption) (map[string]any, error) 3 GetMemory() schema.Memory 4 GetInputKeys() []string 5 GetOutputKeys() []string 6}
检索器接口
1type Retriever interface { 2 GetRelevantDocuments(ctx context.Context, query string) ([]Document, error) 3}
记忆接口
1type Memory interface { 2 GetMemoryKey(ctx context.Context) string 3 MemoryVariables(ctx context.Context) []string 4 LoadMemoryVariables(ctx context.Context, inputs map[string]any) (map[string]any, error) 5 SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error 6 Clear(ctx context.Context) error 7}