微信扫码
添加专属顾问
我要投稿
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-12-18
突发!ChatGPT版应用商店正式发布,第三方开发者新入口!
2025-12-18
实测豆包1.8后,我终于明白字节为什么要推豆包手机了。
2025-12-18
通用Agent模型Seed1.8正式发布
2025-12-18
谷歌发布Gemini 3 Flash,口述即原型,速度堪比搜索引擎
2025-12-18
2026 开年AI对谈:the year of R | 对谈真格基金戴雨森
2025-12-18
再论Skill:Agent 落地第一性原理
2025-12-18
Gemini 3 Flash闪电来袭:智力竟反超Pro!速度快3倍,全球免费
2025-12-18
Gemini 3 Flash 可能是 Google 最狠的一步棋
2025-10-26
2025-10-02
2025-09-29
2025-10-07
2025-09-30
2025-11-19
2025-10-20
2025-11-13
2025-10-02
2025-10-11
2025-12-16
2025-12-15
2025-12-14
2025-12-12
2025-12-12
2025-12-11
2025-12-09
2025-12-08