|
| 1 | +# agent-mapl.py |
| 2 | + |
| 3 | +import os |
| 4 | +import csv |
| 5 | +import anthropic |
| 6 | +from prompts import * |
| 7 | + |
| 8 | +#KEY |
| 9 | +if not os.getenv("ANTHROPIC_API_KEY"): |
| 10 | + os.environ["ANTHROPIC_API_KEY"] = input("Please enter your Antrhopic API key: ") # prompt the user for anthro api key |
| 11 | + |
| 12 | +#CLIENT |
| 13 | +client = anthropic.Anthropic() |
| 14 | +sonnet = "claude-3-5-sonnet-20240620" # model name get from docs.anthropic.com/en/docs/about-claude/models |
| 15 | + |
| 16 | +# Function to read the CSV file form the user |
| 17 | +def read_csv(file_path): |
| 18 | + data = [] |
| 19 | + with open(file_path, "r", newline="") as csvfile: #open csv in readmode |
| 20 | + csv_reader = csv.reader(csvfile) # create csv reader obj |
| 21 | + for row in csv_reader: |
| 22 | + data.append(row) # add each row to the data list |
| 23 | + return data |
| 24 | + |
| 25 | +# Saving |
| 26 | +def save_to_csv(data, output_file, headers=None): |
| 27 | + mode = 'w' if headers else 'a' # set file mode: w write if headers are provide else a append |
| 28 | + with open(output_file, mode, newline="") as f: |
| 29 | + writer=csv.writer(f) # create a csv writer object |
| 30 | + if headers: |
| 31 | + writer.writerow(headers) # write the headers if provided |
| 32 | + for row in csv.reader(data.splitlines()): #sp;lie data stirng into rows |
| 33 | + writer.writerow(row) |
| 34 | + |
| 35 | + |
| 36 | +#my personal librarian agent |
| 37 | +def analyzer_agent(sample_data): |
| 38 | + message = client.messages.create( |
| 39 | + model=sonnet, |
| 40 | + max_tokens=400, #limit response to 400 tokens |
| 41 | + temperature=0.1, # set low temp for more focused, deterministic output |
| 42 | + system=ANALYZER_SYSTEM_PROMPT, # use the predefined system prompt for analyzer |
| 43 | + messages=[ |
| 44 | + { |
| 45 | + "role":"user", |
| 46 | + "content": ANALYZER_USER_PROMPT.format(sample_data=sample_data) |
| 47 | + # format user prompt with provided smpale data |
| 48 | + } |
| 49 | + ] |
| 50 | + ) |
| 51 | + return message.content[0].text # return text content of tfirst message |
| 52 | + |
| 53 | +# generator agent |
| 54 | +def generator_agent(analysis_result, sample_data, num_rows=30): |
| 55 | + message = client.messages.create( |
| 56 | + model=sonnet, |
| 57 | + max_tokens=1500, # allow for longer response |
| 58 | + temperature=1, # high temp for creative, divers output |
| 59 | + system=GENERATOR_SYSTEM_PROMPT, |
| 60 | + messages=[ |
| 61 | + { |
| 62 | + "role":"user", |
| 63 | + "content": GENERATOR_USER_PROMPT.format( |
| 64 | + num_rows=num_rows, |
| 65 | + analysis_result=analysis_result, |
| 66 | + sample_data=sample_data |
| 67 | + ) |
| 68 | + } |
| 69 | + ] |
| 70 | + ) |
| 71 | + return message.content[0].text |
| 72 | + |
| 73 | + |
| 74 | +#main fucntion to run analyzer and generator agents: CONTROL THE AGENTS! |
| 75 | + |
| 76 | +#get input |
| 77 | +file_path = input("\nPuh-lease enter the name of your CSV file: ") |
| 78 | +file_path = os.path.join('/app/data', file_path) |
| 79 | +desired_rows = int(input("n now Enter the number of rows you want in the new dataset: ")) |
| 80 | + |
| 81 | +#read data |
| 82 | +sample_data = read_csv(file_path) |
| 83 | +sample_data_str = "\n".join([",".join(row) for row in sample_data]) # convert 2d list to a single string |
| 84 | + |
| 85 | +print("\nLaunching team of Agents") |
| 86 | + |
| 87 | +analysis_result = analyzer_agent(sample_data_str) |
| 88 | +print("\n### Analyzer Agent output:###\n") |
| 89 | +print(analysis_result) |
| 90 | +print("\n=-============\n\nGenerating new data....") |
| 91 | + |
| 92 | +#output |
| 93 | +output_file = "/app/data/new_dataset.csv" |
| 94 | +headers = sample_data[0] |
| 95 | + |
| 96 | +#creat output with headers |
| 97 | +save_to_csv("", output_file, headers) |
| 98 | + |
| 99 | + |
| 100 | +batch_size = 30 #number of rows to genrate in each batch |
| 101 | +generated_rows = 0 # counter to keep track of rows generated |
| 102 | + |
| 103 | +# generate data in batches until rows reached |
| 104 | +while generated_rows < desired_rows: |
| 105 | + rows_to_generate = min(batch_size, desired_rows - generated rows) |
| 106 | + generated_data = generator_agent(analysis_result, sample_data_str, rows_to_generate) |
| 107 | + #append gen data to output file |
| 108 | + save_to_csv(generated_data, output_file) |
| 109 | + #update count of generated rows |
| 110 | + generated_rows += rows_to_generate |
| 111 | + #print progress update |
| 112 | + print(f"Generated {generated_rows} rows out of {desired_rows}") |
| 113 | + |
| 114 | +# inform process complete |
| 115 | +print(f"\nGenerated data has been saved to {output_file}") |
| 116 | + |
| 117 | +# THE END |
0 commit comments