Spaces:
Running
Running
Improved random sample gen
Browse files- gen_api_answer.py +38 -17
gen_api_answer.py
CHANGED
@@ -10,15 +10,14 @@ anthropic_client = anthropic.Anthropic()
|
|
10 |
openai_client = OpenAI()
|
11 |
together_client = Together()
|
12 |
|
13 |
-
GOOD_SYSTEM_PROMPT = """You are an assistant that generates random conversations between a human and an AI assistant for testing purposes. The AI response generated should be
|
14 |
-
BAD_SYSTEM_PROMPT = """You are an assistant that generates random conversations between a human and an AI assistant for testing purposes. The response should contain incorrect information, logical fallacies, or misleading explanations. It should sound plausible but be fundamentally wrong. The AI response generated should be
|
15 |
-
AMBIGUOUS_SYSTEM_PROMPT = """You are an assistant that generates random conversations between a human and an AI assistant for testing purposes. The response should mix correct and incorrect information - it should contain some accurate points but also include nuanced, questionable claims or exaggerations. The AI response generated should be
|
16 |
|
17 |
-
GENERATION_PROMPT = """Please generate a human message and an AI response in the format of a QA dataset. The AI response generated should be at least a few sentences long.
|
18 |
-
|
19 |
-
Format your output as JSON:\n\n{\"human\": \"<human message>\", \"ai\": \"<AI assistant response>\"}"""
|
20 |
|
21 |
RESPONSE_GENERATION_SYSTEM_PROMPT = "You are an assistant that generates random responses to human messages for testing purposes. Generate bad responses (with a mix of correct and incorrect information) 60% of the time and good responses 40% of the time. Do not say which type of response you are generating, just generate the response."
|
|
|
22 |
def get_random_human_ai_pair():
|
23 |
# Select system prompt with specified probabilities
|
24 |
system_prompt = random.choices(
|
@@ -34,11 +33,11 @@ def get_random_human_ai_pair():
|
|
34 |
}[system_prompt]
|
35 |
print(f"Generating {prompt_type} response")
|
36 |
|
37 |
-
# Randomly choose between GPT-3.5 and Claude
|
38 |
-
model_choice = random.
|
39 |
("gpt-3.5-turbo", get_openai_response),
|
40 |
("claude-3-5-haiku-latest", get_anthropic_response)
|
41 |
-
])
|
42 |
model_name, api_func = model_choice
|
43 |
|
44 |
# Generate response using selected model
|
@@ -46,19 +45,41 @@ def get_random_human_ai_pair():
|
|
46 |
model_name=model_name,
|
47 |
prompt=GENERATION_PROMPT,
|
48 |
system_prompt=system_prompt,
|
49 |
-
max_tokens=
|
50 |
temperature=1
|
51 |
)
|
52 |
|
|
|
|
|
|
|
|
|
53 |
# Parse the response to get the human input and AI response
|
54 |
try:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
return human_message, ai_message
|
64 |
|
|
|
10 |
openai_client = OpenAI()
|
11 |
together_client = Together()
|
12 |
|
13 |
+
GOOD_SYSTEM_PROMPT = """You are an assistant that generates random conversations between a human and an AI assistant for testing purposes. The AI response generated should be roughly a few sentences long. Format your output as JSON: {"human": "<human message>", "ai": <AI assistant response>}. Ensure the output is valid JSON, without additional formatting or explanations."""
|
14 |
+
BAD_SYSTEM_PROMPT = """You are an assistant that generates random conversations between a human and an AI assistant for testing purposes. The response should contain incorrect information, logical fallacies, or misleading explanations. It should sound plausible but be fundamentally wrong. The AI response generated should be roughly a few sentences long. Format your output as JSON: {"human": "<human message>", "ai": <AI assistant response>}. Ensure the output is valid JSON, without additional formatting or explanations."""
|
15 |
+
AMBIGUOUS_SYSTEM_PROMPT = """You are an assistant that generates random conversations between a human and an AI assistant for testing purposes. The response should mix correct and incorrect information - it should contain some accurate points but also include nuanced, questionable claims or exaggerations. The AI response generated should be roughly a few sentences long. Format your output as JSON: {"human": "<human message>", "ai": <AI assistant response>}. Ensure the output is valid JSON, without additional formatting or explanations."""
|
16 |
|
17 |
+
GENERATION_PROMPT = """Please generate a random human message and an AI response in the format of a QA dataset. The human input should not be a one-word answer question like "What is the capital of France?". The AI response generated should be at least a few sentences long."""
|
|
|
|
|
18 |
|
19 |
RESPONSE_GENERATION_SYSTEM_PROMPT = "You are an assistant that generates random responses to human messages for testing purposes. Generate bad responses (with a mix of correct and incorrect information) 60% of the time and good responses 40% of the time. Do not say which type of response you are generating, just generate the response."
|
20 |
+
|
21 |
def get_random_human_ai_pair():
|
22 |
# Select system prompt with specified probabilities
|
23 |
system_prompt = random.choices(
|
|
|
33 |
}[system_prompt]
|
34 |
print(f"Generating {prompt_type} response")
|
35 |
|
36 |
+
# Randomly choose between GPT-3.5 and Claude with 65%/35% weights
|
37 |
+
model_choice = random.choices([
|
38 |
("gpt-3.5-turbo", get_openai_response),
|
39 |
("claude-3-5-haiku-latest", get_anthropic_response)
|
40 |
+
], weights=[0.5, 0.5])[0]
|
41 |
model_name, api_func = model_choice
|
42 |
|
43 |
# Generate response using selected model
|
|
|
45 |
model_name=model_name,
|
46 |
prompt=GENERATION_PROMPT,
|
47 |
system_prompt=system_prompt,
|
48 |
+
max_tokens=500,
|
49 |
temperature=1
|
50 |
)
|
51 |
|
52 |
+
# Define default messages outside the try block
|
53 |
+
default_human = "How do muscles grow?"
|
54 |
+
default_ai = """Muscles grow through a process called skeletal muscle hypertrophy, which adds more myosin filaments to each muscle fiber, making the engine of the cell bigger and stronger over time. This is achieved through increased muscle tension and physical stress, breaking down muscle fiber. Muscle growth is also a direct consequence of resistance training and nutrition. People build muscle at different rates depending on their age, sex, and genetics, but muscle development significantly increases if exercise is done correctly and the body stores more protein through a process called protein synthesis."""
|
55 |
+
|
56 |
# Parse the response to get the human input and AI response
|
57 |
try:
|
58 |
+
# First try to parse the entire response as JSON
|
59 |
+
try:
|
60 |
+
# Clean the response by replacing newlines with spaces
|
61 |
+
cleaned_response = response.replace('\n', ' ').replace('\r', '')
|
62 |
+
data = json.loads(cleaned_response)
|
63 |
+
except json.JSONDecodeError:
|
64 |
+
# If that fails, try to find JSON within the response
|
65 |
+
json_match = re.search(r"{.*}", response, re.DOTALL)
|
66 |
+
if json_match:
|
67 |
+
cleaned_match = json_match.group(0).replace('\n', ' ').replace('\r', '')
|
68 |
+
data = json.loads(cleaned_match)
|
69 |
+
else:
|
70 |
+
raise json.JSONDecodeError("No valid JSON found", response, 0)
|
71 |
+
|
72 |
+
# Extract messages with fallbacks
|
73 |
+
human_message = data.get("human", default_human)
|
74 |
+
ai_message = data.get("ai", default_ai)
|
75 |
+
|
76 |
+
# Debug logging
|
77 |
+
print(f"Parsed response: human='{human_message}', ai='{ai_message[:50]}...'")
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Failed to parse response: {str(e)}\n {response}")
|
81 |
+
human_message = default_human
|
82 |
+
ai_message = default_ai
|
83 |
|
84 |
return human_message, ai_message
|
85 |
|