|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import chromadb |
| 5 | +import openai |
| 6 | +from chromadb.utils import embedding_functions |
| 7 | + |
| 8 | +os.environ["TOKENIZERS_PARALLELISM"] = "false" |
| 9 | + |
| 10 | +DATA_PATH = "data/archive/*" |
| 11 | +CHROMA_PATH = "car_review_embeddings" |
| 12 | +EMBEDDING_FUNC_NAME = "multi-qa-MiniLM-L6-cos-v1" |
| 13 | +COLLECTION_NAME = "car_reviews" |
| 14 | + |
| 15 | +with open("config.json", "r") as json_file: |
| 16 | + config_data = json.load(json_file) |
| 17 | + |
| 18 | +openai.api_key = config_data.get("openai-secret-key") |
| 19 | + |
| 20 | +client = chromadb.PersistentClient(CHROMA_PATH) |
| 21 | +embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction( |
| 22 | + model_name=EMBEDDING_FUNC_NAME |
| 23 | +) |
| 24 | + |
| 25 | +collection = client.get_collection( |
| 26 | + name=COLLECTION_NAME, embedding_function=embedding_func |
| 27 | +) |
| 28 | + |
| 29 | +context = """ |
| 30 | + You are a customer success employee at a large |
| 31 | + car dealership. Use the following car reviews |
| 32 | + to answer questions: {} |
| 33 | + """ |
| 34 | + |
| 35 | +question = """ |
| 36 | + What's the key to great customer satisfaction |
| 37 | + based on detailed positive reviews? |
| 38 | + """ |
| 39 | + |
| 40 | +good_reviews = collection.query( |
| 41 | + query_texts=[question], |
| 42 | + n_results=10, |
| 43 | + include=["documents"], |
| 44 | + where={"Rating": {"$gte": 3}}, |
| 45 | +) |
| 46 | + |
| 47 | +reviews_str = ",".join(good_reviews["documents"][0]) |
| 48 | + |
| 49 | +good_review_summaries = openai.ChatCompletion.create( |
| 50 | + model="gpt-3.5-turbo", |
| 51 | + messages=[ |
| 52 | + {"role": "system", "content": context.format(reviews_str)}, |
| 53 | + {"role": "user", "content": question}, |
| 54 | + ], |
| 55 | + temperature=0, |
| 56 | + n=1, |
| 57 | +) |
| 58 | + |
| 59 | +reviews_str = ",".join(good_reviews["documents"][0]) |
| 60 | + |
| 61 | +print("Good reviews: ") |
| 62 | +print(reviews_str) |
| 63 | +print("###########################################") |
| 64 | + |
| 65 | +good_review_summaries = openai.ChatCompletion.create( |
| 66 | + model="gpt-3.5-turbo", |
| 67 | + messages=[ |
| 68 | + {"role": "system", "content": context.format(reviews_str)}, |
| 69 | + {"role": "user", "content": question}, |
| 70 | + ], |
| 71 | + temperature=0, |
| 72 | + n=1, |
| 73 | +) |
| 74 | + |
| 75 | +print("AI-Generated summary of good reviews: ") |
| 76 | +print(good_review_summaries["choices"][0]["message"]["content"]) |
| 77 | +print("###########################################") |
| 78 | + |
| 79 | + |
| 80 | +context = """ |
| 81 | + You are a customer success employee at a large car dealership. |
| 82 | + Use the following car reivews to answer questions: {} |
| 83 | + """ |
| 84 | +question = """ |
| 85 | + Which of these poor reviews has the worst implications about |
| 86 | + our dealership? Explain why. |
| 87 | + """ |
| 88 | + |
| 89 | +poor_reviews = collection.query( |
| 90 | + query_texts=[question], |
| 91 | + n_results=5, |
| 92 | + include=["documents"], |
| 93 | + where={"Rating": {"$lte": 3}}, |
| 94 | +) |
| 95 | + |
| 96 | +reviews_str = ",".join(poor_reviews["documents"][0]) |
| 97 | + |
| 98 | +print("Worst reviews: ") |
| 99 | +print(poor_reviews["documents"][0][0]) |
| 100 | +print("###########################################") |
| 101 | + |
| 102 | +poor_review_analysis = openai.ChatCompletion.create( |
| 103 | + model="gpt-3.5-turbo", |
| 104 | + messages=[ |
| 105 | + {"role": "system", "content": context.format(reviews_str)}, |
| 106 | + {"role": "user", "content": question}, |
| 107 | + ], |
| 108 | + temperature=0, |
| 109 | + n=1, |
| 110 | +) |
| 111 | + |
| 112 | +print("AI-Generated summary of the single worst review: ") |
| 113 | +print(poor_review_analysis["choices"][0]["message"]["content"]) |
| 114 | +print("###########################################") |
0 commit comments