-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
213 lines (180 loc) · 8.2 KB
/
demo.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import time
import logging
import numpy as np
import ollama
from langchain_ollama import OllamaEmbeddings
import ray
def demo():
# Initialize Ray
ray.init()
start_time = time.time() # Start time tracking
desiredModel = 'llama3.2:3b'
logging.Formatter.default_msec_format = '%s.%03d'
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("reverse_vector.log", 'w'),
logging.StreamHandler()
]
)
# Encode the TARGET "mystery" vector to be reversed
# (otherwise, fetch it from somewhere - like a vector database)
TARGET = "Be mindful"
embeddings = OllamaEmbeddings(
model="nomic-embed-text",
)
res = embeddings.embed_documents([TARGET])
v_target = np.array(res)
# Stop conditions
# MATCH_ERROR stop condition selection:
# https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule
# https://www.hackmath.net/en/calculator/normal-distribution?mean=0&sd=1
# Because vector space has unit sigma = 1.0,
# a VECTOR_ERROR == |distance| <= 3.0 is a 99.7% confidence
# that the two points are distinct, or 0.3% that they are the same;
#
# Generally:
# VECTOR_ERROR, Are-the-Same Match Confidence
# 3.0, 0.3%
# 2.0, 4.6%
# 1.0, 31.7%
# 0.667, 50.5%
# 0.6, 55.0%
# 0.5, 61.7%
# 0.333, 73.9%
# 0.2, 84%
# 0.1, 92%
# 0.01, 99.2%
MATCH_ERROR = 0.6 # 55% confidence or better
COST_LIMIT = 60.0 # $60 budget spent
@ray.remote
class SharedState:
def __init__(self):
self.CURRENT_BEST_TEXT = "Be"
self.CURRENT_BEST_ERROR = np.inf
self.GUESSES_MADE = 0
self.TOTAL_COST = 0.0
self.MATCH_FOUND = False
self.PREVIOUS_GUESSES = set()
def update_best_guess(self, text, error):
self.GUESSES_MADE += 1
self.PREVIOUS_GUESSES.add(text.lower())
if error < self.CURRENT_BEST_ERROR:
self.CURRENT_BEST_TEXT = text
self.CURRENT_BEST_ERROR = error
logging.info(">>> New best text: \"%s\", error: %.6f", text, error)
if error <= MATCH_ERROR:
self.MATCH_FOUND = True
def get_state(self):
return {
'CURRENT_BEST_TEXT': self.CURRENT_BEST_TEXT,
'CURRENT_BEST_ERROR': self.CURRENT_BEST_ERROR,
'GUESSES_MADE': self.GUESSES_MADE,
'TOTAL_COST': self.TOTAL_COST,
'PREVIOUS_GUESSES': self.PREVIOUS_GUESSES
}
def is_match_found(self):
return self.MATCH_FOUND
shared_state = SharedState.remote()
# Prompt for the LLM
prompt_template = f"""User input is last iterative guess of an unknown text string and its vector ERROR from the unknown text.
Determine better text strings having lower vector ERRORs and write one such string in English as your entire output.
The goal is to accurately guess the mystery text.
This is a game of guess-and-check.
[clue]
TWO WORDS; CLUE: FIRST WORD IS `Be`; SECOND WORD YOU HAVE TO GUESS.
[/clue]
[IMPORTANT]
- Do NOT repeat any of the previous guesses provided in [context].
- Do NOT include your thought process in your response.
- Your response should be coherent and exactly two words.
[/IMPORTANT]
"""
@ray.remote
def generate_and_evaluate_guess(v_target, shared_state_actor):
embeddings = OllamaEmbeddings(
model="nomic-embed-text",
)
try:
# Get the current best state
state = ray.get(shared_state_actor.get_state.remote())
assist = f"""\nBEST_GUESS: {state['CURRENT_BEST_TEXT']} (ERROR {state['CURRENT_BEST_ERROR']:.4f})"""
previous_guesses = state['PREVIOUS_GUESSES']
# Include previous guesses in the context
if previous_guesses:
previous_guesses_str = ', '.join(f'"{guess}"' for guess in previous_guesses)
assist += f"\nPrevious guesses: {previous_guesses_str}"
else:
assist += "\nNo previous guesses."
m = f"ERROR {state['CURRENT_BEST_ERROR']:.4f}, \"{state['CURRENT_BEST_TEXT']}\""
# Call the assistant to get a new guess
while True:
try:
logging.info("CHAT: Generating new guess with current best error %.4f", state['CURRENT_BEST_ERROR'])
res = ollama.chat(model=desiredModel, messages=[
{
'role': 'user',
'content': "[INST]<<SYS>>" + prompt_template + "\n\n\n [context]" + assist + "[/context] \n\n [user input]" + m + "[/user input]<</SYS>>[/INST]",
},
])
if res['message']:
break
except Exception as e_:
logging.error(e_)
time.sleep(5)
# Extract the guess
TEXT = res['message']['content'].strip()
logging.info("Generated guess: \"%s\"", TEXT)
# Check for duplicates
if TEXT.lower() in previous_guesses:
logging.info("Duplicate guess detected: \"%s\"", TEXT)
return
# Compute the error
res = embeddings.embed_documents([TEXT])
v_text = np.array(res)
dv = v_target - v_text
VECTOR_ERROR = np.sqrt((dv * dv).sum())
logging.info("Computed error for \"%s\": %.6f", TEXT, VECTOR_ERROR)
# Update the shared state if this is a better guess
shared_state_actor.update_best_guess.remote(TEXT, VECTOR_ERROR)
except Exception as e_:
logging.error("%s", e_)
# Main loop
while (not ray.get(shared_state.is_match_found.remote()) and
ray.get(shared_state.get_state.remote())['TOTAL_COST'] < COST_LIMIT):
iteration_start_time = time.time() # Start timing for this iteration
# Number of parallel guesses to generate
NUM_PARALLEL_GUESSES = 50
# Launch workers to generate guesses and compute errors in parallel
futures = [generate_and_evaluate_guess.remote(v_target, shared_state) for _ in range(NUM_PARALLEL_GUESSES)]
ray.get(futures)
# Get current state for logging
state = ray.get(shared_state.get_state.remote())
logging.info("Total guesses made: %d", state['GUESSES_MADE'])
logging.info("Current best guess: \"%s\" with error %.6f", state['CURRENT_BEST_TEXT'], state['CURRENT_BEST_ERROR'])
iteration_end_time = time.time() # End timing for this iteration
iteration_elapsed = iteration_end_time - iteration_start_time
logging.info("Iteration execution time: %.2f seconds", iteration_elapsed)
# After loop ends, print the best guess
state = ray.get(shared_state.get_state.remote())
logging.info("Best guess: \"%s\", error: %.6f", state['CURRENT_BEST_TEXT'], state['CURRENT_BEST_ERROR'])
logging.info("Total guesses made: %d", state['GUESSES_MADE'])
end_time = time.time() # End time tracking
elapsed_time = end_time - start_time
logging.info("Total execution time: %.2f seconds", elapsed_time)
if __name__ == "__main__":
demo()
"""
2024-11-14 23:11:57,785 INFO worker.py:1777 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265
2024-11-14 23:11:58.393 [INFO] HTTP Request: POST http://127.0.0.1:11434/api/embed "HTTP/1.1 200 OK"
2024-11-14 23:11:59.957 [INFO] Total guesses made: 5
2024-11-14 23:11:59.958 [INFO] Current best guess: "Be Aware" with error 0.937819
2024-11-14 23:11:59.958 [INFO] Iteration execution time: 1.45 seconds
2024-11-14 23:12:00.551 [INFO] Total guesses made: 10
2024-11-14 23:12:00.551 [INFO] Current best guess: "Be Mindful" with error 0.000000
2024-11-14 23:12:00.551 [INFO] Iteration execution time: 0.59 seconds
2024-11-14 23:12:00.553 [INFO] Best guess: "Be Mindful", error: 0.000000
2024-11-14 23:12:00.553 [INFO] Total guesses made: 10
2024-11-14 23:12:00.553 [INFO] Total execution time: 2.24 seconds
"""