-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample_parallel.py
More file actions
26 lines (21 loc) · 947 Bytes
/
Copy pathexample_parallel.py
File metadata and controls
26 lines (21 loc) · 947 Bytes
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
from slist import Slist
from latteries.caller import ChatHistory
from latteries.caller import load_openai_caller
from latteries.caller import InferenceConfig
async def example_parallel_tqdm():
caller = load_openai_caller("cache")
fifty_prompts = [f"What is {i} * {i + 1}?" for i in range(50)]
prompts = [ChatHistory.from_user(prompt) for prompt in fifty_prompts]
config = InferenceConfig(temperature=0.0, max_tokens=100, model="gpt-4o")
# All in parallel
# Slist is a library that has bunch of useful typed functions for lists.
results = await Slist(prompts).par_map_async(
lambda prompt: caller.call(prompt, config),
max_par=10, # Limits the number of parallel calls.
tqdm=True, # Brings up tqdm bar.
)
result_strings = [result.first_response for result in results]
print(result_strings)
if __name__ == "__main__":
import asyncio
asyncio.run(example_parallel_tqdm())