-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag.py
More file actions
173 lines (142 loc) · 5.49 KB
/
rag.py
File metadata and controls
173 lines (142 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import json
import openai
from dotenv import load_dotenv
import numpy as np
from tqdm import tqdm
# Load environment variables
load_dotenv()
# Verify API key
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
print("Error: OPENAI_API_KEY not found in .env file")
exit(1)
print(f"API Key found: {api_key[:5]}...{api_key[-5:]}")
# Set OpenAI API key
openai.api_key = api_key
def test_openai_connection():
"""Test if OpenAI API is working"""
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Test"}],
max_tokens=5
)
print("OpenAI API connection successful!")
return True
except Exception as e:
print(f"OpenAI API Error: {str(e)}")
return False
def semantic_search(query, embeddings, top_k=3):
"""Perform semantic search using cosine similarity."""
if not embeddings:
print("Error: No embeddings available for search")
return []
try:
# Get query embedding
response = openai.Embedding.create(
input=query,
model="text-embedding-ada-002"
)
if 'data' not in response or len(response['data']) == 0:
print("Error: Invalid response format for query embedding")
return []
query_embedding = response['data'][0]['embedding']
# Calculate cosine similarity
similarities = []
for essay in embeddings:
try:
similarity = np.dot(query_embedding, essay['embedding']) / (
np.linalg.norm(query_embedding) * np.linalg.norm(essay['embedding'])
)
similarities.append((similarity, essay))
except Exception as e:
print(f"Error calculating similarity: {str(e)}")
continue
# Sort by similarity and get top k results
similarities.sort(reverse=True)
return similarities[:top_k]
except Exception as e:
print(f"Error in semantic search: {str(e)}")
return []
def get_rag_response(query, relevant_essays):
"""Get response from GPT using relevant essays as context."""
if not relevant_essays:
return "Sorry, I couldn't find any relevant essays to answer your question."
try:
# Prepare context from relevant essays
context = "\n\n".join([
f"Title: {essay['title']}\nContent: {essay['content'][:1000]}..."
for _, essay in relevant_essays
])
# Create prompt
prompt = f"""Based on the following context from Paul Graham's essays, answer the question.
Context:
{context}
Question: {query}
Answer:"""
# Get response from GPT
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that answers questions based on Paul Graham's essays."},
{"role": "user", "content": prompt}
],
max_tokens=500
)
if 'choices' in response and len(response['choices']) > 0:
return response['choices'][0]['message']['content']
else:
return "Error: Invalid response format from GPT"
except Exception as e:
print(f"Error getting RAG response: {str(e)}")
return "Sorry, I encountered an error while trying to answer your question."
def main():
# Test OpenAI connection first
if not test_openai_connection():
print("Exiting due to OpenAI API connection failure")
return
# Check if we have embeddings
if not os.path.exists('paul_essays_embeddings.json'):
print("Error: paul_essays_embeddings.json not found. Please run embeddings.py first.")
return
# Load embeddings
print("Loading embeddings...")
with open('paul_essays_embeddings.json', 'r') as f:
embeddings = json.load(f)
print(f"Loaded {len(embeddings)} embeddings")
print("\nPaul Graham RAG System")
print("Type 'quit' to exit")
try:
while True:
try:
query = input("\nEnter your question: ").strip()
if not query:
continue
if query.lower() == 'quit':
print("Goodbye!")
break
# Get relevant essays
relevant_essays = semantic_search(query, embeddings)
if not relevant_essays:
print("No relevant essays found")
continue
# Get and print response
response = get_rag_response(query, relevant_essays)
print("\nResponse:", response)
# Print sources
print("\nSources:")
for _, essay in relevant_essays:
print(f"- {essay['title']}: {essay['url']}")
except KeyboardInterrupt:
print("\nOperation cancelled. Type 'quit' to exit or continue with a new question.")
continue
except Exception as e:
print(f"Error: {str(e)}")
continue
except KeyboardInterrupt:
print("\nGoodbye!")
except Exception as e:
print(f"Fatal error: {str(e)}")
if __name__ == "__main__":
main()