Igor's Techno Club

Creating a simple "AI Chat with your own PDF" application

All you need is to have installed Python and it's package manager (I use mamba). Please note that you will need to have OpenAI keys generated which you can read more about here

Install required packages

You will need llama-index and openai

The code

import openai
import os

openai.api_key = "YOUR_API_KEY"
os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"

with open("/path/to/any/text/file.md", "r") as f:
    text = f.read()

from llama_index.llms import OpenAI
llm = OpenAI(model="gpt-3.5-turbo", temperature=0)

from llama_index import Prompt

text_qa_template = Prompt(
    "Context information is below.\n"
    "---------------------\n"
    "{context_str}\n"
    "---------------------\n"
    "Given the context information and not prior knowledge, "
    "answer the question: {query_str}\n"
)

question = "Summarize this text"
prompt = text_qa_template.format(context_str=text, query_str=question)
response = llm.complete(prompt)
print(response.text)

#ai #llamaindex #openai #python