前言
大家好,我是章北海mlpy
最近在浅学LangChain,在大模型时代,感觉这玩意很有前途。
LangChain是一个开源的应用开发框架,目前支持Python和TypeScript两种编程语言。
它赋予LLM两大核心能力:数据感知,将语言模型与其他数据源相连接;代理能力,允许语言模型与其环境互动。
LangChain的主要应用场景包括个人助手、基于文档的问答、聊天机器人、查询表格数据、代码分析等。
正文
之前大火的ChatPDF应该就是用LangChain实现的昨晚又看到一个有趣的实例:Chat with CSV&Excel using LangChain and OpenAI,蛮粗糙的,感觉可以用Gradio加个前端,或许有点意思。
https://github.com/amrrs/csvchat-langchain
源代码有TypeError的bug,我改了一下,可以跑通,有感兴趣的可以直接复制。
# -*- coding: utf-8 -*- from langchain.document_loaders import CSVLoader from langchain.indexes import VectorstoreIndexCreator from langchain.chains import RetrievalQA from langchain.llms import OpenAI import os os.environ["OPENAI_API_KEY"] = "sk-你的API" !wget https://gist.githubusercontent.com/armgilles/194bcff35001e7eb53a2a8b441e8b2c6/raw/92200bc0a673d5ce2110aaad4544ed6c4010f687/pokemon.csv # Load the documents import csv loader = CSVLoader(file_path='/Users/huhaiyang/projs/myrepo/pokemon.csv', csv_args={ 'delimiter': ','}) # Create an index using the loaded documents index_creator = VectorstoreIndexCreator() docsearch = index_creator.from_loaders([loader]) # Create a question-answering chain using the index chain = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=docsearch.vectorstore.as_retriever(), input_key="question") # Pass a query to the chain query = "Do you have a column called age?" response = chain({"question": query}) print(response['result'])
我简单测试了,胡说八道含量很高。
这个实例太粗糙了,后期再深入研究吧,看是否可以达到简单统计,甚至实现透视表的程度。
