mobowuhan/DevSecOpsKB
0
1from llama_index import SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage2from langchain.chat_models import ChatOpenAI3import gradio as gr4import sys5import os6os.environ["OPENAI_API_KEY"] = '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'7 8def create_service_context():9 #constraint parameters10 max_input_size = 409611 num_outputs = 51212 max_chunk_overlap = 113 chunk_size_limit = 60014 #allows the user to explicitly set certain constraint parameters15 prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)16 #LLMPredictor is a wrapper class around LangChain's LLMChain that allows easy integration into LlamaIndex17 llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs))18 #constructs service_context19 service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)20 return service_context21 22def data_ingestion_indexing(directory_path):23 #loads data from the specified directory path24 documents = SimpleDirectoryReader(directory_path).load_data()25 26 #when first building the index27 index = GPTVectorStoreIndex.from_documents(28 documents, service_context=create_service_context()29 )30 #persist index to disk, default "storage" folder31 index.storage_context.persist()32 return index33 34def data_querying(input_text):35 #rebuild storage context36 storage_context = StorageContext.from_defaults(persist_dir="./storage")37 #loads index from storage38 index = load_index_from_storage(storage_context, service_context=create_service_context())39 40 #queries the index with the input text41 response = index.as_query_engine().query(input_text)42 43 return response.response44 45iface = gr.Interface(fn=data_querying,46 inputs=gr.components.Textbox(lines=7, label="Enter your question"),47 outputs="text",48 title="Wenqi's Custom-trained DevSecOps Knowledge Base")49#passes in data directory50index = data_ingestion_indexing("data")51iface.launch(share=True)52 