|
1 | | -import json |
2 | 1 | import os |
3 | 2 |
|
4 | 3 | import chromadb |
5 | | -import openai |
6 | | -from chromadb.utils import embedding_functions |
| 4 | +from dotenv import load_dotenv |
| 5 | +from openai import OpenAI |
7 | 6 |
|
8 | 7 | os.environ["TOKENIZERS_PARALLELISM"] = "false" |
9 | 8 |
|
10 | | -DATA_PATH = "data/archive/*" |
11 | 9 | CHROMA_PATH = "car_review_embeddings" |
12 | | -EMBEDDING_FUNC_NAME = "multi-qa-MiniLM-L6-cos-v1" |
13 | 10 | COLLECTION_NAME = "car_reviews" |
| 11 | +MODEL = "gpt-5.6-luna" |
14 | 12 |
|
15 | | -with open("config.json", "r") as json_file: |
16 | | - config_data = json.load(json_file) |
| 13 | +load_dotenv() |
17 | 14 |
|
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 | | -) |
| 15 | +openai_client = OpenAI() |
| 16 | +chroma_client = chromadb.PersistentClient(CHROMA_PATH) |
| 17 | +collection = chroma_client.get_collection(name=COLLECTION_NAME) |
28 | 18 |
|
29 | 19 | context = """ |
30 | | - You are a customer success employee at a large |
31 | | - car dealership. Use the following car reviews |
32 | | - to answer questions: {} |
33 | | - """ |
| 20 | +You are a customer success employee at a large |
| 21 | + car dealership. Use the following car reviews |
| 22 | + to answer questions: {} |
| 23 | +""" |
34 | 24 |
|
35 | 25 | question = """ |
36 | | - What's the key to great customer satisfaction |
37 | | - based on detailed positive reviews? |
38 | | - """ |
| 26 | +What's the key to great customer satisfaction |
| 27 | + based on detailed positive reviews? |
| 28 | +""" |
39 | 29 |
|
40 | 30 | good_reviews = collection.query( |
41 | 31 | query_texts=[question], |
|
46 | 36 |
|
47 | 37 | reviews_str = ",".join(good_reviews["documents"][0]) |
48 | 38 |
|
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 | 39 | print("Good reviews: ") |
62 | 40 | print(reviews_str) |
63 | 41 | print("###########################################") |
64 | 42 |
|
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, |
| 43 | +good_review_summaries = openai_client.responses.create( |
| 44 | + model=MODEL, |
| 45 | + instructions=context.format(reviews_str), |
| 46 | + input=question, |
73 | 47 | ) |
74 | 48 |
|
75 | 49 | print("AI-Generated summary of good reviews: ") |
76 | | -print(good_review_summaries["choices"][0]["message"]["content"]) |
| 50 | +print(good_review_summaries.output_text) |
77 | 51 | print("###########################################") |
78 | 52 |
|
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 | 53 | question = """ |
85 | | - Which of these poor reviews has the worst implications about |
86 | | - our dealership? Explain why. |
87 | | - """ |
| 54 | +Which of these poor reviews has the |
| 55 | + worst implications about our dealership? |
| 56 | + Explain why. |
| 57 | +""" |
88 | 58 |
|
89 | 59 | poor_reviews = collection.query( |
90 | 60 | query_texts=[question], |
|
99 | 69 | print(poor_reviews["documents"][0][0]) |
100 | 70 | print("###########################################") |
101 | 71 |
|
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, |
| 72 | +poor_review_analysis = openai_client.responses.create( |
| 73 | + model=MODEL, |
| 74 | + instructions=context.format(reviews_str), |
| 75 | + input=question, |
110 | 76 | ) |
111 | 77 |
|
112 | 78 | print("AI-Generated summary of the single worst review: ") |
113 | | -print(poor_review_analysis["choices"][0]["message"]["content"]) |
| 79 | +print(poor_review_analysis.output_text) |
114 | 80 | print("###########################################") |
0 commit comments