保姆级教程:TableGPT2-7B大模型对话EXCEL,CSV
在当今这个信息爆炸的时代,数据已成为驱动各行各业发展的核心引擎。无论是企业决策、市场分析,还是学术研究、个人生活,数据都无处不在地渗透其中,发挥着举足轻重的作用。然而,面对海量且复杂的数据,如何高效地收集、整理、分析并提取有价值的信息,成为了摆在我们面前的一大挑战。
“大模型”引入到了Excel的数据处理流程中,通过构建智能对话系统,使用户能够以自然语言的方式与Excel进行交互。这种全新的交互方式不仅极大地降低了code门槛,使得非专业人士也能轻松上手,更重要的是,它为用户提供了一个更加直观、高效、便捷的数据处理。
接下来详细阐释实现大模型对话的 EXCL 流程,用TableGPT2-7B,生成可执行的代码准确率较高,,推荐使用,该方法也可以使用其他大模型。
接入本地大模型服务
from openai import OpenAI
    client = OpenAI(
    api_key= “none”,
    base_url= “http://172.30.4.169:8001/v1”
    )
    def send_chatgpt_message(prompt):
    “””
        请求chatGPT函数
        “””
    messages = [
    {“role”: “system”, “content”: “You are a helpful assistant.”},
    {“role”: “user”, “content”: f”{prompt}“}
    ]
    response = client.chat.completions.create(
    model=“TableGPT2-7B”,
    messages=messages,
    )
    answer = response.choices[0].message.content
    return answer
读入EXCE数据
我们以泰坦尼克数据为例
import pandas as pd
    df=pd.read_excel(Titanic.xlsx)
    df
数据
大模型prompt模版
example_prompt_template = “””Given access to several pandas dataframes, write the Python code to answer the users question.
        /*
        “{var_name}.head(5).to_string(index=False)” as follows:
        {df_info}
        “{var_name}.columns” as follows:
        {df_columns}
        */
        Question: {user_question}
        “””
    question = “登船港口,查看其数据组成”
    prompt = example_prompt_template.format(
    var_name=“df”,
    df_info=df.head(5).to_string(index=False),
    df_columns = df.columns,
    user_question=question,
    )
问题1查询
问题1:登船港口,查看其数据组成
answer = send_chatgpt_message(prompt)
    answer
执行结果
code = answer.split(“`python)[1].split(“`)[0]
    print(code)
    exec(code)

问题2:登船港口,亦即对于缺失数据,使用S进行替代

问题1画图
问题1:画活着男女分布图

