Xây dựng giao diện trò chuyện của riêng bạn với dữ liệu của bạn MÀ KHÔNG CÓ API OpenAI
Những gì bạn có thể làm với các mô hình của OpenAI thật hấp dẫn. Ngoài ra, các công cụ như LangChain và llama-index giúp bạn dễ dàng thiết lập và chạy một hệ thống cơ bản giống như ChatGPT trong một vài dòng mã. Tuy nhiên, hầu hết các ví dụ được xây dựng trên API của OpenAI, điều này không thực tế trong mọi trường hợp, ví dụ: vì bạn không thể gửi dữ liệu của mình lên đám mây hoặc bạn không thể tiêu tiền .

Vì gần đây tôi đã cố gắng chạy một hệ thống cơ bản bằng cách sử dụng dolly của databrick và nó cần một chút thử và sai ở đây, hướng dẫn nhanh của tôi về cách sử dụng LLM mở, tùy chỉnh để xây dựng giao diện trò chuyện cho dữ liệu của riêng bạn!
Bước 1: Thu thập dữ liệu của bạn và cài đặt các phụ thuộc
Đối với bước này, chỉ cần thu thập tất cả dữ liệu bạn muốn sử dụng và đặt nó vào một thư mục trên máy cục bộ của bạn. Trong trường hợp của tôi, đây là một loạt các tệp đánh dấu mà tôi đã lấy từ tài liệu của công cụ quản lý dữ liệu Spotlight của chúng tôi (hãy xem thử ;-)).
Tiếp theo, cài đặt mọi thứ bạn cần:
pip install torch transformers langchain llama-index====0.6.0.alpha3
Sao chép đoạn mã sau và điều chỉnh đường dẫn đến thư mục đầu vào của bạn. Nó sử dụng thư viện máy biến áp Huggingface để tạo các phần nhúng để truy xuất và dolly của databrick để tạo đầu ra cuối cùng.
from pathlib import Path
import torch
from transformers import pipeline
from langchain.llms.base import LLM
from llama_index import SimpleDirectoryReader, LangchainEmbedding, GPTVectorStoreIndex, PromptHelper, LLMPredictor, ServiceContext
from llama_index.langchain_helpers.text_splitter import TokenTextSplitter
from llama_index.node_parser.simple import SimpleNodeParser
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
INPUT_FOLDER = "path/to/your/data/folder"
index_files = list(Path(INPUT_FOLDER).glob("*"))
max_input_size = 2048
num_output = 256
max_chunk_overlap = 20
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)
pipe = pipeline("text-generation", model="databricks/dolly-v2-3b", trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto")
embed_model = LangchainEmbedding(HuggingFaceEmbeddings())
class CustomLLM(LLM):
model_name = "databricks/dolly-v2-3b"
def _call(self, prompt, stop = None):
response = pipe(prompt, max_new_tokens=num_output)[0]["generated_text"]
return response
@property
def _identifying_params(self):
return {"name_of_model": self.model_name}
@property
def _llm_type(self):
return "custom"
# define our LLM
llm_predictor = LLMPredictor(llm=CustomLLM())
node_parser = SimpleNodeParser(text_splitter=TokenTextSplitter(chunk_size=512, chunk_overlap=max_chunk_overlap))
prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, embed_model=embed_model, prompt_helper=prompt_helper, node_parser=node_parser, chunk_size_limit=512)
# Load your data
documents = SimpleDirectoryReader(input_files=index_files).load_data()
index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context)
query_engine = index.as_query_engine()
Bước 3: Sử dụng hệ thống của bạn
Chúng tôi đã làm xong rồi! Giờ đây, bạn có thể sử dụng đối tượng công cụ truy vấn để đặt câu hỏi về dữ liệu của mình!
Một lời nhắc ví dụ mà tôi đã thử trên các tài liệu của chúng tôi là như thế này:
print(query_engine.query("Summarize typical use cases of the similarity map in few sentences."))
The Similarity Map helps to explore simulation similarities and find explanations for observable phenomens.
It can be used to group designs by similarity, find outliers and detect correlations between features and target values.