rag

package
v0.0.0-...-1634ac5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Copyright © 2021 eryajf

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var EmbeddingCmd = &cobra.Command{
	Use:   "embedding",
	Short: "将文档块儿转换为向量",
	Run: func(cmd *cobra.Command, args []string) {
		filepath, _ := cmd.Flags().GetString("filepath")
		chunkSize, _ := cmd.Flags().GetInt("chunksize")
		chunkOverlap, _ := cmd.Flags().GetInt("chunkoverlap")
		docs, err := TextToChunks(filepath, chunkSize, chunkOverlap)
		if err != nil {
			logger.Error("转换文件为块儿失败,错误信息: %v", err)
		}
		err = storeDocs(docs, getStore())
		if err != nil {
			logger.Error("转换块儿为向量失败,错误信息: %v", err)
		} else {
			logger.Info("转换块儿为向量成功")
		}
	},
}
View Source
var FileToChunksCmd = &cobra.Command{
	Use:   "filetochunks",
	Short: "将文件转换为块儿",
	Run: func(cmd *cobra.Command, args []string) {
		filepath, _ := cmd.Flags().GetString("filepath")
		chunkSize, _ := cmd.Flags().GetInt("chunksize")
		chunkOverlap, _ := cmd.Flags().GetInt("chunkoverlap")

		docs, err := TextToChunks(filepath, chunkSize, chunkOverlap)
		if err != nil {
			logger.Error("转换文件为块儿失败,错误信息: %v", err)
		}
		logger.Info("转换文件为块儿成功,块儿数量: ", len(docs))
		for _, v := range docs {
			fmt.Printf("🗂 块儿内容==> %v\n", v.PageContent)
		}
	},
}
View Source
var GetAnwserCmd = &cobra.Command{
	Use:   "getanswer",
	Short: "获取回答",
	Run: func(cmd *cobra.Command, args []string) {
		topk, _ := cmd.Flags().GetInt("topk")

		prompt, err := GetUserInput("请输入你的问题")
		if err != nil {
			logger.Error("获取用户输入失败,错误信息: %v", err)
		}
		rst, err := useRetriaver(getStore(), prompt, topk)
		if err != nil {
			logger.Error("检索文档失败,错误信息: %v", err)
		}
		answer, err := GetAnswer(context.Background(), getOllamaMistral(), rst, prompt)
		if err != nil {
			logger.Error("获取回答失败,错误信息: %v", err)
		} else {
			fmt.Printf("🗂 原始回答==> %s\n\n", answer)
			rst, err := Translate(getOllamaLlama2(), answer)
			if err != nil {
				logger.Error("翻译回答失败,错误信息: %v", err)
			} else {
				fmt.Printf("🗂 翻译后的回答==> %s\n", rst)
			}
		}
	},
}
View Source
var RetrieverCmd = &cobra.Command{
	Use:   "retriever",
	Short: "将用户问题转换为向量并检索文档",
	Run: func(cmd *cobra.Command, args []string) {
		topk, _ := cmd.Flags().GetInt("topk")

		prompt, err := GetUserInput("请输入你的问题")
		if err != nil {
			logger.Error("获取用户输入失败,错误信息: %v", err)
		}
		rst, err := useRetriaver(getStore(), prompt, topk)
		if err != nil {
			logger.Error("检索文档失败,错误信息: %v", err)
		}
		for _, v := range rst {
			fmt.Printf("🗂 根据输入的内容检索出的块儿内容==> %v\n", v.PageContent)
		}
	},
}

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

func GetAnswer

func GetAnswer(ctx context.Context, llm llms.Model, docRetrieved []schema.Document, prompt string) (string, error)

GetAnswer 获取答案

func GetUserInput

func GetUserInput(promptString string) (string, error)

GetUserInput 获取用户输入

func TextToChunks

func TextToChunks(dirFile string, chunkSize, chunkOverlap int) ([]schema.Document, error)

TextToChunks 函数将文本文件转换为文档块

func Translate

func Translate(llm llms.Model, text string) (string, error)

Translate 将文本翻译为中文

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL