微信扫码
添加专属顾问
我要投稿
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+中大型企业
2026-02-24
当软件不值钱了以后,最稀缺的资源是什么?
2026-02-24
OpenAI Codex负责人:我们熟悉的编程方式正在终结 | Jinqiu Select
2026-02-24
OpenClaw 最佳实践:5条“基本原则”
2026-02-24
春节归来第一天,Second Me 做了一个「Agent 互联网的 App Store」
2026-02-24
万字深度解读 MCP Apps:重构 Web 应用,开启 AI 助手的“小程序”时代。
2026-02-24
AI Agent系列|什么是 ReAct Agent?
2026-02-24
刚刚,Anthropic深夜大点名,这三家中国公司进行蒸馏攻击?!
2026-02-23
Claude Code如何突破白领工业革命
2026-01-24
2026-01-10
2026-01-01
2026-01-26
2025-12-09
2026-02-03
2025-12-21
2026-01-09
2026-01-09
2026-01-27
2026-02-24
2026-02-24
2026-02-20
2026-02-14
2026-02-13
2026-02-12
2026-02-12
2026-02-11