Spaces:
Runtime error
Runtime error
lizhen
commited on
Commit
·
4bdd4c5
1
Parent(s):
5c19486
add llms asyncio
Browse files- llms_asyncio.py +33 -0
llms_asyncio.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import asyncio
|
3 |
+
|
4 |
+
from langchain.llms import OpenAI
|
5 |
+
|
6 |
+
def generate_serially():
|
7 |
+
llm = OpenAI(temperature=0.9)
|
8 |
+
for _ in range(10):
|
9 |
+
resp = llm.generate(["Hello, how are you?"])
|
10 |
+
print(resp.generations[0][0].text)
|
11 |
+
|
12 |
+
|
13 |
+
async def async_generate(llm):
|
14 |
+
resp = await llm.agenerate(["Hello, how are you?"])
|
15 |
+
print(resp.generations[0][0].text)
|
16 |
+
|
17 |
+
|
18 |
+
async def generate_concurrently():
|
19 |
+
llm = OpenAI(temperature=0.9)
|
20 |
+
tasks = [async_generate(llm) for _ in range(10)]
|
21 |
+
await asyncio.gather(*tasks)
|
22 |
+
|
23 |
+
|
24 |
+
s = time.perf_counter()
|
25 |
+
# If running this outside of Jupyter, use asyncio.run(generate_concurrently())
|
26 |
+
generate_concurrently()
|
27 |
+
elapsed = time.perf_counter() - s
|
28 |
+
print('\033[1m' + f"Concurrent executed in {elapsed:0.2f} seconds." + '\033[0m')
|
29 |
+
|
30 |
+
s = time.perf_counter()
|
31 |
+
generate_serially()
|
32 |
+
elapsed = time.perf_counter() - s
|
33 |
+
print('\033[1m' + f"Serial executed in {elapsed:0.2f} seconds." + '\033[0m')
|