license: apache-2.0
tags:
- 文本生成推理
datasets:
- Josephgflowers/Finance-Instruct-500k
language:
- en
base_model:
- unsloth/Meta-Llama-3.1-8B
pipeline_tag: text-generation
library_name: transformers
金融-Llama-8B模型卡
该模型是基于unsloth/Meta-Llama-3.1-8B
在Josephgflowers/Finance-Instruct-500k
数据集上微调的版本,专为金融任务、推理和多轮对话设计。
核心特性
- 广泛覆盖: 训练数据包含超过50万条涵盖金融问答、推理、情感分析、主题分类、多语言命名实体识别和对话式AI的条目。
- 多轮对话能力: 擅长进行强调上下文理解和推理的深度对话。
- 多样化数据源: 整合了来自Cinder、Sujet-Finance-Instruct-177k、Phinance数据集、BAAI/IndustryInstruction_Finance-Economics、Josephgflowers/Financial-NER-NLP等多个高质量数据集的内容。
- 金融专业化: 专为金融推理、问答、实体识别、情感分析等场景优化。
数据集详情
Finance-Instruct-500k数据集
概述
Finance-Instruct-500k是一个精心策划的综合数据集,旨在为高级语言模型提供金融任务、推理和多轮对话的训练素材。该语料库整合了多个高质量金融数据集,提供超过50万条条目,为金融领域的指令调优和微调提供了无与伦比的深度和多样性。
数据集内容涵盖金融推理、问答、实体识别、情感分析、地址解析和多语言自然语言处理(NLP)。经过去重处理的多样化条目使其适用于各类金融AI应用场景,包括领域专用助手、对话代理和信息提取系统。
数据集核心特点
- 广泛覆盖: 超过50万条涵盖金融问答、推理、情感分析、主题分类、多语言NER和对话式AI的条目
- 多轮对话: 强调上下文理解和推理的丰富对话样本
- 多样化数据源: 包含来自Cinder、Sujet-Finance-Instruct-177k、Phinance数据集等高质量金融数据集的内容
使用方式
该模型可通过transformers
库的文本生成管道使用。
首先确保已安装transformers
和torch
库:
pip install transformers torch
使用方式
Transformers管道
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
import torch
model_id = "tarun7r/Finance-Llama-8B"
print("正在加载优化内存的模型...")
try:
print("尝试FP16加载...")
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
device_map="auto",
low_cpu_mem_usage=True,
trust_remote_code=True
)
print("‚úì FP16加载成功")
except Exception as e:
print(f"FP16加载失败: {e}")
try:
print("尝试CPU卸载...")
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
device_map="balanced",
low_cpu_mem_usage=True,
trust_remote_code=True
)
print("‚úì CPU卸载加载成功")
except Exception as e:
print(f"CPU卸载失败: {e}")
print("正在CPU上加载...")
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
device_map="cpu",
low_cpu_mem_usage=True,
trust_remote_code=True
)
print("‚úì CPU加载成功")
tokenizer = AutoTokenizer.from_pretrained(model_id)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer
)
print("‚úì 管道创建成功!")
finance_prompt_template = """以下是描述任务的指令,附带提供更多上下文的输入。请编写适当完成请求的响应。
### 指令:
{}
### 输入:
{}
### 响应:
"""
messages = [
{"role": "system", "content": "您是一个知识渊博的金融聊天机器人。您的目标是根据用户特定需求和上下文,提供准确、有见地且可操作的金融建议。"},
{"role": "user", "content": "在波动市场中,个人投资者可以采用哪些策略来有效分散投资组合?"},
]
prompt = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages])
print("\n--- 正在生成响应 ---")
try:
outputs = generator(
prompt,
do_sample=True,
temperature=0.7,
top_p=0.9,
pad_token_id=tokenizer.eos_token_id,
num_beams=1,
early_stopping=True,
use_cache=True
)
generated_text = outputs[0]['generated_text']
response_start = generated_text.rfind("### 响应:")
if response_start != -1:
response = generated_text[response_start + len("### 响应:"):].strip()
print("\n--- 响应 ---")
print(response)
else:
print(generated_text)
if torch.cuda.is_available():
torch.cuda.empty_cache()
except Exception as e:
print(f"生成错误: {e}")
引用
@misc{tarun7r/Finance-Llama-8B,
author = {tarun7r},
title = {tarun7r/Finance-Llama-8B: 基于Josephgflowers/Finance-Instruct-500k微调的Llama 3.1 8B模型},
year = {2025},
publisher = {Hugging Face},
journal = {Hugging Face模型中心},
howpublished = {\url{https://huggingface.co/tarun7r/Finance-Llama-8B}}
}