forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
832 B
Python
36 lines
832 B
Python
import asyncio
|
|
|
|
from langchain.llms import OpenAI
|
|
|
|
|
|
def generate_serially():
|
|
llm = OpenAI(temperature=0)
|
|
for _ in range(10):
|
|
resp = llm.generate(["Hello, how are you?"])
|
|
# print(resp)
|
|
|
|
|
|
async def async_generate(llm):
|
|
resp = await llm.agenerate(["Hello, how are you?"])
|
|
# print(resp)
|
|
|
|
|
|
async def generate_concurrently():
|
|
llm = OpenAI(temperature=0)
|
|
tasks = [async_generate(llm) for _ in range(10)]
|
|
await asyncio.gather(*tasks)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import time
|
|
|
|
s = time.perf_counter()
|
|
asyncio.run(generate_concurrently())
|
|
elapsed = time.perf_counter() - s
|
|
print(f"Concurrent executed in {elapsed:0.2f} seconds.")
|
|
|
|
s = time.perf_counter()
|
|
generate_serially()
|
|
elapsed = time.perf_counter() - s
|
|
print(f"Serial executed in {elapsed:0.2f} seconds.")
|