File size: 10,060 Bytes
9c24cef c4528c6 9c24cef 601609a b418157 601609a b418157 601609a b418157 601609a c4528c6 3d0e128 c4528c6 5aeb4e2 c4528c6 5aeb4e2 c4528c6 3d0e128 c4528c6 5aeb4e2 c4528c6 3d0e128 c4528c6 5aeb4e2 c4528c6 9c24cef c4528c6 9c24cef b418157 9c24cef c4528c6 9c24cef 9919e6f 9c24cef b418157 9c24cef c4528c6 9c24cef c4528c6 9c24cef c4528c6 9c24cef c4528c6 9e33f28 c4528c6 fbfcf03 c4528c6 9919e6f c4528c6 9919e6f c4528c6 b24410c c4528c6 d474752 2c0b608 9919e6f 2c0b608 d474752 2c0b608 9919e6f c4528c6 3588612 c4528c6 d474752 c4528c6 d474752 c4528c6 d474752 5aeb4e2 c4528c6 9919e6f c4528c6 2c0b608 9919e6f 9c24cef c4528c6 9c24cef 9919e6f c4528c6 |
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
import openai
from openai import OpenAI
import difflib
prompt_fix_text_gpt = """
Исправь ошибки в данном тексте. Жаргонизмы считаются ошибками, приемлим только официальный стиль.
Исправляй грамматические, пунктуационные и орфоргафические ошибки.
Логические, лексические, стилистические ошибки, а также ошибки форматирования (напрмер неправильные переносы строки) ошибками не считаются!!! Их исправлять не надо
Буква "е" вместо буквы "ё" не считается ошибкой.
Не исправляй ошибки форматрирования!!! В текст заранее удалены все ненужны переносы, это сделано осознанно!
Верни только текст, никаких комментариев от себя не оставляй.
### Пример 1:
input text:
Это все мое, чашки вилки и ложи
Твой ответ:
Это все мое: чашки, вилки и ложки
Теперь твоя очередь:
input text:
{}
"""
prompt_compare_get_comment = """
Ты должен писать комментарий об ошибке в тексте.
Тебе дан кусок текст, в котором есть одна или несколько ошибок и этот же кусок текста с исправленными ошибками.
Твоя задача - кратко описать все ошибки, которые есть в тексте.
Пиши кратко, не больше одного-двух предложений. Отвечай на русском языке.
### Пример 1:
original text:
"кросивую, сабаку"
corrected text:
"красивую собаку"
Твой ответ:
Орфографические ошибки: "кросивую" -> "красивую", "сабаку" -> "собаку", лишняя запятая после "красивую".
### Пример 2:
original:
"я решил пойти, в"
corrected text:
"я решил пойти в"
Твой ответ:
Опечатка: повтор пробела, лишняя запятая после слова "пойти".
Теперь твоя очередь:
original:
"{}"
corrected text:
"{}"
"""
prompt = """
Ты должен писать комментарии об ошибках в тексте.
Тебе дан кусок текст, в котором есть ошибка, контекст, в котором стоит это слово и сообщение об ошибке. Твоя задача - кратко описать суть ошибки и, если необходимо, исправить её.
Исправляй только ту ошибку, на которую указывает сообщение. Отвечай на русском языке.
### Пример 1:
Кусок текста:
"кросивую"
Сообщение об ошибке:
"Возможно найдена орфографическая ошибка."
Текст:
"...т! Сегодня я был в парке и встретил там кросивую собаку. Она повиляла хвостом и побежа..."
Твой ответ:
Орфографическая ошибка в слове "кросивую" - правильно "красивую".
### Пример 2:
Кусок текста:
" "
Сообщение об ошибке:
"Повтор пробела."
Текст:
"...ретил там кросивую собаку. Она повиляла хвостом и побежала к речке. Я решил что ..."
Твой ответ:
Обнаружен повтор пробела между словами.
Теперь твоя очередь:
Кусок текста:
"{}"
Сообщение об ошибке:
"{}"
Текст:
"{}"
"""
def get_gpt_response_openai(inp):
response = openai.ChatCompletion.create(
model="openai/gpt-4.1-mini",
messages=[
{"role": "user", "content": inp}
]
)
return response.choices[0].message['content']
def get_gpt_response_vsegpt(inp):
client = OpenAI(
api_key='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',
base_url='https://openrouter.ai/api/v1'
)
response = client.chat.completions.create(
model="openai/gpt-4.1-mini",
messages=[{"role": "user", "content": inp}]
).choices[0].message.content
return response
def get_gpt_response(inp, client_name):
if client_name == "openai":
return get_gpt_response_openai(inp)
elif client_name == "vsegpt":
return get_gpt_response_vsegpt(inp)
else:
raise ValueError(f"Unsupported client: {client_name}")
def find_corrected_positions(original, corrected):
matcher = difflib.SequenceMatcher(None, original, corrected)
changes = []
for opcode in matcher.get_opcodes():
tag, i1, i2, j1, j2 = opcode
if tag != 'equal':
changes.append({
'original': (i1, i2),
'corrected': (j1, j2),
'operation': tag
})
return changes
def get_piece_of_text_bounds(s, start, end):
if start != 0: start -= 1
while start != 0 and s[start] not in [' ', "\n", '\t']:
start -= 1
if s[start] in [' ', "\n", '\t']: start += 1
if end < len(s) - 1: end += 1
while end < len(s) - 1 and s[end] not in [' ', "\n", '\t']:
end += 1
if end == len(s) - 1: end += 1
return start, end
def add_comments_to_text(text, errors, add_errors=False):
errors = sorted(errors, key=lambda x: x['end'])
shift = 0
for i, error in enumerate(errors, 1):
error['start'] += shift
error['end'] += shift
inp = f"({i})"
if add_errors:
inp = inp[:-1] + ' - ' + error['message'] + ')'
text = text[:error['end']] + inp + text[error['end']:]
error["end"] += len(inp)
shift += len(inp)
return text.replace("\n", " ")
def check_text(text, tool, mode="chat_gpt", highlight_mode=False, add_errors=False):
if mode == "tool":
return check_text_with_tool(text, tool, add_errors=add_errors)
else:
if highlight_mode:
return check_text_chat_gpt_highlight_mode(text, add_errors=add_errors)
else:
return check_text_chat_gpt(text, add_errors=add_errors)
def check_text_chat_gpt(text, fixed_text=None, add_errors=False, *args, **kwargs):
if fixed_text is None:
fixed_text = get_gpt_response(prompt_fix_text_gpt.format(text), "vsegpt")
changes = find_corrected_positions(text, fixed_text)
errors = []
for change in changes:
start_orig, end_orig = get_piece_of_text_bounds(text, change['original'][0], change['original'][1])
start_corr, end_corr = get_piece_of_text_bounds(fixed_text, change['corrected'][0], change['corrected'][1])
inp = prompt_compare_get_comment.format(text[start_orig:end_orig], fixed_text[start_corr:end_corr])
errors.append({
'start': start_orig,
'end': end_orig,
'message': get_gpt_response(inp, client_name="vsegpt"),
})
text_with_comments = add_comments_to_text(text, errors, add_errors=add_errors)
return text_with_comments, errors
def check_text_chat_gpt_highlight_mode(text, fixed_text=None, add_errors=False, *args, **kwargs):
if fixed_text is None:
fixed_text = get_gpt_response(prompt_fix_text_gpt.format(text), "vsegpt")
changes = find_corrected_positions(text, fixed_text)
bounds_init = []
for change in changes:
start_orig, end_orig = get_piece_of_text_bounds(text, change['original'][0], change['original'][1])
start_corr, end_corr = get_piece_of_text_bounds(fixed_text, change['corrected'][0], change['corrected'][1])
bounds_init.append({"start_orig": start_orig,
"end_orig": end_orig,
"start_corr": start_corr,
"end_corr": end_corr})
bounds_init = sorted(bounds_init, key=lambda x: x["start_orig"])
bounds_result = [bounds_init[0]] if len(bounds_init) > 0 else []
for bound in bounds_init[1:]:
if bounds_result[-1]["end_orig"] >= bound["start_orig"]:
bounds_result[-1]["end_orig"] = max(bounds_result[-1]["end_orig"], bound["end_orig"])
bounds_result[-1]["end_corr"] = max(bounds_result[-1]["end_corr"], bound["end_corr"])
else:
bounds_result.append(bound.copy())
errors = []
for bound in bounds_result:
inp = prompt_compare_get_comment.format(text[bound["start_orig"]:bound["end_orig"]],
fixed_text[bound["start_corr"]:bound["end_corr"]])
errors.append({
'start': bound["start_orig"],
'end': bound["end_orig"],
'message': get_gpt_response(inp, client_name="vsegpt"),
})
text_with_comments = add_comments_to_text(text, errors, add_errors=add_errors)
return text_with_comments, errors
def check_text_with_tool(text, tool, add_errors=False):
matches = tool.check(text)
errors = []
for match in matches:
inp = prompt.format(text[match.offset:match.offset + match.errorLength],
match.message, match.context)
error_info = {
'start': match.offset,
'end': match.offset + match.errorLength,
'message': get_gpt_response(inp, client_name="vsegpt"),
}
errors.append(error_info)
text_with_comments = add_comments_to_text(text, errors, add_errors=add_errors)
return text_with_comments, errors
|