Find answers from the community

Updated 2 years ago

how the query engine decides if it find

At a glance
how the query engine decides if it find response from llama index or not
def create_index(path):
max_input = 4096
tokens = 512
chunk_size = 600
max_chunk_overlap = 0.2
promptHelper = PromptHelper(max_input, tokens, max_chunk_overlap, chunk_size_limit=chunk_size)

# Define LLm
llmPredictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="text-davinci-003", max_tokens=tokens))

# Load data
docs = SimpleDirectoryReader(path).load_data()

# Create vector index
service_context = ServiceContext.from_defaults(llm_predictor=llmPredictor, prompt_helper=promptHelper)

vectorIndex = GPTVectorStoreIndex.from_documents(documents=docs, service_context=service_context)
vectorIndex.storage_context.persist(persist_dir='index_store')
return vectorIndex


def answerMe(question):
storage_context = StorageContext.from_defaults(persist_dir='index_store')
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine(streaming=True,retriever=True ,verbose=True)
response = query_engine.query(question)
print(query_engine.query(question).print_response_stream())
# i want to check something weather the query engine find a response to just return it if not i want tot do smth else >>(what should i check)
return response
L
3
15 comments
It's up to the LLM to decide that πŸ™‚ We just show the model some text, and it's up to it to decide how best to respond
If you want, you could use a chat engine or data agent, which may help with this use-case
i want something to use as a flag or something to do if condition if the response is from the document (index) or not
Not really possible at the moment, at least with just a query engine. The chat engine and data agents give you more control in how to instruct the model to respond though, by setting a system prompt

https://gpt-index.readthedocs.io/en/latest/examples/chat_engine/chat_engine_context.html

https://gpt-index.readthedocs.io/en/latest/examples/agent/openai_agent_with_query_engine.html
do they have the same accuracy and functionalty of the basic query engine ?
It uses a query engine under the hood yes, but also an extra "chat" layer on top, allowing for more normal interaction and conversation history
okay could you guide me how to use the chatengine in the code above ?
and how to add the condition to instruct
The notebook is pretty thorough, but
Plain Text
from llama_index.memory import ChatMemoryBuffer

memory = ChatMemoryBuffer.from_defaults(token_limit=1500)

chat_engine = index.as_chat_engine(
    chat_mode="context",
    memory=memory,
    system_prompt="You are a chatbot, able to have normal interactions, as well as talk about an essay discussing Paul Grahams life.",
)
The system prompt is where you put instructions on how to act
okay thankyou
ValueError: Unknown chat mode: context i am getting these always why do you think that ?
def create_index(path):
max_input = 4096
tokens = 512
chunk_size = 600
max_chunk_overlap = 0.2
promptHelper = PromptHelper(max_input, tokens, max_chunk_overlap, chunk_size_limit=chunk_size)

# Define LLm
llmPredictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="text-davinci-003", max_tokens=tokens))

# Load data
docs = SimpleDirectoryReader(path).load_data()

# Create vector index
service_context = ServiceContext.from_defaults(llm_predictor=llmPredictor, prompt_helper=promptHelper)

vectorIndex = GPTVectorStoreIndex.from_documents(documents=docs, service_context=service_context)
vectorIndex.storage_context.persist(persist_dir='index_store')
return vectorIndex


def answerMe(question):
from llama_index import VectorStoreIndex, SimpleDirectoryReader
from llama_index.memory import ChatMemoryBuffer
data = SimpleDirectoryReader('uploads').load_data()
index = VectorStoreIndex.from_documents(data)
memory = ChatMemoryBuffer.from_defaults(token_limit=1500)
chat_engine = index.as_chat_engine(
chat_mode="context",
memory=memory,
system_prompt="You are a chatbot, able to have normal interactions, as well as talk the index,if the response is negative return none.",
)
response = chat_engine.chat(question)
return response
here is my two methods
Try updating your llama index version

pip install --upgrade llama_index
Add a reply
Sign up and join the conversation on Discord