Skip to content

Commit 5876d3d

Browse files
committed
Merge branch 'master' of https://github.com/thomasm1/python_2018
2 parents df360fa + a0d3d62 commit 5876d3d

File tree

687 files changed

+9381
-203751
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

687 files changed

+9381
-203751
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.9-slim
2+
3+
WORKDIR /app
4+
5+
COPY simple_server.py .
6+
7+
CMD ["python", "simple_server.py"]
8+

PY_AGENTS/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.env
2+
.gitignore
3+
env
4+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM python:3.11-slim
2+
3+
WORKDIR /app
4+
5+
COPY . /app
6+
7+
RUN pip install --no-cache-dir -r requirements.txt
8+
9+
#COPY agents.py prompts.py ./
10+
11+
RUN mkdir /app/data
12+
13+
14+
EXPOSE 80
15+
#set the environmen variable
16+
#ENV PYTHONUNBUFFERED=1
17+
18+
CMD ["python", "agents.py"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
docker run -it -v "C:\Users\thoma\git\_python\PY_AGENTS:/app/data" mapl-agents
2+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Type,Indicators,Name,Instructions
2+
bookwork,studious,iWorm,read and teach
3+
taskrunner,heavy duty,ichabod,pick up and carry
4+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#prompts.py
2+
ANALYZER_SYSTEM_PROMPT = """You are an AI agent that analyzes the CSV provided by the user.
3+
The focus of your analysis should be on what the data is, how it is formatted, what each column stands for, and how new data should be interpreted."""
4+
5+
GENERATOR_SYSTEM_PROMPT = """You are an AI agent that generates new CSV rows based on analyssi results and sample data.
6+
Follow the exact formatting and don't outpu any extra text. You only output formatted data, never any other text."""
7+
8+
ANALZER_USER_PROMPT = """ Analyze the structure and patterns of this sample dataset:
9+
10+
{sampl_data}
11+
12+
Provide a concise summary of the following:
13+
1. formatting of the dataset, be crystal clear when describing the structure of the CSV
14+
2. what the dataset represets, what each column stands for
15+
3. how new data should look like, based on the patterns you've identified
16+
"""
17+
18+
GENERATOR_USER_PROMPT = """Generate {num_rows} new CSV rows based on this analysis and sample data:
19+
20+
21+
Analysis:
22+
{analysis_result}
23+
24+
Sample Data:
25+
{sample_data}
26+
27+
Use the exact same formatting as the orginal data. Output onlyh the generated code, no extra text.
28+
29+
DO NOT USE ANY TEXT BEFVOFE/AFTER THE DATA. JUST START BY OUTPUTTING THE NEW ROWS. NO EXTRA TEXT!!!!"""
30+
31+
32+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
anthropic
2+

PY_AGENTS/n8n-ai-starter-kit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 9c99ced2919dbce0764b8d42b6451526dab586d4

0 commit comments

Comments
 (0)