Skip to content

Instantly share code, notes, and snippets.

@amix
Last active July 25, 2023 17:52
Show Gist options
  • Save amix/d20187ff35fd3073b356a7b8feb1e657 to your computer and use it in GitHub Desktop.
Save amix/d20187ff35fd3073b356a7b8feb1e657 to your computer and use it in GitHub Desktop.
Use LlamaIndex and GPT-3 to query customer insights
import os
import logging
import sys
import textwrap
from llama_index import (
GPTKeywordTableIndex,
SimpleDirectoryReader,
LLMPredictor,
)
from langchain import OpenAI
if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, level=logging.CRITICAL)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
if not os.path.exists("index.json"):
documents = SimpleDirectoryReader("docs").load_data()
llm_predictor = LLMPredictor(
llm=OpenAI(temperature=0,
model_name="text-davinci-003",
max_tokens=2048)
)
index = GPTKeywordTableIndex(documents, llm_predictor=llm_predictor)
index.save_to_disk("index.json")
else:
index = GPTKeywordTableIndex.load_from_disk("index.json")
while True:
try:
prompt = input("What should I figure out? ")
response = index.query(prompt)
response = str(response).strip()
if not response:
continue
for line in textwrap.wrap(response, width=75):
print(line)
print("-----")
except KeyboardInterrupt:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment