nhathuy07 commited on
Commit
be0f26f
·
verified ·
1 Parent(s): 754ea14

Experimental async requests

Browse files
Files changed (1) hide show
  1. main.py +41 -37
main.py CHANGED
@@ -26,6 +26,8 @@ import os
26
  import pytesseract
27
  import lang
28
 
 
 
29
  from secrets import SystemRandom
30
 
31
  from random import randint, sample
@@ -184,19 +186,20 @@ async def __query_ml_predict(qtype: QType, content: str, header: str, token_limi
184
  case QType.WH:
185
 
186
  # Make request to Awan LLM endpoint
187
- _r = requests.post(
188
- url="https://api.awanllm.com/v1/chat/completions",
189
- headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
190
- data=json.dumps({
191
- "model": "Meta-Llama-3-8B-Instruct",
192
- "messages": [
193
- {"role": "user", "content": prompt.gen_prompt_wh(content=content, header=header, num_qs=num_qs, lang=l)}
194
- ],
195
- "max_tokens": max(token_limit, 4096),
196
- "presence_penalty":0.3,
197
- "temperature":0.55
198
- })
199
- )
 
200
 
201
  print(time() - stopwatch)
202
  return {"content": _r.json()['choices'][0]['message']['content'], "style": QType.WH}
@@ -204,32 +207,33 @@ async def __query_ml_predict(qtype: QType, content: str, header: str, token_limi
204
  case QType.STMT:
205
 
206
  # Make request to Awan LLM endpoint
207
- _r = requests.post(
208
- url="https://api.awanllm.com/v1/chat/completions",
209
- headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
210
- data=json.dumps({
211
- "model": "Meta-Llama-3-8B-Instruct",
212
- "messages": [
213
- {"role": "user", "content": prompt.gen_prompt_statements(content=content, header=header, num_qs=num_qs, lang=l)}
214
- ],
215
- "max_tokens": max(token_limit, 4096),
216
-
217
- })
218
- )
 
219
 
220
  _r_content = _r.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
221
-
222
- _w = requests.post(
223
- url="https://api.awanllm.com/v1/chat/completions",
224
- headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
225
- data=json.dumps({
226
- "model": "Meta-Llama-3-8B-Instruct",
227
- "messages": [
228
- {"role": "user", "content": prompt.gen_prompt_statements_false(content=_r_content, lang=l)}
229
- ],
230
- "max_tokens": max(token_limit, 4096),
231
-
232
- })
233
  )
234
 
235
  _w_content = _w.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
 
26
  import pytesseract
27
  import lang
28
 
29
+ import httpx
30
+
31
  from secrets import SystemRandom
32
 
33
  from random import randint, sample
 
186
  case QType.WH:
187
 
188
  # Make request to Awan LLM endpoint
189
+ async with httpx.AsyncClient() as client:
190
+ _r = await client.post(
191
+ url="https://api.awanllm.com/v1/chat/completions",
192
+ headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
193
+ data=json.dumps({
194
+ "model": "Meta-Llama-3-8B-Instruct",
195
+ "messages": [
196
+ {"role": "user", "content": prompt.gen_prompt_wh(content=content, header=header, num_qs=num_qs, lang=l)}
197
+ ],
198
+ "max_tokens": max(token_limit, 4096),
199
+ "presence_penalty":0.3,
200
+ "temperature":0.55
201
+ })
202
+ )
203
 
204
  print(time() - stopwatch)
205
  return {"content": _r.json()['choices'][0]['message']['content'], "style": QType.WH}
 
207
  case QType.STMT:
208
 
209
  # Make request to Awan LLM endpoint
210
+ async with httpx.AsyncClient() as client:
211
+ _r = await client.post(
212
+ url="https://api.awanllm.com/v1/chat/completions",
213
+ headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
214
+ data=json.dumps({
215
+ "model": "Meta-Llama-3-8B-Instruct",
216
+ "messages": [
217
+ {"role": "user", "content": prompt.gen_prompt_statements(content=content, header=header, num_qs=num_qs, lang=l)}
218
+ ],
219
+ "max_tokens": max(token_limit, 4096),
220
+
221
+ })
222
+ )
223
 
224
  _r_content = _r.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
225
+ async with httpx.AsyncClient() as client:
226
+ _w = await client.post(
227
+ url="https://api.awanllm.com/v1/chat/completions",
228
+ headers={'Content-Type': 'application/json', 'Authorization': f'Bearer {LLM_API_KEY}'},
229
+ data=json.dumps({
230
+ "model": "Meta-Llama-3-8B-Instruct",
231
+ "messages": [
232
+ {"role": "user", "content": prompt.gen_prompt_statements_false(content=_r_content, lang=l)}
233
+ ],
234
+ "max_tokens": max(token_limit, 4096),
235
+
236
+ })
237
  )
238
 
239
  _w_content = _w.json()['choices'][0]['message']['content'].split('\n\n',1)[1]