Spaces:
Sleeping
Sleeping
lukestanley
commited on
Commit
•
139217d
1
Parent(s):
550885e
Move prompt strings and types to own file, reorder code a bit
Browse files- chill.py +18 -42
- prompts.py → promptObjects.py +20 -0
- utils.py +9 -1
chill.py
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
import json
|
2 |
import time
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from prompts import (
|
6 |
improve_prompt,
|
7 |
critique_prompt,
|
8 |
faith_scorer_prompt,
|
9 |
spicy_scorer_prompt,
|
|
|
|
|
|
|
|
|
10 |
)
|
11 |
|
12 |
# This script uses the llama_cpp server to improve a text.
|
@@ -23,35 +26,22 @@ from prompts import (
|
|
23 |
original_text = """Stop chasing dreams instead. Life is not a Hollywood movie. Not everyone is going to get a famous billionaire. Adjust your expectations to reality, and stop thinking so highly of yourself, stop judging others. Assume the responsibility for the things that happen in your life. It is kind of annoying to read your text, it is always some external thing that "happened" to you, and it is always other people who are not up to your standards. At some moment you even declare with despair. And guess what? This is true and false at the same time, in a fundamental level most people are not remarkable, and you probably aren't too. But at the same time, nobody is the same, you have worth just by being, and other people have too. The impression I get is that you must be someone incredibly annoying to work with, and that your performance is not even nearly close to what you think it is, and that you really need to come down to earth. Stop looking outside, work on yourself instead. You'll never be satisfied just by changing jobs. Do therapy if you wish, become acquainted with stoicism, be a volunteer in some poor country, whatever, but do something to regain control of your life, to get some perspective, and to adjust your expectations to reality."""
|
24 |
# From elzbardico on https://news.ycombinator.com/item?id=36119858
|
25 |
|
26 |
-
# TODO: See README.md for the more plans.
|
27 |
-
# TODO: Segment the text into sentences
|
28 |
"""
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
"""
|
32 |
|
33 |
global suggestions
|
34 |
suggestions = []
|
35 |
|
36 |
start_time = time.time()
|
37 |
-
|
38 |
-
|
39 |
-
class ImprovedText(BaseModel):
|
40 |
-
text: str = Field(str, description="The improved text.")
|
41 |
-
|
42 |
-
|
43 |
-
class SpicyScore(BaseModel):
|
44 |
-
spicy_score: float = Field(float, description="The spiciness score of the text.")
|
45 |
-
|
46 |
-
|
47 |
-
class Critique(BaseModel):
|
48 |
-
critique: str = Field(str, description="The critique of the text.")
|
49 |
-
|
50 |
-
|
51 |
-
class FaithfulnessScore(BaseModel):
|
52 |
-
faithfulness_score: float = Field(
|
53 |
-
float, description="The faithfulness score of the text."
|
54 |
-
)
|
55 |
|
56 |
|
57 |
def improve_text():
|
@@ -86,12 +76,6 @@ def critique_text(last_edit):
|
|
86 |
return combined_resp
|
87 |
|
88 |
|
89 |
-
def calculate_overall_score(faithfulness, spiciness):
|
90 |
-
baseline_weight = 0.8
|
91 |
-
overall = faithfulness + (1 - baseline_weight) * spiciness * faithfulness
|
92 |
-
return overall
|
93 |
-
|
94 |
-
|
95 |
def should_stop(
|
96 |
iteration,
|
97 |
overall_score,
|
@@ -129,9 +113,6 @@ def print_iteration_result(iteration, overall_score, time_used):
|
|
129 |
print(json.dumps(suggestions, indent=2))
|
130 |
|
131 |
|
132 |
-
max_iterations = 20
|
133 |
-
|
134 |
-
|
135 |
for iteration in range(1, max_iterations + 1):
|
136 |
try:
|
137 |
if iteration % 2 == 1:
|
@@ -153,13 +134,8 @@ for iteration in range(1, max_iterations + 1):
|
|
153 |
print("ValueError:", e)
|
154 |
continue
|
155 |
|
|
|
156 |
"""
|
157 |
-
|
158 |
-
|
159 |
-
"critique": "The revised text effectively conveys the same message as the original but in a more constructive and diplomatic tone, maintaining the original's intention while promoting a more positive discussion.",
|
160 |
-
"faithfulness_score": 0.85,
|
161 |
-
"spicy_score": 0.25,
|
162 |
-
"overall_score": 0.89,
|
163 |
-
"edit": "Consider shifting your focus from chasing dreams to finding fulfillment in reality. Life isn't a Hollywood movie, and becoming a famous billionaire isn't a realistic goal for everyone. It might be helpful to recalibrate your expectations to better align with what's possible. Instead of judging others, try to understand them better. Take responsibility for the events in your life, rather than attributing them to external factors or other people. I understand that it can be frustrating when things don't go as planned, but keep in mind that most people, including yourself, are not inherently exceptional or unremarkable. However, everyone has unique worth that doesn't depend on their achievements or status. It's essential to recognize that you may come across as demanding to work with and that your self-perception might not match others' opinions of your performance. To gain a fresh perspective and adjust your expectations, you could explore personal growth opportunities such as therapy, practicing stoicism, volunteering in underserved communities, or any other activity that helps you develop self-awareness and emotional intelligence."
|
164 |
-
}
|
165 |
"""
|
|
|
1 |
import json
|
2 |
import time
|
3 |
+
from utils import calculate_overall_score, query_ai_prompt
|
4 |
+
from promptObjects import (
|
|
|
5 |
improve_prompt,
|
6 |
critique_prompt,
|
7 |
faith_scorer_prompt,
|
8 |
spicy_scorer_prompt,
|
9 |
+
ImprovedText,
|
10 |
+
Critique,
|
11 |
+
FaithfulnessScore,
|
12 |
+
SpicyScore,
|
13 |
)
|
14 |
|
15 |
# This script uses the llama_cpp server to improve a text.
|
|
|
26 |
original_text = """Stop chasing dreams instead. Life is not a Hollywood movie. Not everyone is going to get a famous billionaire. Adjust your expectations to reality, and stop thinking so highly of yourself, stop judging others. Assume the responsibility for the things that happen in your life. It is kind of annoying to read your text, it is always some external thing that "happened" to you, and it is always other people who are not up to your standards. At some moment you even declare with despair. And guess what? This is true and false at the same time, in a fundamental level most people are not remarkable, and you probably aren't too. But at the same time, nobody is the same, you have worth just by being, and other people have too. The impression I get is that you must be someone incredibly annoying to work with, and that your performance is not even nearly close to what you think it is, and that you really need to come down to earth. Stop looking outside, work on yourself instead. You'll never be satisfied just by changing jobs. Do therapy if you wish, become acquainted with stoicism, be a volunteer in some poor country, whatever, but do something to regain control of your life, to get some perspective, and to adjust your expectations to reality."""
|
27 |
# From elzbardico on https://news.ycombinator.com/item?id=36119858
|
28 |
|
|
|
|
|
29 |
"""
|
30 |
+
Outputs something like this:
|
31 |
+
{
|
32 |
+
"critique": "The revised text effectively conveys the same message as the original but in a more constructive and diplomatic tone, maintaining the original's intention while promoting a more positive discussion.",
|
33 |
+
"faithfulness_score": 0.85,
|
34 |
+
"spicy_score": 0.25,
|
35 |
+
"overall_score": 0.89,
|
36 |
+
"edit": "Consider shifting your focus from chasing dreams to finding fulfillment in reality. Life isn't a Hollywood movie, and becoming a famous billionaire isn't a realistic goal for everyone. It might be helpful to recalibrate your expectations to better align with what's possible. Instead of judging others, try to understand them better. Take responsibility for the events in your life, rather than attributing them to external factors or other people. I understand that it can be frustrating when things don't go as planned, but keep in mind that most people, including yourself, are not inherently exceptional or unremarkable. However, everyone has unique worth that doesn't depend on their achievements or status. It's essential to recognize that you may come across as demanding to work with and that your self-perception might not match others' opinions of your performance. To gain a fresh perspective and adjust your expectations, you could explore personal growth opportunities such as therapy, practicing stoicism, volunteering in underserved communities, or any other activity that helps you develop self-awareness and emotional intelligence."
|
37 |
+
}
|
38 |
"""
|
39 |
|
40 |
global suggestions
|
41 |
suggestions = []
|
42 |
|
43 |
start_time = time.time()
|
44 |
+
max_iterations = 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
def improve_text():
|
|
|
76 |
return combined_resp
|
77 |
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def should_stop(
|
80 |
iteration,
|
81 |
overall_score,
|
|
|
113 |
print(json.dumps(suggestions, indent=2))
|
114 |
|
115 |
|
|
|
|
|
|
|
116 |
for iteration in range(1, max_iterations + 1):
|
117 |
try:
|
118 |
if iteration % 2 == 1:
|
|
|
134 |
print("ValueError:", e)
|
135 |
continue
|
136 |
|
137 |
+
# TODO: Segment the text into sentences
|
138 |
"""
|
139 |
+
import pysbd
|
140 |
+
sentences = pysbd.Segmenter(language="en", clean=False).segment(paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
"""
|
prompts.py → promptObjects.py
RENAMED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
improve_prompt = """
|
2 |
Your task is to rephrase inflammatory text, so it is more calm and constructive, without changing the intended meaning.
|
3 |
The improved text should have a softened tone, avoiding judgemental and extreme words.
|
@@ -90,3 +92,21 @@ Output your response as valid JSON in this format, then stop:
|
|
90 |
}
|
91 |
Please score the text.
|
92 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel, Field
|
2 |
+
|
3 |
improve_prompt = """
|
4 |
Your task is to rephrase inflammatory text, so it is more calm and constructive, without changing the intended meaning.
|
5 |
The improved text should have a softened tone, avoiding judgemental and extreme words.
|
|
|
92 |
}
|
93 |
Please score the text.
|
94 |
"""
|
95 |
+
|
96 |
+
|
97 |
+
class ImprovedText(BaseModel):
|
98 |
+
text: str = Field(str, description="The improved text.")
|
99 |
+
|
100 |
+
|
101 |
+
class SpicyScore(BaseModel):
|
102 |
+
spicy_score: float = Field(float, description="The spiciness score of the text.")
|
103 |
+
|
104 |
+
|
105 |
+
class Critique(BaseModel):
|
106 |
+
critique: str = Field(str, description="The critique of the text.")
|
107 |
+
|
108 |
+
|
109 |
+
class FaithfulnessScore(BaseModel):
|
110 |
+
faithfulness_score: float = Field(
|
111 |
+
float, description="The faithfulness score of the text."
|
112 |
+
)
|
utils.py
CHANGED
@@ -4,7 +4,9 @@ import requests
|
|
4 |
|
5 |
from llama_cpp import (
|
6 |
json_schema_to_gbnf,
|
7 |
-
)
|
|
|
|
|
8 |
|
9 |
# The main interface is the HTTP server, not the library directly.
|
10 |
|
@@ -71,3 +73,9 @@ def query_ai_prompt(prompt, replacements, model_class):
|
|
71 |
# print('prompt')
|
72 |
# print(prompt)
|
73 |
return llm_streaming(prompt, model_class)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
from llama_cpp import (
|
6 |
json_schema_to_gbnf,
|
7 |
+
)
|
8 |
+
|
9 |
+
# Only used directly to convert the JSON schema to GBNF,
|
10 |
|
11 |
# The main interface is the HTTP server, not the library directly.
|
12 |
|
|
|
73 |
# print('prompt')
|
74 |
# print(prompt)
|
75 |
return llm_streaming(prompt, model_class)
|
76 |
+
|
77 |
+
|
78 |
+
def calculate_overall_score(faithfulness, spiciness):
|
79 |
+
baseline_weight = 0.8
|
80 |
+
overall = faithfulness + (1 - baseline_weight) * spiciness * faithfulness
|
81 |
+
return overall
|