import os from llama_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper import gradio as gr import openai from langchain import OpenAI os.environ["OPENAI_API_KEY"] = os.environ["OPENAI_API_KEY"] openai_engines = ["text-davinci-003", "code-davinci-002", "text-curie-001"] prompt = "" def construct_index(directory_path): max_input_size = 4096 num_outputs = 512 max_chunk_overlap = 20 chunk_size_limit = 600 prompt_helper = PromptHelper( max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit) llm_predictor = LLMPredictor(llm=OpenAI( temperature=0.3, model_name="text-davinci-003", max_tokens=num_outputs)) documents = SimpleDirectoryReader(directory_path).load_data() index = GPTSimpleVectorIndex( documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper) index.save_to_disk('index.json') return index def chatbot_local(input_text): index = GPTSimpleVectorIndex.load_from_disk('index.json') query_configs = [ { "index_struct_type": "simple_dict", "query_mode": "default", "query_kwargs": { "similarity_top_k": 1, # "include_summary": True }, }, { "index_struct_type": "list", "query_mode": "default", "query_kwargs": { "response_mode": "tree_summarize", "verbose": True } }, ] response = index.query(input_text, mode="recursive", response_mode="compact", query_configs = query_configs) return response.response def openai_completion( prompt, openai_token=None, engine="text-davinci-003", temperature=0.9, max_tokens=150, top_p=1, frequency_penalty=0, presence_penalty=0.6, stop=[" Human:", " AI:"], ): openai.api_key = openai_token response = openai.Completion.create( engine=engine, prompt=prompt, temperature=temperature, max_tokens=max_tokens, top_p=top_p, frequency_penalty=frequency_penalty, presence_penalty=presence_penalty, stop=stop, ) return response.choices[0].text def chatgpt3( prompt, history, ): history = history or [] # history_prompt = list(sum(history, ())) # history_prompt.append(f"\nHuman: {prompt}") # inp = " ".join(history_prompt) # keep the prompt length limited to ~2000 tokens # create the output with openai out = chatbot_local(prompt) history.append((prompt, out)) return history, history, "" with gr.Blocks(title="ASDOS") as block: gr.Markdown("## ASDOS") with gr.Row(): # with gr.Column(): # openai_token = gr.Textbox(label="OpenAI API Key", value=os.getenv("OPENAI_API_KEY")) # engine = gr.Dropdown( # label="GPT3 Engine", # choices=openai_engines, # value="text-davinci-003", # ) # temperature = gr.Slider(label="Temperature", minimum=0, maximum=1, step=0.1, value=0.9) # max_tokens = gr.Slider(label="Max Tokens", minimum=10, maximum=400, step=10, value=150) # top_p = gr.Slider(label="Top P", minimum=0, maximum=1, step=0.1, value=1) # frequency_penalty = gr.Slider( # label="Frequency Penalty", # minimum=0, # maximum=1, # step=0.1, # value=0, # ) # presence_penalty = gr.Slider( # label="Presence Penalty", # minimum=0, # maximum=1, # step=0.1, # value=0.6, # ) with gr.Column(): chatbot = gr.Chatbot() message = gr.Textbox(value=prompt, label="Saya adalah asisten dosen kuncie", placeholder="Minta tolong apapun yg berkaitan dengan kelas ini") state = gr.State() message.submit( fn=chatgpt3, inputs=[ message, state, ], outputs=[chatbot, state, message], ) submit = gr.Button("Send") submit.click( chatgpt3, inputs=[ message, state, # openai_token, # engine, # temperature, # max_tokens, # top_p, # frequency_penalty, # presence_penalty, ], outputs=[chatbot, state, message], ) if __name__ == "__main__": index = construct_index("docs") block.launch(debug=True)