-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
123 lines (104 loc) · 3.28 KB
/
utils.py
File metadata and controls
123 lines (104 loc) · 3.28 KB
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
import os
from openai import OpenAI, AzureOpenAI
from google import genai
from google.genai import types
import subprocess
from itertools import islice
def get_lines(file_path, start, end):
with open(file_path, "r", encoding="utf-8") as f:
return [line.strip() for line in islice(f, start - 1, end)]
def count_lines(file_path):
return int(subprocess.check_output(["wc", "-l", file_path]).split()[0])
def openai_generate(model_name, content, system_prompt):
client = OpenAI()
response = []
for part in content:
partial_response = client.chat.completions.create(
model=model_name,
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": system_prompt
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": part
}
]
}
],
response_format={
"type": "text"
},
)
response.append(partial_response.choices[0].message.content)
return response
def azure_generate(model_name, content, system_prompt):
client = AzureOpenAI(
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("OPENAI_API_VERSION"),
)
response = []
for part in content:
partial_response = client.chat.completions.create(
model=model_name,
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": system_prompt
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": part
}
]
}
],
stream=False
)
response.append(partial_response.choices[0].message.content)
return response
def gemini_generate(model_name, content, system_prompt):
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
response = []
for part in content:
contents = [
types.Content(
role="user",
parts=[
types.Part.from_text(text=f"""{part}"""),
],
),
]
generate_content_config = types.GenerateContentConfig(
response_mime_type="text/plain",
system_instruction=[
types.Part.from_text(text=f"""{system_prompt}"""),
],
)
partial_response = client.models.generate_content(
model=model_name,
contents=contents,
config=generate_content_config,
)
response.append(partial_response.text.strip())
return response