MCPcopy Create free account
hub / github.com/CoopReason/TESSY / call_vllm_api_async

Function call_vllm_api_async

run_tessy.py:51–102  ·  view source on GitHub ↗
(session: aiohttp.ClientSession, api_url: str, model_name: str, prompt: str, max_tokens: int, temperature: float = 0.7, top_p: float = 0.8, top_k: int = 20, min_p: float = 0.0, max_retries: int = 3)

Source from the content-addressed store, hash-verified

49 return f"{user_message_formatted}<|start|>assistant<|channel|>analysis<|message|><|end|><|start|>assistant<|channel|>final<|message|>"
50
51async def call_vllm_api_async(session: aiohttp.ClientSession, api_url: str, model_name: str, prompt: str, max_tokens: int, temperature: float = 0.7, top_p: float = 0.8, top_k: int = 20, min_p: float = 0.0, max_retries: int = 3):
52# , stop_sequences: list = None):
53 headers = {'Content-Type': 'application/json'}
54 # print('model_name:', model_name)
55 payload = {
56 "model": model_name,
57 "prompt": prompt, # prompt is complete now, including original prompt and generated content
58 "max_tokens": max_tokens,
59 "temperature": temperature,
60 "top_p": top_p,
61 "top_k": top_k,
62 "min_p": min_p,
63 "n": 1,
64 "stream": False,
65 }
66
67 # if stop_sequences:
68 # payload["stop"] = stop_sequences
69
70 for attempt in range(max_retries):
71 try:
72 async with session.post(api_url, headers=headers, json=payload, timeout=600) as response:
73 response.raise_for_status()
74 api_res = await response.json()
75 generated_text = api_res['choices'][0]['text']
76
77 # If return is empty, also treat as failure
78 if not generated_text or len(generated_text.strip()) == 0:
79 print(f"API returned empty (attempt {attempt + 1}/{max_retries})")
80 if attempt < max_retries - 1:
81 await asyncio.sleep(2 ** attempt) # Exponential backoff: 2s, 4s
82 continue
83 else:
84 return {"text": ""}
85
86 return {"text": generated_text}
87 except aiohttp.ClientError as e:
88 print(f"API call failed (attempt {attempt + 1}/{max_retries}): {e}")
89 if attempt < max_retries - 1:
90 await asyncio.sleep(2 ** attempt) # Exponential backoff: 2s, 4s
91 else:
92 print(f"Request URL: {api_url}, Payload: {payload}")
93 return {"text": ""}
94 except asyncio.TimeoutError:
95 print(f"API call timeout (attempt {attempt + 1}/{max_retries})")
96 if attempt < max_retries - 1:
97 await asyncio.sleep(2 ** attempt) # Exponential backoff: 2s, 4s
98 else:
99 print(f"Request URL: {api_url}, Payload: {payload}")
100 return {"text": ""}
101
102 return {"text": ""}
103
104def classify_next_token_decision(batch_cur_texts, batch_generated_texts, current_model_name,
105 classifier_tokenizer, classifier_model, max_length=512, classifier_batch_size=32):

Calls

no outgoing calls

Tested by

no test coverage detected