-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (58 loc) · 2.33 KB
/
app.py
File metadata and controls
72 lines (58 loc) · 2.33 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
from mpi4py import MPI
from openai import OpenAI
import os
from dotenv import load_dotenv
from loguru import logger
load_dotenv()
from helper import Node, mcts_iteration
def main():
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
# Add rank to logger context
logger.configure(extra={"rank": rank})
logger.info(f"Process {rank} started")
try:
client = OpenAI(api_key=os.getenv('GROQ_API_KEY'),
base_url=os.getenv('GROQ_API_BASE_URL'))
logger.debug("OpenAI client initialized")
except Exception as e:
logger.error(f"Failed to initialize OpenAI client: {e}")
comm.Abort()
return
if rank == 0:
# Get the question from user
question = input("Enter your question: ")
logger.info(f"Question received: {question}")
# Initialize MCTS with "I don't know" as root
root = Node("I don't know")
root.visits = 1
num_iterations = 3 # Adjust based on desired depth/breadth
for i in range(num_iterations):
logger.info(f"Starting MCTS iteration {i+1}")
mcts_iteration(client, question, root, comm, rank, size)
# Find best path from root to leaf
current = root
path = [current]
while current.children:
current = max(current.children, key=lambda x: x.total_score/x.visits if x.visits > 0 else 0)
path.append(current)
# Print results
print("\nMCTS Search Results:")
print(f"Question: {question}")
print("\nAnswer evolution:")
for i, node in enumerate(path):
print(f"\nIteration {i}:")
print(f"Answer: {node.response}")
print(f"Score: {node.total_score/node.visits if node.visits > 0 else 0:.2f}")
print(f"Visits: {node.visits}")
print(f"\nBest final answer: {path[-1].response}")
print(f"Final score: {path[-1].total_score/path[-1].visits if path[-1].visits > 0 else 0:.2f}")
else:
# Worker processes participate in MCTS iterations
num_iterations = 3
for _ in range(num_iterations):
mcts_iteration(client, "", root=None, comm=comm, rank=rank, size=size)
logger.info(f"Process {rank} finished")
if __name__ == "__main__":
main()