Find answers from the community

Updated 3 months ago

def api_get_chatbot_response(self, project_id, question, index): answer = ""

def api_get_chatbot_response(self, project_id, question, index):
answer = ""
try:
persist_dir = self.config.host.path + f"projects/{project_id}/docs/reports/llama_vectordb"
# Set your OpenAI API key
openai.api_key = ""
index = load_index_from_storage(
StorageContext.from_defaults(persist_dir=persist_dir)
)

qa_prompt = PromptTemplate(
)


class RAGStringQueryEngine(CustomQueryEngine):
"""RAG String Query Engine."""

retriever: BaseRetriever
response_synthesizer: BaseSynthesizer
llm: OpenAI
qa_prompt: PromptTemplate

def custom_query(self, query_str: str):
nodes = self.retriever.retrieve(query_str)

context_str = "\n\n".join([n.node.get_content() for n in nodes])
response = self.llm.complete(
qa_prompt.format(context_str=context_str, query_str=query_str)
)

return str(response)

# configure retriever
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=2,
)

# configure response synthesizer
response_synthesizer = get_response_synthesizer(
streaming=True,
response_mode="tree_summarize",
)
llm = OpenAI(model="gpt-3.5-turbo")

# assemble query engine
query_engine = RAGStringQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
llm=llm,
qa_prompt=qa_prompt,
)

answer = str(query_engine.query(question))

I want to load the index separetly and keep it loaded , I will call this api to get the answer everytime
k
2 comments
Please stand by @Joey as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Based on the information provided in the extracts, you can load the index separately and keep it loaded. However, you need to ensure that the index is not modified during the lifetime of the application, as this could lead to inconsistencies.

Here's an example of how you can load the index:

Plain Text
from llama_index import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
from llama_index.llms import OpenAI

# Necessary to use the latest OpenAI models that support function calling API
service_context = ServiceContext.from_defaults(
    llm=OpenAI(model="gpt-3.5-turbo-0613")
)
data = SimpleDirectoryReader(input_dir="../data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(data, service_context=service_context)

(Source: GitHub ())

Once the index is loaded, you can use it to create a chat engine and get responses:

Plain Text
chat_engine = index.as_chat_engine(chat_mode="openai", verbose=True)
response = chat_engine.chat("Your question here")
print(response)

(Source: GitHub ())

Please note that you need to replace "Your question here" with the actual question you want to ask.

Remember to install the LlamaIndex library if you haven't done so:

Plain Text
!pip install llama-index

(Source: GitHub ())

Please note that the code snippets provided are based on the information available in the extracts and may need to be adjusted to fit your specific use case.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord