我目前正在开发一个由大型语言模型 (LLM) 提供支持的聊天机器人,我希望它能够根据我自己的文档提供响应。我知道在我的文档上使用微调模型可能不会产生直接响应,因此我正在探索检索增强生成 (RAG) 的概念以增强其性能。
在我的研究中,我遇到了两种工具,Langchain 和 LlamaIndex,它们似乎有助于 RAG。然而,我很难理解它们之间的主要区别。我注意到一些教程和资源同时使用这两种工具,我很好奇为什么人们会选择使用一种工具而不是另一种工具,或者什么时候一起使用它们才有意义。
有人可以深入了解 Langchain 和 LlamaIndex for RAG 之间的主要区别,以及何时使用一种工具优于另一种工具或在聊天机器人开发中将它们结合起来?
chatbot openai-api llama-index langchain large-language-model
大语言模型的指令调优和普通微调有什么区别?
另外,我所指的指令调整不是上下文/提示的指令调整。
最近所有关于微调的论文似乎都是关于指令调优的。
我看过几篇关于微调/指令调优(例如 FLAN)的论文,但没有一篇真正描述指令调优和替代方案(无论替代方案是什么)之间的区别。
我理解指令调整是微调的一种形式,但带有指令数据集。但所有数据集都不是指令数据集吗?还有哪些种类呢?
我在他们的官方网站上搜索了所有 langchain 文档,但没有找到如何从 python 中的 str 变量创建 langchain 文档,所以我在他们的 GitHub 代码中搜索,发现了这个:
doc=Document(
page_content="text",
metadata={"source": "local"}
)
Run Code Online (Sandbox Code Playgroud)
PS:我添加了元数据属性
,然后尝试将该文档与我的链一起使用:
内存和链:
memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
chain = load_qa_chain(
llm, chain_type="stuff", memory=memory, prompt=prompt
)
Run Code Online (Sandbox Code Playgroud)
调用方法:
chain({"input_documents": doc, "human_input": query})
Run Code Online (Sandbox Code Playgroud)
提示模板:
template = """You are a senior financial analyst analyzing the below document and having a conversation with a human.
{context}
{chat_history}
Human: {human_input}
senior financial analyst:"""
prompt = PromptTemplate(
input_variables=["chat_history", "human_input", "context"], template=template
)
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
AttributeError: 'tuple' object has no attribute 'page_content'
Run Code Online (Sandbox Code Playgroud)
当我在将 …
我正在创建这样的对话:
llm = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name=OPENAI_DEFAULT_MODEL)
conversation = ConversationChain(llm=llm, memory=ConversationBufferMemory())
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是能够保存和加载它,ConversationBufferMemory()以便它在会话之间保持不变。似乎没有任何明显的教程,但我注意到“Pydantic”,所以我尝试这样做:
saved_dict = conversation.memory.chat_memory.dict()
cm = ChatMessageHistory(**saved_dict) # or cm = ChatMessageHistory.parse_obj(saved_dict)
Run Code Online (Sandbox Code Playgroud)
但这失败了:
ValidationError: 6 validation errors for ChatMessageHistory
messages -> 0
Can't instantiate abstract class BaseMessage with abstract method type (type=type_error)
Run Code Online (Sandbox Code Playgroud)
想法?我希望有任何类型的指南、存储库、参考资料等的链接。
我正在使用 llama-index 基于文档创建一个非常简单的问答应用程序。此前,我曾将其与 OpenAI 一起使用。现在我想尝试不使用外部 API,因此我尝试此链接中的Hugging Face 示例。
它在链接的示例中说:“请注意,为了获得完全私人的体验,还需要设置本地嵌入模型(此处的示例)。” 我假设下面给出的示例是所引用的示例。因此,很自然地,我尝试复制该示例(此处为更完整的示例)。
这是我的代码:
from pathlib import Path
import gradio as gr
import sys
import logging
import os
from llama_index.llms import HuggingFaceLLM
from llama_index.prompts.prompts import SimpleInputPrompt
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
from llama_index import SimpleDirectoryReader, VectorStoreIndex, ServiceContext, load_index_from_storage, StorageContext
storage_path = "storage/"
docs_path="docs"
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
#max_chunk_overlap = 20
chunk_overlap_ratio = 0.1
chunk_size_limit = 600
#prompt_helper = PromptHelper(max_input_size, num_outputs, chunk_overlap_ratio, chunk_size_limit=chunk_size_limit)
system_prompt = """<|SYSTEM|># StableLM Tuned …Run Code Online (Sandbox Code Playgroud) python huggingface-transformers huggingface llama-index large-language-model
我有三个问题:
给定 LLM 参数的数量(以十亿为单位),您如何计算出运行模型需要多少 GPU RAM?
如果您有足够的 CPU-RAM(即没有 GPU),您可以运行该模型,即使它很慢
你可以在混合 GPU-RAM 和 CPU-RAM 中运行 LLM 模型(如 h2ogpt、open-assistant)吗?
artificial-intelligence deep-learning gpt-3 large-language-model
有没有办法从huggingface 的meta-llama/Llama-2-13b-chat-hf 获取句子嵌入?
\n模特链接:https://huggingface.co/meta-llama/Llama-2-13b-chat-hf
\n我尝试使用拥抱面孔中的 transfomer.Automodel 模块来获取嵌入,但结果看起来并不符合预期。实施方式参见以下链接。参考:https ://github.com/Muennighoff/sgpt#ametry-semantic-search-be\xc2\xa0
\nartificial-intelligence huggingface-transformers huggingface large-language-model llama
基本上我想用 Flask 和 LangChain 来实现这一点:https://www.youtube.com/watch?v =x8uwwLNxqis 。
我正在构建一个在后端使用 LangChain 的问答 Flask 应用程序,但我在传输来自 ChatGPT 的响应时遇到问题。我的链条看起来像这样:
chain = VectorDBQA.from_chain_type(llm=ChatOpenAI(model_name="gpt-3.5-turbo", streaming=True, chain_type="stuff", vectorstore=docsearch)
...
result = chain({"query": query})
output = result['result']
Run Code Online (Sandbox Code Playgroud)
Jinja 只是打印{{ output }},并且工作正常,但结果不会出现在网站中,直到整个响应完成。我想流式传输由 ChatGPT 生成的结果。
我尝试过使用stream_template,但它不起作用(它不传输结果,它只是立即打印完整的响应,尽管我可能做错了什么)。
我终于解决了:
我尝试functions在调用 Azure OpenAI GPT 时使用,如https://platform.openai.com/docs/api-reference/chat/create#chat/create-functions中所述
我用:
import openai
openai.api_type = "azure"
openai.api_base = "https://XXXXXXXX.openai.azure.com/"
openai.api_version = "2023-06-01-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.ChatCompletion.create(
engine="gpt-35-turbo-XXX",
model="gpt-35-turbo-0613-XXXX"
messages=messages,
functions=functions,
function_call="auto",
)
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
openai.error.InvalidRequestError:
Unrecognized request argument supplied: functions
Run Code Online (Sandbox Code Playgroud)
为什么?
运行上面示例代码的数据(messages并且functions需要定义):
messages = [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"}]
functions = [
{
"name": "fetch_pages",
"description": "Fetch the content of specified pages from the document.",
"parameters": {
"type": "object",
"properties": …Run Code Online (Sandbox Code Playgroud) 我正在尝试在带有服务器的计算机上运行 Llama 2.0,它警告我,我的速度会变慢,因为我犯了一些我不知道的错误,但是它可以工作,但我不知道如何优化它
以下是功能代码
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
class LlamaChatBot:
def __init__(self, model_name ="daryl149/llama-2-7b-chat-hf"):
torch.cuda.empty_cache()
self.isGPU = torch.cuda.is_available()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if self.isGPU:
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map='auto', load_in_4bit=True
)
else:
self.tokenizer = AutoTokenizer.from_pretrained("daryl149/llama-2-7b-chat-hf")
self.model = AutoModelForCausalLM.from_pretrained(model_name).to(self.device)
def generate_response(self, prompt):
if self.isGPU():
input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.to('cuda')
else: input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids
generated_ids = self.model.generate(input_ids, max_length=1024)
generated_text = self.tokenizer.decode(generated_ids[0], skip_special_tokens=True)
print(generated_text)
return generated_text
Run Code Online (Sandbox Code Playgroud)
警告 :
warnings.warn(f'Input type into Linear4bit is torch.float16, …Run Code Online (Sandbox Code Playgroud) python ×5
langchain ×4
gpt-3 ×2
huggingface ×2
llama ×2
llama-index ×2
openai-api ×2
azure ×1
azure-openai ×1
chatbot ×1
fine-tuning ×1
flask ×1
nlp ×1
pydantic ×1
pytorch ×1