微信扫码
添加专属顾问
我要投稿
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-03-10
【一文看懂】大白话解释大模型的技术原理,为什么它那么聪明?
2025-03-10
【一文看懂】7B、175B,这些大模型参数是什么意思?它们是怎么算出来的?参数越多=模型越强?
2025-03-10
大模型领域常用名词解释(近100个)
2025-03-10
大模型应用联网搜索:重塑智能时代的交互与决策
2025-03-10
MCP:为 AI Agent 打造开放与互操作性的“超级接口”
2025-03-10
QwQ-32B,支持Function Call的推理模型,深度思考Agent的时代来了!
2025-03-10
国产自强!实在Agent+DeepSeek+华为昇腾一体机重磅发布!
2025-03-10
1次搭建完胜1亿次编码,MCP硅谷疯传!Anthropic协议解锁智能体「万能手」
2024-08-13
2024-06-13
2024-09-23
2024-08-21
2024-05-28
2024-07-31
2024-08-04
2024-04-26
2024-07-09
2024-09-17
2025-03-10
2025-03-10
2025-03-10
2025-03-10
2025-03-08
2025-03-08
2025-03-07
2025-03-07