Langchain - 仅依赖上下文

问题描述 投票:0回答:2

我的目标是让聊天机器人能够:

  1. 有记忆
  2. 输入一些文档
  3. 仅依赖给出的文档,当输入文档中没有信息时说“我不知道”。

我发现这个教程非常有用:https://github.com/jerryjliu/llama_index/blob/main/examples/chatbot/Chatbot_SEC.ipynb

它工作得很好,但我不明白如何应用第 3 点(仅依赖于输入文档)。

你觉得用浪链更容易吗?非常感谢,祝你有愉快的一天!卡洛

注意到了

create_llama_chat_agent

依靠 langchain/agents/conversational/prompt.py 文件来构建提示,我操纵了 PREFIX 文本来表达类似“请仅依赖于您的上下文”之类的内容,但它并不有效。

chatbot llama-index py-langchain
2个回答
1
投票

经过大量测试,比想象的要容易。 使用这个:https://blog.langchain.dev/tutorial-chatgpt-over-your-data/

我只是修改了 QA_PROMPT 说仅在与上下文相关时回答问题。

正在工作!

希望这对你们有用,祝你们有美好的一天!卡洛


0
投票

我碰巧写了一个代码(使用本地

sentence-transformers
VectorStore和
GPT4All
Mistral
LLM,全部本地安装)应该可以实现你想要的,供你参考。

from langchain.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(
    model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")


from langchain_community.vectorstores import FAISS
from langchain.memory import VectorStoreRetrieverMemory
vectorstore = FAISS.from_texts(
    ["Harry Potter's owl is in the castle"], embedding=embeddings)
retriever = vectorstore.as_retriever(search_kwargs=dict(k=1))
memory = VectorStoreRetrieverMemory(retriever=retriever)


from langchain_community.llms import GPT4All
llm = GPT4All(
    model="/home/jeff/.cache/huggingface/hub/gpt4all/mistral-7b-openorca.Q4_0.gguf",
    device='gpu',
    n_threads=8)

from langchain.prompts import PromptTemplate
_DEFAULT_TEMPLATE = """You're a helpful assistant, aiming at solving the problem.

Relevant pieces of previous conversation:
{history}

(You do not need to use these pieces of information if not relevant)

Answer my question: {input}
"""
PROMPT = PromptTemplate(
   input_variables=["history", "input"], template=_DEFAULT_TEMPLATE
)


from langchain.chains import ConversationChain
conversation_with_summary = ConversationChain(
   llm=llm,
   prompt=PROMPT,
   memory=memory,
   verbose=True
)
# result = conversation_with_summary.predict(input="Where is the hedwig? Why is it in castle?")

while True:
    user_input = input("Enter your question: ")
    if user_input == "exit":
        break
    else:
        result = conversation_with_summary.predict(input=user_input)
        print(result)
© www.soinside.com 2019 - 2024. All rights reserved.