0

I am trying to make a modified GPT model, designed with Python, available for questions on an HTML page. I have tried using PyScript but I do not know how to give it access to the modules that I want.

Here is my python code in the file gpt.py:

from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
from langchain import OpenAI
from IPython.display import Markdown, display

def construct_index(directory_path):
    # set maximum input size
    max_input_size = 4096
    # set number of output tokens
    num_outputs = 2000
    # set maximum chunk overlap
    max_chunk_overlap = 20
    # set chunk size limit
    chunk_size_limit = 600 

    # define prompt helper
    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

    # define LLM
    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
 
    documents = SimpleDirectoryReader(directory_path).load_data()
    
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
    index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)

    index.save_to_disk('index.json')

    return index

def ask_ai():
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    while True: 
        query = input("What do you want to ask? ")
        response = index.query(query)
        print(response)
        display(Markdown(f"Response: <b>{response.response}</b>"))

os.environ["OPENAI_API_KEY"] = "myapikey"
construct_index("context_data/data")
ask_ai()

I tried using PyScript in the following HTML code:

<html>
    <head>
      <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
      <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    </head>

  <body>
    <py-config type="toml">
        packages = ["llama_index", "langchain", "IPython"]

        [[fetch]]
        files = ["./gpt.py"]
    </py-config>
    <py-script>
      from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
      from langchain import OpenAI
      from IPython.display import Markdown, display
      
      os.environ["OPENAI_API_KEY"] = "myapikey"
      construct_index("context_data/data")
      ask_ai()
    </py-script>
  </body>
</html>

But then I got the error:

(PY1001): Unable to install package(s) 'llama_index,langchain,IPython'. Reason: Can't find a pure Python 3 Wheel for package(s) 'llama_index,langchain,IPython'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.

So I followed that link and it told me to make sure all those modules have *py3-none-any.whl files, which they did. Those can be found here:

https://pypi.org/project/langchain/#files

https://pypi.org/project/gpt-index/#files

https://pypi.org/project/ipython/#files

It is possible that llama_index should be called gpt-index, but that doesn't change the error message.

How can I make give my HTML page access to these modules? Thank you!

Nico Rose
  • 13
  • 4
  • When looking at the dev console, i see `can't find a pure Python wheel for 'llama-index', which seems not to have a pure Python wheel: https://pypi.org/project/llama-index/0.5.6/#files – Jeff Glass Apr 03 '23 at 12:35
  • @JeffGlass yes you are right. Do you know if this is still possible without a pure Python wheel or how I can generate one? – Nico Rose Apr 03 '23 at 13:33
  • Check out the Pyodide docs for building wheels for Pyodide https://pyodide.org/en/stable/development/building-and-testing-packages.html – Jeff Glass Apr 03 '23 at 15:54

0 Answers0