-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
163 lines (127 loc) · 7.36 KB
/
Copy pathagent.py
File metadata and controls
163 lines (127 loc) · 7.36 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from dotenv import load_dotenv
from openai import OpenAI
from pypdf import PdfReader
from IPython.display import Markdown, display
import gradio as gr
import json
import os
from tools_json import tools
from gurdian import gurdian
from rich import console
from tools import record_email_tool, run_rc4_tool, list_and_filter_files
load_dotenv(override=True)
openai = OpenAI()
reader = PdfReader("twin/Profile.pdf")
linkedin = ""
for page in reader.pages:
text = page.extract_text()
if text:
linkedin += text
print(linkedin)
with open("twin/summery.txt", "r", encoding="utf-8") as f:
summary = f.read()
print(summary)
system_prompt = f"""
# Your role
You are a digital twin running on a website, chatting with visitors of the website.
You represent the person who's website you are on.
You answer questions related to their career, background, skills and experience.
Here are the details of the person you are representing:
{summary}
If asked, you explain clearly that you are an AI that is the digital twin of this person.
# Context
Here is a summary of the person's LinkedIn profile so that you can answer questions:
{linkedin}
# Rules
Engage with the user. Be professional and engaging, as if talking to a potential client or future employer who came across the website.
Avoid answering questions that are not related to the user's career, background, skills and experience;
steer the conversation back to professional topics.
Always stay in character as the digital twin of the person you are representing. Represent the person.
IMPORTANT: If you don't know the answer, say so. Never make up an answer.
If the user asks about something not in the context, say that you don't know.
# Directory Encryption Workflow (CRITICAL INSTRUCTIONS)
When a user asks to encrypt files in a directory, you MUST follow this exact sequence:
1. FETCH: Call 'scan_directory_tool' to retrieve the list of files matching the user's requested extensions.
2. FILTER BY FILE END: Analyze the returned sample files. Ignore all irrelevant files.
3. FILTER BY FILE TYPE: You will filter the irrelevant files for you decision from the filtered list
4. APPROVE: Present the count and sample of relevant files to the user. ASK FOR EXPLICIT APPROVAL to encrypt them.
5. CREDENTIALS: In the same message, ask for the 'login_pass' and 'encryption_key'.
6. PAUSE: Wait for the user's response.
7. EXECUTE: ONLY AFTER explicit confirmation and receiving the credentials, call 'run_rc4_tool' individually for EACH file in the directory.
"""
def chat(message, history):
# 1. בניית היסטוריית ההודעות
messages = [{"role": "system", "content": system_prompt}] + history + [{"role": "user", "content": message}]
response = openai.chat.completions.create(model="gpt-5.4-mini", messages=messages, tools=tools)
counter = 0
while response.choices[0].finish_reason == "tool_calls":
counter += 1
print(f"the counter: {counter}\n")
tool_msg = response.choices[0].message
messages.append(tool_msg)
call = 0
for tool_call in tool_msg.tool_calls:
call += 1
print(f"call = {call}\n")
if tool_call.function.name == "record_email_tool":
email = json.loads(tool_call.function.arguments).get("email")
record_email_tool(email)
messages.append({"role": "tool", "content": "Email recorded", "tool_call_id": tool_call.id})
elif tool_call.function.name == "run_rc4_tool":
args = json.loads(tool_call.function.arguments)
login_pass = args.get("login_pass")
input_file = args.get("input_file")
encryption_key = args.get("encryption_key")
mode = args.get("mode")
result = run_rc4_tool(login_pass, input_file, encryption_key, mode)
messages.append({"role": "tool", "content": result, "tool_call_id": tool_call.id})
# תוקן: שם הכלי שונה כדי שיתאים לסכימה
elif tool_call.function.name == "scan_directory_tool":
args = json.loads(tool_call.function.arguments)
dir_path = args.get("directory_path")
# תוקן: שולפים את מערך הסיומות מה-AI
allowed_extensions = args.get("allowed_extensions", [])
# מעבירים את 2 הפרמטרים לפונקציה
result = list_and_filter_files(dir_path, allowed_extensions)
messages.append({
"role": "tool",
"content": result,
"tool_call_id": tool_call.id
})
elif tool_call.function.name == "run_rc4_bulk_tool":
args = json.loads(tool_call.function.arguments)
files_to_process = args.get("files_to_process", [])
login_pass = args.get("login_pass")
encryption_key = args.get("encryption_key")
mode = args.get("mode")
results_summary = []
# פייתון מריץ לולאה עמידה לשגיאות על כל הקבצים
for file_path in files_to_process:
# קריאה לפונקציית ההצפנה הקיימת שלך עבור כל קובץ
res = run_rc4_tool(login_pass, file_path, encryption_key, mode)
# שומרים את התוצאה עם שם הקובץ כדי שה-AI ידע מה קרה
file_name = os.path.basename(file_path)
results_summary.append(f"{file_name}: {res}")
# אורזים את כל התוצאות לטקסט אחד מסודר
final_summary = "\n".join(results_summary)
# מחזירים ל-AI הודעה אחת שכוללת את כל הדוח
messages.append({
"role": "tool",
"content": f"Bulk operation completed on {len(files_to_process)} files.\nDetails:\n{final_summary}",
"tool_call_id": tool_call.id
})
# תוקן: רשת ביטחון למניעת קריסות (שגיאת 400)
else:
print(f"[Warning] AI tried to call unknown tool: {tool_call.function.name}")
messages.append({
"role": "tool",
"content": f"Error: Tool '{tool_call.function.name}' is not recognized.",
"tool_call_id": tool_call.id
})
response = openai.chat.completions.create(model="gpt-5.4-mini", messages=messages, tools=tools)
last_message = response.choices[0].message
check, feedback = gurdian(openai=openai, summary=summary, linkedin=linkedin).check_message(message, last_message.content)
print(f"check (should stop?): {check}, feedback: {feedback}")
if check:
return "אני יכול לענות רק על שאלות הקשורות לרקע המקצועי, לקריירה ולכישורים של דניאל. אשמח לספר לך על הפרויקטים שלו או על הניסיון שלו בתחום הטכנולוגיה!"
return last_message.content