forked from OpenDataBox/ST-Raptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_utils.py
More file actions
200 lines (158 loc) · 5.58 KB
/
api_utils.py
File metadata and controls
200 lines (158 loc) · 5.58 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
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
import base64
import os
import requests
from openai import OpenAI
from utils.constants import *
def generate_text_third_party(
prompt, key, url, model="gpt-4o-2024-11-20", max_tokens=512, temperature=1.0
):
payload = {
"model": model,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant skilled in handling tabular data.",
},
{"role": "user", "content": prompt},
],
"max_tokens": max_tokens,
"n": 1, # 生成一条回复
"stop": None,
"temperature": temperature,
}
headers = {
"Content-Type": "application/json",
"Authorization": key,
"content-type": "application/json",
}
response = requests.request("POST", url, json=payload, headers=headers)
print("Chat response:", response.json()["choices"][0]["message"]["content"].strip())
return response.json()["choices"][0]["message"]["content"].strip()
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def llm_generate(prompt, model=LLM_MODEL_TYPE, port=LLM_PORT):
# Set OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = f"http://localhost:{port}/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
chat_response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
)
# print("Chat response:", chat_response.choices[0].message.content)
return chat_response.choices[0].message.content
def vlm_generate(
prompt="Describe this image in one sentence.",
image="https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
model=VLM_MODEL_TYPE,
port=VLM_PORT,
):
"""_summary_
Args:
prompt (str, optional): _description_. Defaults to 'Describe this image in one sentence.'.
image (str, optional): url or image_path. Defaults to 'https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg'.
"""
if os.path.exists(image):
image = f"data:image/jpeg;base64,{encode_image(image)}"
# Set OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = f"http://localhost:{port}/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
chat_response = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": image}},
],
}
],
)
# print("Chat response:", chat_response.choices[0].message.content)
return chat_response.choices[0].message.content
def generate_deepseek(
prompt, key=API_KEY, url=API_URL, model=LLM_MODEL_TYPE, max_tokens=8192, temperature=1.0
):
client = OpenAI(api_key=key, base_url=url)
res = ""
cnt = 0
while cnt < 20:
try:
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant skilled in handling tabular data."},
{"role": "user", "content": prompt},
],
max_tokens=max_tokens,
temperature=temperature,
stream=False
)
res = response.choices[0].message.content
except Exception as e:
print(e)
print(f"Deepseek API 请求失败!尝试第{cnt}次!")
import traceback; traceback.print_exc()
import time; time.sleep(0.1)
cnt += 1
break
return res
def generate_deepseek_old(
prompt, key=API_KEY, url=API_URL, model=LLM_MODEL_TYPE, max_tokens=20480, temperature=1.0
):
# DeepSeek-R1:671B
# deepseek-v3:671b
payload = {
"model": model,
"messages": [
{
"role": "system",
"content": "You are a helpful assistant skilled in handling tabular data.",
},
{"role": "user", "content": prompt},
],
"max_tokens": max_tokens,
"n": 1,
"stop": None,
"temperature": temperature,
}
headers = {
"Content-Type": "application/json",
"Authorization": key,
}
res = ""
cnt = 0
while cnt < 100:
try:
response = requests.request("POST", url, json=payload, headers=headers)
# print("Chat response:", response.json()['choices'][0]['message']['content'].strip())
rj = response.json()
if 'message' in rj and "context length" in rj['message']:
print(rj)
print(prompt)
return "0"
res = response.json()["choices"][0]["message"]["content"].strip()
return res
except Exception as e:
print(e)
print(f"Deepseek API 请求失败!尝试第{cnt}次!")
import traceback; traceback.print_exc()
import time; time.sleep(0.1)
cnt += 1
return res
def main():
print(generate_deepseek("hello!",key=API_KEY, url=API_URL, model = 'qwen'))
if __name__ == "__main__":
main()