InfiniText:利用 Mistral-7B-Instruct-v0.1 赋能对话与内容

社区文章 发布于 2023 年 10 月 12 日

image/jpeg

在不断发展的人工智能领域,语言模型的突破不断重塑我们与人工智能互动和利用其力量的方式。Mistral-7B-Instruct 就是这样一项开创性的创新,它将效率和强大功能集于一身,封装在一个紧凑的包中。尽管参数数量少于 Meta 的 Llama 2 13B 等模型,Mistral-7B-Instruct 仍能在众多任务中轻松超越它们。其多功能性涵盖了从掌握英语语言任务到在编码世界中表现出色,使其成为各种企业应用程序的宝贵资产。

在本文中,我们将涵盖以下内容

  • Mistral 7B-Instruct 及其优势
  • Mistral-7B 对比 Llama:巨头之战
  • 代码实现

Mistral 7B-Instruct:功能强大的性能王者

Mistral-7B-Instruct 的独特之处在于,尽管参数数量较少,但其仍能提供卓越的性能。该模型表明,在语言模型方面,尺寸并非一切。它在各种任务中超越了其更大的竞争对手,对于寻求经济高效且高性能解决方案的用户来说,它改变了游戏规则。

值得注意的是,Mistral-7B-Instruct 旨在擅长两个主要领域:英语语言任务和编码任务。这种双重性使其有别于许多其他语言模型,因为它弥合了语言能力与编码相关应用所需的技术知识之间的鸿沟。它在这两个领域的卓越表现使其成为企业和开发人员的诱人选择。

image/png

开源优势

Mistral-7B-Instruct 最吸引人的地方之一可能是其开源性质。在重视协作和定制的时代,开源语言模型提供了无与伦比的灵活性。开发人员和组织可以充分利用 Mistral-7B-Instruct 的潜力,根据其独特需求进行修改,并构建自定义 AI 应用程序,而没有任何限制性障碍。

通过开源访问,Mistral-7B-Instruct 能够支持广泛的应用程序,从理解细微对话的客户服务聊天机器人到增强软件开发过程的代码生成工具。这种开放的可访问性对于寻求创新和掌控其 AI 解决方案的用户来说是颠覆性的。

image/png

Mistral-7B 对比 Llama:巨头之战

乍一看,鉴于其参数数量较少,Mistral-7B-Instruct 可能看起来像是大卫对歌利亚。然而,正是这种对比展示了该模型的强大功能。Mistral-7B-Instruct 正在与 Llama 展开竞争,原因如下:

  1. 效率优先于规模:Mistral-7B-Instruct 表明,更大并不总是更好。该紧凑型语言模型专注于效率和有效的参数利用,始终如一地提供卓越的性能,在各种基准测试中经常超越更大的 Llama 模型。

  2. 多任务通用性:Mistral-7B-Instruct 的优势不仅限于英语语言任务。它在编码相关任务中表现出色,使其成为连接语言能力和编码熟练度的通用解决方案。Llama 可能难以匹敌这种双重熟练度。

  3. 成本效益:像 Mistral-7B-Instruct 这样更小、更高效的模型为希望部署高性能语言模型的企业和组织提供了经济高效的替代方案。这在预算有限的情况下尤其具有吸引力。

image/png

代码实现:连接文本生成与编码

Mistral-7B-Instruct 不仅在于超越竞争对手;它还在于彻底改变我们处理文本生成和编码的方式。以下是它如何实现这种转变:

  1. 自动代码生成:凭借 Mistral-7B-Instruct 的能力,开发人员可以自动化代码生成任务。它理解并生成代码片段,为软件开发提供了巨大帮助。这减少了手动编码工作,并加速了开发周期。

  2. 调试辅助:Mistral-7B-Instruct 不仅生成代码;它还协助调试。通过理解代码逻辑,它可以识别错误并推荐解决方案,从而简化调试过程。

  3. 算法优化:软件优化对于提高性能至关重要。Mistral-7B-Instruct 可以建议算法优化,从而有助于开发更高效、更快的软件。

  4. 简化开发:结合文本生成和编码能力,Mistral-7B-Instruct 简化了软件开发过程。它可以生成文档、注释,甚至编写测试用例,从而减少开发人员的手动工作量。

image/png

导入库:

!pip install -q -U bitsandbytes
!pip install -q -U git+https://github.com/huggingface/transformers.git
!pip install -q -U git+https://github.com/huggingface/peft.git
!pip install -q -U git+https://github.com/huggingface/accelerate.git

!pip install -qqq pyautogen modelz-llm huggingface_hub 
!pip install -q datasets loralib sentencepiece
!pip -qqq install xformers einops
!pip -qqq install langchain
!git lfs install
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, AutoProcessor
from transformers import GenerationConfig, pipeline
from langchain import HuggingFacePipeline
from langchain import PromptTemplate, LLMChain

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)

model_id = "mistralai/Mistral-7B-Instruct-v0.1"
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)

提示格式

<s>[INST] <user_prompt> [/INST]

<assistant_response> </s>

[INST] <user_prompt>[/INST]
import random
import textwrap
device = 'cuda' if torch.cuda.is_available() else 'cpu'

def wrap_text(text, width=90): #preserve_newlines
  lines = text.split('\n')
  wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
  wrapped_text = '\n'.join(wrapped_lines)
  return wrapped_text

def multimodal_prompt(input_text:str, system_prompt="",max_length=512) -> str:
  """few-shot text-to-text prompting

  Generates text using a large language model, given a prompt and a device.

  Args:
    model: An AutoModelForCausalLM instance.
    tokenizer: An AutoTokenizer instance.
    prompt: The prompt to use for generation.
    device: The device to use for generation.

  Returns:
    A string containing the generated text.
  """
  prompt = f"""<s>[INST]{input_text}[/INST]"""
  encodeds = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
  model_inputs = encodeds.to(device)

  output = model.generate(**model_inputs,
                                max_length=max_length,
                                use_cache=True,
                                early_stopping=True,
                                bos_token_id=model.config.bos_token_id,
                                eos_token_id=model.config.eos_token_id,
                                pad_token_id=model.config.eos_token_id,
                                temperature=0.1,
                               do_sample=True)

# Randomly select one of the generated outputs
  response = random.choice(tokenizer.batch_decode(output))

  # Wrap the response text to a width of 90 characters
  wrapped_response = wrap_text(response)
  print(wrapped_response)
multimodal_prompt('Write a detailed analogy between mathematics and a lighthouse.',
         max_length=256)

输出

Write a detailed analogy between mathematics and a lighthouse.[/INST]
Mathematics and a lighthouse are interconnected in many ways, just as the different
components of a lighthouse work together to guide ships safely to shore.

Firstly, just as a lighthouse stands tall and strong, a pillar of guidance in the midst of
often stormy seas, mathematics provides a stable foundation for understanding the world
around us. It is a tool that can be used to navigate through the complexities of life,
just as the lighthouse uses its beacon to guide ships through treacherous waters.

The beacon of the lighthouse, much like the principles of mathematics, is a source of
light in the darkness. It provides a clear and unwavering guide for those who are lost or
unsure of their way. Similarly, the theorems, formulas, and equations of mathematics
provide a clear and precise way of understanding the world, illuminating the often
confusing and murky unknowns of life.

聊天

multimodal_prompt("""Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestion? \n\n Bob:""",max_length=128)

输出

Alice: I don't know why, I'm struggling to maintain focus while studying. Any suggestion?

 Bob: Well, have you tried breaking your study sessions into smaller chunks? It can help
you stay focused and retain more information.

 Alice: That's a good idea, I'll give it a try.

 Bob: Also, make sure you're taking breaks and doing something enjoyable during those
breaks. It can help you recharge and come back to studying with renewed energy.

 Alice: Yeah, I've been doing that, but I'm still having trouble staying

结论

Mistral-7B-Instruct 代表了语言模型和人工智能领域的重大飞跃。它突出了在保持卓越效率的同时实现卓越性能的可能性。该模型在各种基准测试中超越了大型竞争对手的能力,表明创新和能力并非完全由模型大小决定。引入了新颖的注意力机制,例如分组查询注意力和滑动窗口注意力,显著提高了模型的速度和内存效率,使其成为实时应用的理想选择。

此外,Mistral-7B-Instruct 的开源性质促进了协作和定制,使开发人员和组织能够充分利用其潜力。这种灵活性支持广泛的应用程序,从复杂的客户服务聊天机器人到高级代码生成工具。微调功能进一步强调了其适应性和高性能,使其成为各种实际应用的有前景的选择。

Mistral-7B-Instruct 不仅止步于性能和效率;它还通过其系统提示强调负责任的人工智能使用,这些提示允许用户强制执行内容约束,确保安全和道德的内容生成。它对内容进行分类和审核的能力使其成为在各种应用程序中保持质量和安全性的宝贵工具。

这种语言模型通过其创新设计、开放可访问性和负责任的使用能力,标志着高性能、经济高效且高效的语言模型开发迈出了重要一步。随着人工智能格局的不断发展,Mistral-7B-Instruct 为技术不仅更智能,而且更负责任和更易于访问的未来铺平了道路。

我希望本文能激发您进一步了解 Mistral-7B-Instruct 和其他大型语言模型的兴趣。我们可以共同利用这些工具,为所有人塑造一个更美好的未来。

"通过各种平台保持联系并支持我的工作

请求和问题:如果您有想让我做的项目,或者对我的概念有任何疑问,请随时告诉我。我一直在寻找未来 Notebook 的新想法,并且乐于帮助解决您可能有的任何疑问。

请记住,每一个“赞”、“分享”和“星标”都极大地有助于我的工作,并激励我继续创作更多高质量的内容。感谢您的支持!

资源

社区

注册登录 发表评论