赞
踩
可视化UI网址:https://smith.langchain.com/
目前需要邀请码
参考代码:
import os import openai sys.path.append('../..') openai.api_key ='your_openai_key' import os os.environ["LANGCHAIN_TRACING_V2"] = "true" os.environ["LANGCHAIN_ENDPOINT"] = "https://api.langchain.plus" os.environ["LANGCHAIN_API_KEY"] = "..." # 替换成自己的langchain_api_key from langchain.vectorstores import Chroma from langchain.embeddings.openai import OpenAIEmbeddings from langchain.chat_models import ChatOpenAI from langchain.chains import RetrievalQA from langchain.prompts import PromptTemplate persist_directory = 'docs/chroma/' #存放文件的地方 embedding = OpenAIEmbeddings() vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding) #创建向量数据库 llm_name = "gpt-3.5-turbo" #模型名 llm = ChatOpenAI(model_name=llm_name, temperature=0)#初始化chatgpt #!!!!默认的方式 # 构造prompt template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer. {context} Question: {question} Helpful Answer:""" QA_CHAIN_PROMPT = PromptTemplate.from_template(template) # 初始化chain qa_chain = RetrievalQA.from_chain_type( llm, retriever=vectordb.as_retriever(), return_source_documents=True, chain_type_kwargs={"prompt": QA_CHAIN_PROMPT} ) question = "Is probability a class topic?" result = qa_chain({"query": question}) result["result"] #!!!mapreduce方式 qa_chain_mr = RetrievalQA.from_chain_type( llm, retriever=vectordb.as_retriever(), chain_type="map_reduce" ) result_mr = qa_chain_mr({"query": question}) result_mr["result"] #!!!refine方式 qa_chain_rf = RetrievalQA.from_chain_type( llm, retriever=vectordb.as_retriever(), chain_type="refine" ) result_fr = qa_chain_fr({"query": question}) result_fr["result"]
默认方式是将检索到的(假设这里我们使用的是4个文档)所有文档丢入llm,根据所有文档回答相应问题
mapreduce方式是将检索到的文档(假设这里我们使用的是4个文档)先分别丢入llm(调用4次),由llm判定是否和问题相关,在将相关的丢入llm,回答问题
refine方式将建多到的文档按照顺序丢入。
如下图:
具体的langchain ui使用图如下:
每一次请求都会有相应的tag,具体点开可以到详细信息
参考网址:
[1]: https://smith.langchain.com/
[2]: https://deeplearning.ai
[3]: https://github.io/
[4]: http://langchain.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。