微信扫码
与创始人交个朋友
我要投稿
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+中大型企业
2024-12-24
2025,AI世界的“大厦已成”,红杉资本的三大AI预测
2024-12-24
国内AI“六小龙”概览
2024-12-24
「深度」O1意义被大大低估!OpenAI核心科学家重磅发声:测试时计算将引爆AGI时代!
2024-12-24
AI大牛解析o3技术路线!大模型下一步技术路线已现端倪?
2024-12-23
AI Agent智能体产品的5个级别
2024-12-23
Anthropic官方揭秘:构建AI智能体的"最强攻略"!
2024-12-23
2025年AI大模型的趋势与洞察
2024-12-23
阶跃星辰完成数亿美元 B 轮融资,发力「超级模型」+「超级应用」
2024-05-28
2024-04-26
2024-08-13
2024-08-21
2024-07-09
2024-06-13
2024-08-04
2024-04-11
2024-07-18
2024-07-01