Skip to content

Instantly share code, notes, and snippets.

@mirzap
Forked from amix/query_customer_feedback.py
Created March 11, 2023 21:30
Show Gist options
  • Save mirzap/18d97d43c4ed52b4d0e7ad774ef6ac35 to your computer and use it in GitHub Desktop.
Save mirzap/18d97d43c4ed52b4d0e7ad774ef6ac35 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