5

I'm using the llama-index code below to create an index object from a saved text corpus. I'm then loading the saved index object and querying it to produce a response. I'm using an openai apikey so I can use a chatgpt model for the LLM. I'm wondering if I could use the same code or a modified version to use an open source LLM like for example llama-7b-chat that I have downloaded the model weights for on my local machine. does anyone know if that is possible and can you suggest how I would need to update the code below to use an opensource LLM hosted locally?

code:

# creating index from corpus

from config import api_key, old_api_key, personal_api_key

import os

os.environ['OPENAI_API_KEY'] = old_api_key


# Load you data into 'Documents' a custom type by LlamaIndex
# from typing_extensions import Protocol
from llama_index import SimpleDirectoryReader

documents = SimpleDirectoryReader('./data').load_data()


from llama_index import GPTVectorStoreIndex

index = GPTVectorStoreIndex.from_documents(documents)

# save storage context

storage_context_dict=index.storage_context.to_dict()

import json

# Serialize data into file:
json.dump( storage_context_dict, open( "general_attributes_storage_context_dict.json", 'w' ) )

# load saved context

import os

# plus
os.environ['OPENAI_API_KEY'] = old_api_key


# using previously saved index
import json

saved_context=json.load( open( "general_attributes_storage_context_dict.json" ) )

from llama_index import StorageContext, load_index_from_storage

# rebuild storage context

storage_context=StorageContext.from_dict(saved_context)    

stored_index=load_index_from_storage(storage_context)


query_engine = stored_index.as_query_engine()
response = query_engine.query("some question")
print(response)

1 Answer 1

1
from llama_index import SimpleDirectoryReader, ServiceContext, VectorStoreIndex
from llama_index import StorageContext, load_index_from_storage

llm = ('load local llm')

service_context = ServiceContext.from_defaults(llm=llm)


documents = SimpleDirectoryReader('./data').load_data()
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
index.storage_context.persist("data_index")
context = StorageContext.from_defaults(persist_dir='data_index')

stored_index = load_index_from_storage(context, service_context=service_context)

query_engine = stored_index.as_query_engine()
response = query_engine.query("some question")
print(response)
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for getting back to me with this. In the "llm = ('load local llm')", what should be in the place of 'load local llm'. I tried putting in the name of the model or the path to where I downloaded the weights but just get a "string has no attribute metadata" error.
from langchain import HuggingFacePipeline from transformers import AutoTokenizer, pipeline, AutoModelForCausalLM model_path = 'model_path_from_local_or_huggingface tokenizer = AutoTokenizer.from_pretrained(model_path) model = AutoModelForCausalLM.from_pretrained(model_path) text_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer) llm = HuggingFacePipeline(pipeline=text_pipeline, model_kwargs={"temperature": 0})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.