Jakob commited on
Commit
67543b3
·
1 Parent(s): 44fb760
Files changed (2) hide show
  1. requirements.txt +6 -0
  2. validation.py +87 -0
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain==0.0.188
2
+ openai
3
+ llama_index==0.6.12
4
+ pypdf
5
+ PyCryptodome
6
+ ratelimit
validation.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from llama_index import SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage
3
+ from langchain.chat_models import ChatOpenAI
4
+ import gradio as gr
5
+ import sys
6
+ import os
7
+ import openai
8
+ from ratelimit import limits, sleep_and_retry
9
+ from langchain import HuggingFaceHub
10
+
11
+
12
+ # fixing bugs
13
+ # 1. open ai key: https://stackoverflow.com/questions/76425556/tenacity-retryerror-retryerrorfuture-at-0x7f89bc35eb90-state-finished-raised
14
+ # 2. rate limit error in lang_chain default version - install langchain==0.0.188. https://github.com/jerryjliu/llama_index/issues/924
15
+ # 3. added true Config variable in langchain: https://github.com/pydantic/pydantic/issues/3320
16
+ # 4. deploy on huggingfaces https://huggingface.co/welcome
17
+ # create huggingfaces token https://huggingface.co/settings/tokens
18
+ # login: huggingface-cli login
19
+ # add requirements.txt file https://huggingface.co/docs/hub/spaces-dependencies
20
+
21
+ os.environ["OPENAI_API_KEY"] = os.environ.get("openai_key")
22
+ openai.api_key = os.environ["OPENAI_API_KEY"]
23
+
24
+ # Define the rate limit for API calls (requests per second)
25
+ RATE_LIMIT = 3
26
+
27
+ # Implement the rate limiting decorator
28
+ @sleep_and_retry
29
+ @limits(calls=RATE_LIMIT, period=1)
30
+ def create_service_context():
31
+
32
+ #constraint parameters
33
+ max_input_size = 4096
34
+ num_outputs = 512
35
+ max_chunk_overlap = 20
36
+ chunk_size_limit = 600
37
+
38
+ #allows the user to explicitly set certain constraint parameters
39
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
40
+
41
+ #LLMPredictor is a wrapper class around LangChain's LLMChain that allows easy integration into LlamaIndex
42
+ llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
43
+
44
+ #constructs service_context
45
+ service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
46
+ return service_context
47
+
48
+
49
+ # Implement the rate limiting decorator
50
+ @sleep_and_retry
51
+ @limits(calls=RATE_LIMIT, period=1)
52
+ def data_ingestion_indexing(directory_path):
53
+
54
+ #loads data from the specified directory path
55
+ documents = SimpleDirectoryReader(directory_path).load_data()
56
+
57
+ #when first building the index
58
+ index = GPTVectorStoreIndex.from_documents(
59
+ documents, service_context=create_service_context()
60
+ )
61
+
62
+ #persist index to disk, default "storage" folder
63
+ index.storage_context.persist()
64
+
65
+ return index
66
+
67
+ def data_querying(input_text):
68
+
69
+ #rebuild storage context
70
+ storage_context = StorageContext.from_defaults(persist_dir="./storage")
71
+
72
+ #loads index from storage
73
+ index = load_index_from_storage(storage_context, service_context=create_service_context())
74
+
75
+ #queries the index with the input text
76
+ response = index.as_query_engine().query(input_text)
77
+
78
+ return response.response
79
+
80
+ iface = gr.Interface(fn=data_querying,
81
+ inputs=gr.components.Textbox(lines=30, label="Enter your question"),
82
+ outputs=gr.components.Textbox(lines=40, label="Response", style="height: 400px; overflow-y: scroll;"),
83
+ title="Therapy Validation GPT 0.1 pre alpha")
84
+
85
+ #passes in data directory
86
+ index = data_ingestion_indexing("book-validation")
87
+ iface.launch(inline=True)