微信扫码
添加专属顾问
我要投稿
TableGPT2-7B 是由浙江大学开发的大规模解码器,专为数据密集型任务设计,特别强调处理和分析表格数据。该模型旨在填补传统大语言模型(LLM)能力和实际需求之间的差距,特别是在商业智能(BI)、自动数据驱动分析以及涉及数据库或数据仓库的任务中表现出色。
首先,确保安装了 transformers 库:
pip install transformers>=4.37.0
from transformers import AutoModelForCausalLM, AutoTokenizer
import pandas as pd
from io import StringIO
# 示例表格数据
EXAMPLE_CSV_CONTENT = """
"Loss","Date","Score","Opponent","Record","Attendance"
"Hampton (14–12)","September 25","8–7","Padres","67–84","31,193"
"Speier (5–3)","September 26","3–1","Padres","67–85","30,711"
"Elarton (4–9)","September 22","3–1","@ Expos","65–83","9,707"
"Lundquist (0–1)","September 24","15–11","Padres","67–83","30,774"
"Hampton (13–11)","September 6","9–5","Dodgers","61–78","31,407"
"""
# 读取表格数据
csv_file = StringIO(EXAMPLE_CSV_CONTENT)
df = pd.read_csv(csv_file)
# 加载模型和分词器
model_name = "tablegpt/TableGPT2-7B"
model = AutoModelForCausalLM.from_pretrained(
model_name, torch_dtype="auto", device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 构建提示
example_prompt_template = """Given access to several pandas dataframes, write the Python code to answer the user's question.
/*
"{var_name}.head(5).to_string(index=False)" as follows:
{df_info}
*/
Question: {user_question}
"""
question = "哪些比赛的战绩达到了40胜40负?"
prompt = example_prompt_template.format(
var_name="df",
df_info=df.head(5).to_string(index=False),
user_question=question,
)
# 生成响应
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
text = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_new_tokens=512)
generated_ids = [
output_ids[len(input_ids):]
for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-11-03
当AI的知识与认知能力全面超越人类时
2025-11-03
Spring AI Agents 震撼发布:下一代 AI 开发范式已来!
2025-11-03
ollama v0.12.9 发布:修复 CPU Only 系统性能回退并优化 GPU 与 ROCm 支持
2025-11-02
老年人怎样用活法定义算法:1年100人1场实践
2025-11-02
Claude Skills,4000字详解 Anthropic 的思考
2025-11-02
什么是AI PaaS?一文读懂AI开发新未来
2025-11-02
浅谈 AI 浏览器
2025-11-01
谷歌又出神器:只要给个网址,自动帮你出官网同款设计!
2025-08-21
2025-08-21
2025-08-19
2025-09-16
2025-10-02
2025-09-08
2025-09-17
2025-08-19
2025-09-29
2025-08-20