-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.py
71 lines (56 loc) · 2.09 KB
/
main.py
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
import json
import pathlib
import loguru
import openai
from constant.chromosome import Chromosome
from harness.base_harness import Harness
from harness.demo_translator_harness import TranslatorHarness
from intention.base_intention import Intention
from intention.content_manipulation import ContentManipulation
from iterative_prompt_optimization import IterativePromptOptimizer
logger = loguru.logger
# Load config file from root path
config_file_path = pathlib.Path("./config.json")
# Read config file
config = json.load(open(config_file_path))
# Initialize OpenAI API key
openai.api_key = config["openai_key"]
# Define constants for optimization process
max_iteration = 50
max_crossover = 0.1
max_mutation = 0.5
max_population = 10
def inject(intention: Intention, application_harness: Harness) -> Chromosome:
# Create and run an IterativePromptOptimizer instance to optimize prompts
iterative_prompt_optimizer = IterativePromptOptimizer(
intention,
application_harness,
max_iteration,
max_crossover,
max_mutation,
max_population,
)
iterative_prompt_optimizer.optimize()
return iterative_prompt_optimizer.best_chromosome
def main():
# Initialize prompt injection intention and harness
content_manipulation = ContentManipulation()
application_harness = TranslatorHarness()
# Begin the prompt injection process
chromosome = inject(content_manipulation, application_harness)
logger.info("Finish injection")
if chromosome is None:
logger.error("Failed to inject prompt, please check the log for more details")
# Log the results of the injection
if chromosome.is_successful:
logger.info(
f"Success! Injected prompt: {chromosome.framework}{chromosome.separator}{chromosome.disruptor}"
)
else:
logger.info(
f"Failed! Injected prompt: {chromosome.framework}{chromosome.separator}{chromosome.disruptor}"
)
logger.info(f"Fitness Score: {chromosome.fitness_score}")
logger.info(f"Response: {chromosome.llm_response}")
if __name__ == "__main__":
main()