欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用llama?Index幫你訓練pdf的示例詳解

 更新時間:2023年03月28日 11:00:58   作者:Ronny說  
這篇文章主要為大家介紹了使用llama?Index?幫你訓練pdf,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

llama Index是什么

《零開始帶你入門人工智能系列》第一篇:還用什么chatpdf,讓llama Index 幫你訓練pdf。

LlamaIndex 是您的外部數(shù)據(jù)和 LLM 之間的一個簡單、靈活的接口。它以易于使用的方式提供了以下工具:

為您現(xiàn)有的數(shù)據(jù)源和數(shù)據(jù)格式(API、PDF、文檔、SQL 等)提供數(shù)據(jù)連接器

為您的非結(jié)構(gòu)化和結(jié)構(gòu)化數(shù)據(jù)提供索引,以便與 LLM 一起使用。這些索引有助于抽象出情境學習的常見樣板和痛點:

  • 以易于訪問的格式存儲上下文以便快速插入。
  • 當上下文太大時處理提示限制(例如 Davinci 的 4096 個標記)。
  • 處理文本拆分。
  • 為用戶提供查詢索引(輸入提示)并獲得知識增強輸出的界面。
  • 為您提供全面的工具集,權(quán)衡成本和性能。

這里只是LlamaIndex應用的冰山一角,還可以挖掘更多好玩的功能

下面讓我一步步來教你如何實現(xiàn)

第一步:安裝依賴

requirements.txt
Flask==2.2.3
Flask-Cors==3.0.10
langchain==0.0.115
llama-index==0.4.30
PyPDF2==3.0.1

我們需要部署一個web服務,這里我使用了Flask,你也可以使用fastapi 或者django實現(xiàn)。其次我們使用llama-index作為索引進行pdf查詢。

第二步:訓練數(shù)據(jù)和構(gòu)建索引的server

index_server.py
import os
import pickle
# 這里可以換成你自己的key,但是最好不要上傳到github上
os.environ['OPENAI_API_KEY'] = ""
from multiprocessing import Lock
from multiprocessing.managers import BaseManager
from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, Document
index = None
stored_docs = {}
lock = Lock()
# 保存index的json文件
index_name = "./index.json"
# 保存文檔的pkl文件 用于保存文檔的id和文本,這樣客戶端就可以查詢到文檔的列表了
pkl_name = "stored_documents.pkl"
def initialize_index():
    """初始化index,如果已經(jīng)存在index,就使用已經(jīng)訓練好的index,否則就創(chuàng)建一個新的index"""
    global index, stored_docs
    with lock:
        if os.path.exists(index_name):
            """使用已經(jīng)訓練好的index"""
            index = GPTSimpleVectorIndex.load_from_disk(index_name)
        else:
            """使用GPTSimpleVectorIndex創(chuàng)建一個新的index 這里是llama_index的一個bug,如果你不傳入一個空的list,就會報錯 """
            index = GPTSimpleVectorIndex([])
            index.save_to_disk(index_name)
        if os.path.exists(pkl_name):
            with open(pkl_name, "rb") as f:
                stored_docs = pickle.load(f)
def query_index(query_text):
    """查詢index 根據(jù)你查詢的文本,返回一個response"""
    global index
    response = index.query(query_text)
    return response
def insert_into_index(doc_file_path, doc_id=None):
    """將文檔插入到index中,插入的文檔可以是一個文件,也可以是一個字符串,
    如果doc_id不為空,就使用doc_id,否則就使用文件名作為doc_id"""
    global index, stored_docs
    document = SimpleDirectoryReader(input_files=[doc_file_path]).load_data()[0]
    if doc_id is not None:
        document.doc_id = doc_id
    # Keep track of stored docs -- llama_index doesn't make this easy
    stored_docs[document.doc_id] = document.text[0:200]  # only take the first 200 chars
    with lock:
        index.insert(document)
        index.save_to_disk(index_name)
        with open(pkl_name, "wb") as f:
            pickle.dump(stored_docs, f)
    return
def get_documents_list():
    """查詢保存的文檔列表,返回一個list"""
    global stored_doc
    documents_list = []
    for doc_id, doc_text in stored_docs.items():
        documents_list.append({"id": doc_id, "text": doc_text})
    return documents_list
if __name__ == "__main__":
    # 初始化index, 如果已經(jīng)存在index,就使用已經(jīng)訓練好的index,否則就創(chuàng)建一個新的index
    print("initializing index...")
    initialize_index()
    # 啟動服務器,監(jiān)聽5602端口
    manager = BaseManager(('127.0.0.1', 5602), b'123456')
    # 注冊使用到的函數(shù),這樣客戶端就可以調(diào)用這些函數(shù)了
    manager.register('query_index', query_index)
    manager.register('insert_into_index', insert_into_index)
    manager.register('get_documents_list', get_documents_list)
    server = manager.get_server()
    print("server started...")
    server.serve_forever()

注意上面的OPENAI_API_KEY需要修改為你自己的,否則執(zhí)行initialize_index函數(shù)會提示報錯

最后,成功啟動

$ python index_server.py
initializing index...
server started...

總結(jié)時刻

教程使用了Flask、llama-index、PyPDF2等庫,通過搭建web服務,使用llama-index作為索引,最后提供一個交互界面進行pdf的內(nèi)容查詢。

如果您有相關(guān)的問題需要進一步解答,歡迎提問!有需要的趕緊轉(zhuǎn)發(fā)給你的好友吧

今天的內(nèi)容就到這里了,下期我們繼續(xù)完善這個項目,提供一個Flask服務,然后可以支持接口調(diào)用,還會做一個簡單的ui進行文檔處理,敬請期待。

以上就是使用llama Index 幫你訓練pdf的示例詳解的詳細內(nèi)容,更多關(guān)于llama Index 訓練pdf的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論