-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question]: How to add previous Chat context when querying an index? #7296
Comments
🤖 Hello, To send the context of your chat application to LlamaIndex via the .query() function, you can use the Here is a simplified example of how you can use it: from llama_index.chat_engine.context import ContextChatEngine
from llama_index.indices.base_retriever import BaseRetriever
from llama_index.llms.base import LLM, ChatMessage
from llama_index.memory import BaseMemory
from llama_index.indices.service_context import ServiceContext
# Initialize your components
retriever = BaseRetriever() # Replace with your actual retriever
llm = LLM() # Replace with your actual LLM
memory = BaseMemory() # Replace with your actual memory
service_context = ServiceContext() # Replace with your actual service context
prefix_messages = [ChatMessage(role="system", content="Your system prompt here")] # Replace with your actual prefix messages
# Initialize the chat engine
chat_engine = ContextChatEngine(
retriever=retriever,
llm=llm,
memory=memory,
prefix_messages=prefix_messages,
)
# Use the chat method
message = "Your message here"
chat_history = [ChatMessage(role="user", content="Previous message")] # Replace with your actual chat history
response = chat_engine.chat(message, chat_history)
print(response) Please replace the placeholders with your actual components. The You can find more details about the I hope this helps! If you have any other questions, feel free to ask. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
I have the same question, while this method works to inject previous chat history, it seems to forgo a key function of the query engine which is to constrain the response to the knowledge base provided. ContextChatEngine does not seems to be limited to the index (at least I don't know how to). |
@mcmoochi the context chat engine CAN be restrained, you just have to write a system prompt that encourages that behaviour. Tbh the system prompt is powerful if written well. Other options include the
|
Thanks very much for the response. I have tried the system prompt, it doesn't seem to be working for me for some reason. For example, I'm passing this system prompt: "Don't answer if question is not directly related to [My company]." and using. "how to cook a potato" as a control question. My index only consist of terms of conditions of products, currently the bot will give a detailed response on how to cook a potato... Condense question seems to do the same thing, not sure if I'm doing something wrong. I'll try the agent forced too calling. Any help would be greatly appreciated! Here are my codes:
|
@logan-markewich When using the chat engine instead of the query engine, don’t you lose the ability to also use the data as context? It also does not look like the chat engine supports streaming like the query engine does with |
@niels-bosman nope, you still get the data as context. Each chat engine uses the index under the hood in slightly different ways Also, with chat engines, streaming is enabled by using specific endpoints -- Then, you can iterate over the response Going to link some docs pages that relevant, but I think it's safe to close this for now. Feel free to reach out on discord! https://gpt-index.readthedocs.io/en/latest/examples/agent/openai_agent.html#streaming-chat |
Question Validation
Question
I am looking for a way to send the context of my chat application to Llama index via the .query() function but it seems like it only accepts a string:
I would like to know how I can make it so Llama understands the context of my chats like I can do with OpenAI:
Any tips on how I can achieve this using the Llama API? Thanks.
The text was updated successfully, but these errors were encountered: