Samantha和Mistral 7B:强大而多功能的语言模型组合

Mistral 7B是一款突破性的语言模型,它既高效又强大。它的参数比其他大型语言模型(如Meta的Llama 2 13B)少,但在许多任务上都超越了它们。Mistral 7B还非常通用,在英语语言任务和编码任务上都表现出色。这使其成为各种企业应用程序的宝贵工具。
此外,Mistral 7B是开源的,这意味着任何人都可以无限制地使用和修改它。这使得它成为希望开发自己的定制AI应用程序的公司和组织的绝佳选择。
本文将涵盖以下内容:
- Samantha LLM
- Samantha LLM与Mistral 7B
- 代码实现
Samantha LLM
Samantha是一个大型语言模型(LLM),它在大量的文本和代码数据集上进行了训练。它可以生成文本,翻译语言,编写不同类型的创意内容,并以信息丰富的方式回答您的问题。Samantha仍在开发中,但它已经学会执行多种任务,包括:
认真遵循您的指示并完成您的请求。利用其知识以全面且信息丰富的方式回答您的问题,即使这些问题是开放式的、具有挑战性的或奇怪的。生成不同创意文本格式的文本内容,如诗歌、代码、脚本、音乐作品、电子邮件、信件等。
Samantha LLM 与 Mistral 7B
Mistral 7B是另一个大型语言模型(LLM),在海量文本和代码数据集上进行训练。它以其高效和强大而闻名,尽管参数较少,但其性能优于Meta的Llama 2 13B等大型模型。Mistral 7B也具有多功能性,在英语语言任务和编码任务方面均表现出色。
通过结合Samantha和Mistral 7B,您可以创建一个功能更强大、更通用的语言模型。这对于各种任务都非常有用,例如:
- 生成更具创造性和信息性的文本内容。
- 更准确地翻译语言。
- 编写更复杂的代码。
- 以更全面、更具信息量的方式回答您的问题。
代码实现
在文章的代码实现部分,我将提供如何将Samantha和Mistral 7B结合使用的代码示例。这将使读者更容易开始使用这些强大的语言模型。
导入库
!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 -q install sentencepiece Xformers einops
!pip -q install langchain
import torch
import transformers
from transformers import GenerationConfig, pipeline
from transformers import AutoTokenizer, AutoModelForCausalLM
from transformers import BitsAndBytesConfig
import bitsandbytes as bnb
import torch
import transformers
from transformers import GenerationConfig, pipeline
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("ehartford/samantha-mistral-7b")
model = AutoModelForCausalLM.from_pretrained("ehartford/samantha-mistral-7b",
load_in_8bit=True,
device_map='auto',
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
)
tokenizer.eos_token_id, tokenizer.pad_token_id
tokenizer.pad_token_id = 0
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_length=1536,
temperature=0.7,
top_p=0.95,
repetition_penalty=1.15
)
提示与响应
import json
import textwrap
system_prompt = "A chat between a curious user and an artificial intelligence assistant. \nThe assistant gives helpful, detailed, and polite answers to the user's questions."
addon_prompt = "Your name is Samantha."
# USER: What is 4x8?
# ASSISTANT:
def get_prompt(human_prompt):
# prompt_template=f"{human_prompt}"
prompt_template = f"{addon_prompt}\n{system_prompt}\n\nUSER: {human_prompt} \nASSISTANT: "
return prompt_template
print(get_prompt('What is the meaning of life?'))
def remove_human_text(text):
return text.split('USER:', 1)[0]
def parse_text_after_input(data, input_string):
for item in data:
text = item['generated_text']
input_string_index = text.find(input_string)
if input_string_index != -1:
output_text = text[input_string_index+len(input_string):].strip()
output_text = parse_text(output_text)
wrapped_text = textwrap.fill(output_text, width=100)
print(wrapped_text)
def parse_text(data):
for item in data:
text = item['generated_text']
assistant_text_index = text.find('ASSISTANT:')
if assistant_text_index != -1:
assistant_text = text[assistant_text_index+len('ASSISTANT:'):].strip()
assistant_text = remove_human_text(assistant_text)
wrapped_text = textwrap.fill(assistant_text, width=100)
print(wrapped_text)
# return assistant_text
data = [{'generated_text': '### Human: What is the capital of England? \n### Response: The capital city of England is London.'}]
parse_text(data)
A chat between a curious user and an artificial intelligence assistant.
The assistant gives helpful, detailed, and polite answers to the user's questions.
Your name is Samantha.
USER: What is the meaning of life?
ASSISTANT:
让我们尝试一些提示
%%time
prompt = 'What is your name?'
raw_output = pipe(get_prompt(prompt))
parse_text(raw_output)
My name is Samantha. I'm here to provide support, companionship, and engaging conversations for you.
CPU times: user 14.1 s, sys: 0 ns, total: 14.1 s
Wall time: 14.1 s
%%time
prompt = 'What can you help me with?'
raw_output = pipe(get_prompt(prompt))
parse_text(raw_output)
I am here to provide companionship, emotional support, and information on various subjects. Whether
it's sharing interesting facts, discussing books or movies, or engaging in creative activities like
writing poetry, I'm always ready for a conversation. My main goal is to make your life more
enjoyable and interesting while offering a listening ear when needed.
CPU times: user 45.6 s, sys: 0 ns, total: 45.6 s
Wall time: 45.5 s
%%time
prompt = 'What are the difference between Llamas, Alpacas and Vicunas?'
raw_output = pipe(get_prompt(prompt))
parse_text(raw_output)
Llamas (pronounced "yah-mas") and alpacas belong to the camelid family and are native to South
America. They have long necks, small ears, and soft, woolly coats that come in various colors like
black, white, brown, and fawn. Both llamas and alpacas can be domesticated and raised for their
fiber, which is used to make clothing and other textiles. Vicuñas (pronounced "vee-koo-nahs"), on
the other hand, are smaller than both llamas and alpacas and live only in the Andes Mountains of
Peru, Bolivia, Chile, and Ecuador. Their fleece is finer and softer than either llama or alpaca fur,
making it highly prized for its luxurious quality.
CPU times: user 35.9 s, sys: 9.58 ms, total: 35.9 s
Wall time: 35.8 s
结论
在这篇文章中,我们踏上了一段引人入胜的探索之旅,探索了Samantha-Mistral语言模型的可能性。我们学习了如何以自然直观的方式与Samantha互动,以及如何利用她的力量来检索和处理信息。
这趟旅程才刚刚开始。随着大型语言模型的不断发展,它们将为我们开启新的、令人兴奋的可能性。Samantha-Mistral只是这种潜力中的一个例子。它是一个强大的工具,可用于解决复杂问题,创建创新解决方案,并探索知识的新领域。
我希望这篇文章能激励您了解更多关于Samantha-Mistral和其他大型语言模型的信息。我们可以共同利用这些工具来塑造一个更美好的未来。
领英:您可以在领英上关注我,以获取我的最新项目和帖子。这是我的个人资料链接:https://www.linkedin.com/in/ankushsingal/
GitHub:您也可以在GitHub上支持我。我在那里上传了我所有的Notebooks和其他开源项目。如果您喜欢我的内容,请随时给我一个星标。这是我的GitHub链接:https://github.com/andysingal?tab=repositories
请求和问题:如果您有希望我从事的项目,或者对我的解释有任何疑问,请随时告诉我。我一直在寻找未来Notebooks的新想法,并且很乐意帮助解决您可能有的任何疑问。
请记住,每一个“赞”、“分享”和“星标”都极大地有助于我的工作,并激励我继续创作更多高质量的内容。感谢您的支持!