Sarath0x8f commited on
Commit
51e5312
1 Parent(s): 51eda4f

Create bot.py

Browse files
Files changed (1) hide show
  1. bot.py +41 -0
bot.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ import time
3
+ from datetime import datetime
4
+ from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
5
+ from llama_parse import LlamaParse
6
+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding
7
+
8
+ llm = llm = HuggingFaceInferenceAPI(
9
+ model_name="meta-llama/Llama-3.2-1B"
10
+ )
11
+
12
+ parser = LlamaParse(api_key='llx-zKtsC5UBLs8DOApOsLluXMBdQhC75ea0Vs80SmPSjsmDzuhh', result_type='markdown')
13
+ file_extractor = {'.pdf': parser}
14
+ documents = SimpleDirectoryReader('data/', file_extractor=file_extractor).load_data()
15
+
16
+ embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
17
+ vector_index = VectorStoreIndex.from_documents(documents, embed_model=embed_model)
18
+ query_engine = vector_index.as_query_engine(llm=llm)
19
+
20
+ def query_with_retry(query, max_retries=3, wait_time=5):
21
+ for attempt in range(max_retries):
22
+ try:
23
+ start_time = datetime.now()
24
+ response = query_engine.query(query)
25
+ end_time = datetime.now()
26
+ duration = (end_time - start_time).total_seconds()
27
+ print(f"Query completed in {duration:.2f} seconds.\n {response}")
28
+ return response
29
+ except httpx.ReadTimeout:
30
+ if attempt < max_retries - 1:
31
+ print(f"Timeout occurred. Retrying in {wait_time} seconds...")
32
+ time.sleep(wait_time)
33
+ else:
34
+ raise
35
+ except Exception as e:
36
+ print(f"An error occurred: {e}")
37
+ break
38
+
39
+ if __name__ == "__main__":
40
+ q3= 'Your task is to act as my personal [UHV] professor. Provide a detailed, well-structured explanation on the topic of [What are the programs needed to achieve the comprehensive human goal?]. Begin with an engaging introduction, followed by a comprehensive description, and break down key concepts under relevant subheadings. The content should be thorough and professionally written, similar to educational resources found on sites like GeeksforGeeks, JavaTpoint, and other learning platforms'
41
+ print(query_with_retry(q3))