-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
57 lines (52 loc) · 1.73 KB
/
utils.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
import requests
import json
import re
from os.path import exists, join
from config import *
def extract_xml_structures(input_string):
pattern = r'<(\w+)(?:>(.*?)</\1>|/>)' # Updated pattern to handle empty tags
matches = re.findall(pattern, input_string)
result = []
for match in matches:
tag, content = match
element = {
'tag': tag,
'content': content.strip()
}
result.append(element)
return result
def get_completion(messages, model, or_api_key, add_assistant_response=True):
#print('get response start', messages)
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {or_api_key}",
},
data=json.dumps({
"model": model, #"openai/gpt-3.5-turbo", # Optional'anthropic/claude-3-opus', #
"messages": messages
})
)
data = response.json()
#print('data', data)
assert len(data['choices']) == 1, 'Expected one choice'
response_text = data['choices'][0]['message']['content']
#print('res text', response_text)
#print('get reponse end')
if add_assistant_response:
messages.append({
"role": "assistant",
"content": response_text
})
return response_text
def diff_workdir_postdir_compare(dir1, dir2):
# using subprocess do diff -r dir1 dir2 and get string output
import subprocess
cmd = f'diff -r {dir1} {dir2}'
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
output_str = output.decode('utf-8')
if error is not None:
# exception
raise Exception(f'Error in diff command: {error}')
return output_str