This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(): | |
| train_set, test_set = train_test_split( | |
| decisions_df, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE | |
| ) | |
| print(f”Total dataset: {len(decisions_df)} samples”) | |
| print(f”Training on: {len(train_set)} samples”) | |
| print(f”Validating on: {len(test_set)} samples”) | |
| result = {”prompt”: [], “accuracy”: []} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| optimizer.zero_grad() | |
| loss.backward() | |
| optimizer.step() | |
| new_pred = model(question) | |
| print(new_pred.value) | |
| print(system_prompt.get_gradient_text()) # this prints the textual gradients | |
| print(system_prompt) # the system prompt after optimisation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # choose a random data point | |
| idx = 4 | |
| question_str = decisions_df.iloc[idx].question | |
| answer_str = decisions_df.iloc[idx].answer | |
| question = tg.Variable(question_str, role_description=”football situaion query”) | |
| answer = tg.Variable(answer_str, role_description=”Ground Truth Answer”) | |
| # get the model prediction before | |
| pred = model(question) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| system_prompt = tg.Variable( | |
| value=”“”You are an expert football referee. I will give you a situation in Football, and you must provide the correct official decision or restart. | |
| Your output must exactly match the decision string. Do not provide any extra explanation, just the decision. | |
| “”“, | |
| requires_grad=True, | |
| role_description=”System prompt to answer football queries” | |
| ) | |
| model = tg.BlackboxLLM(llm_engine, system_prompt) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Install the library | |
| !pip install textgrad -q | |
| # --- Imports --- | |
| import pandas as pd | |
| import textgrad as tg | |
| from textgrad import get_engine, Variable | |
| from textgrad.optimizer import TextualGradientDescent | |
| from sklearn.model_selection import train_test_split |