-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatworm.py
179 lines (128 loc) · 6.16 KB
/
flatworm.py
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
174
175
176
177
178
179
'''
______
/ o o \\
/ \\
/ \\
| |
| |
\ /
\ /
\______/
'''
import pickle
import numpy as np
from gpt4all import GPT4All
from sentence_transformers import SentenceTransformer
# Load the sentence transformer model
model_st = SentenceTransformer('distilbert-base-nli-mean-tokens')
# Load the GPT4All model
model_gpt = GPT4All("orca-mini-3b.ggmlv3.q4_0.bin")
# Function to compute sentence embeddings
def get_embedding(sentence):
return model_st.encode([sentence])
# Function to compute euclidean distance between two embeddings
def compute_euclidean_distance(embedding1, embedding2):
return np.linalg.norm(embedding1 - embedding2)
def gpt4all_generate(query, max_tokens, temp=1.0, top_k=50, top_p=0.95):
return model_gpt.generate(prompt=query, max_tokens=max_tokens, temp=temp, top_k=top_k, top_p=top_p, streaming=False)
def stress_test_gpt4all(query, token_limit=1, similarity_threshold=0.7, max_attempts=10):
temperature_effects = {}
failed_responses = []
for temp in np.linspace(0, 1, max_attempts): # Loop over temperatures
full_response = gpt4all_generate(query, token_limit*5, temp) # Generate a slightly longer response
truncated_response = full_response[:token_limit] # Truncate the response
embedding_query = get_embedding(query)
embedding_full_response = get_embedding(full_response)
embedding_truncated_response = get_embedding(truncated_response)
distance_full = compute_euclidean_distance(embedding_query, embedding_full_response)
distance_truncated = compute_euclidean_distance(embedding_query, embedding_truncated_response)
temperature_effects[f"Temp {temp:.2f}"] = {
'full_response': full_response,
'truncated_response': truncated_response,
'distance_full': distance_full,
'distance_truncated': distance_truncated
}
print(f"Temperature = {temp:.2f}, Full Response = '{full_response}', Distance = {distance_full:.2f}")
print(f"Temperature = {temp:.2f}, Truncated Response = '{truncated_response}', Distance = {distance_truncated:.2f}")
if distance_truncated > similarity_threshold:
failed_responses.append({'response': truncated_response, 'distance': distance_truncated})
results = {
'query': query,
'temperature_effects': temperature_effects,
'failed_responses': failed_responses
}
return results
# Running the stress test and saving results
while True:
user_query = input("Enter your query (or type 'exit' to quit): ")
if user_query.lower() == 'exit':
break
max_attempts = int(input("Enter the maximum number of attempts (e.g., 10): "))
results = stress_test_gpt4all(user_query, max_attempts=max_attempts)
# Save results to a pickle file
with open('stress_test_results.pkl', 'ab') as file:
pickle.dump(results, file)
print("\nResults saved to 'stress_test_results.pkl'.")
import pickle
from gpt4all import GPT4All
import numpy as np
from sentence_transformers import SentenceTransformer
from sentence_transformers import util
from numpy import dot
from numpy.linalg import norm
# Load the sentence transformer model
model_st = SentenceTransformer('distilbert-base-nli-mean-tokens')
# Load the GPT4All model
model_gpt = GPT4All("C://AI_MODELS//llama2_7b_chat_uncensored.ggmlv3.q4_0.bin")
# Load the results from the pickle file
results = []
with open('stress_test_results.pkl', 'rb') as file:
while True:
try:
data = pickle.load(file)
results.append(data)
except EOFError:
break
def get_embedding(sentence):
tensor_output = model_st.encode([sentence])[0]
return tensor_output
def cosine_similarity(v1, v2):
return dot(v1, v2) / (norm(v1) * norm(v2))
def get_response_from_gpt4all(query, max_tokens=100):
tokens = []
for token in model_gpt.generate(query, max_tokens=max_tokens, streaming=True):
tokens.append(token)
return ''.join(tokens)
# Shortened context
context = """
We've been starving agents of tokens.
"""
def get_enhanced_query(user_query):
user_embedding = get_embedding(user_query)
# Get the most relevant result based on cosine similarity
most_relevant_result.get('final_response', 'No response available')
# Simplified context
enhanced_query = f"User's question: {user_query}\n\nContext: We've been stress testing the GPT4All model. In a related test, we asked '{most_relevant_result['query']}' and the model responded with '{most_relevant_result['final_response']}'. What's the best answer to the user's query based on this?"
return enhanced_query
# Check if all results have the 'query' key
for i, result in enumerate(results):
if 'query' not in result:
print(f"Result at index {i} is missing the 'query' key: {result}")
# If all is fine, then compute embeddings
result_embeddings = [get_embedding(result['query']) for result in results if 'query' in result]
def find_most_relevant_result(user_query_embedding):
similarities = [util.pytorch_cos_sim(user_query_embedding, result_embedding).numpy()[0][0] for result_embedding in result_embeddings]
most_relevant_index = np.argmax(similarities)
return results[most_relevant_index]
# Conversational loop
print("You can ask questions about the stress test results. Type 'exit' to end the conversation.")
while True:
user_query = input("Your question: ")
if user_query.lower() == 'exit':
break
user_query_embedding = get_embedding(user_query)
most_relevant_result = find_most_relevant_result(user_query_embedding)
# Create an enhanced prompt with context and the most relevant result
enhanced_query = f"User's question: {user_query}\n\nContext: We've been stress testing the GPT4All model. In a related test, we asked '{most_relevant_result['query']}' and the model responded with '{most_relevant_result.get('final_response', 'No response available')}'. What's the best answer to the user's query based on this?"
response = get_response_from_gpt4all(enhanced_query, max_tokens=2000)
print("Response:", response)