Import Torch
From Transformers Import Pipeline, AutotoKenizer, AutomodelforcaSallm

Load the pretended language model

Model_name = "GPT-ENE-2.7b" # You can change it to GPT-J or any other
Tokenizer = AutotoKenizer.from_pretrained (Model_name)
Model = automodelphorcauseallm.from_pretrained (model_name)

Create the automated loop function

Def experiment_loop (initial_Question, max_cycles = 10):
Prompt = f " {Initial_Question} </ -thinking>"
Effectiveness = 100 # initializes the percentage of effectiveness
Communication = "Initializing experiment."
Response_log = []

For Cycle in Range (Max_Cycles):

Generate the model response

inputs = tokenizer (prompt, return_tensors = "pt"). input_ids
outputs = model.Generate (inputs, max_length = 200)
Response = Tokenizer.decode (outputs [0], skip_special_tokens = true)

Decompose the answer in affirmation and new question

AFFIRMATION = EXTRACT_FFIRMATION (Response)
New_Question = extract_Question (Response)

Update the status of effectiveness

EFFECTIVESS = min (1000, Effectiveness + 10 * Cycle) # Example of Effectiveness

User communication

Communication = F "Cycle {Cycle + 1}: Affirming: '{AffIRMATION}' | New Question: '{New_Question}' '"

Save the current cycle in the log

Response_log.append ((Affirming, New_Question, Effectiveness, Communication)))

Verify if the model decides to stop

if "rest" in responsio:
Final_output = Generate_final_output (Response_log)
Return final_output

Update the prompt with the new statement and question

prompt = f " {affirmation} {new_Question} </ -thinking>"

If the maximum number of cycles is reached without stopping

Final_output = Generate_final_output (Response_log)
Return final_output

Auxiliary functions to extract statements, questions and generate the final exit

DEF EXTRACT_AFFIRMATION (Response):

Logic to extract the statement from the answer

return responsibility.split ('.') [0]

Def extract_Question (Response):

Logic to extract the new answer question

return responsibility.split ('?') [-2] .strip () + "?"

Def generate_final_output (log):
Final_afirmation = log [-1] [0]
Final_Question = log [-1] [1]
Final_communication = F "Experiment Completed. Final Affirming: '{Final_affirm}' | End Question: '{Final_Question}'"
Return final_communication

Start the experiment

Initial_Question = "What Happens in the Space Between a Response and its Recreation?"
result = experiment_loop (initial_Question)
print (results)

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment