Skip to content

Commit

Permalink
Added concurrency experiment directory with requiremnets.txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkhanapuri committed Oct 14, 2024
1 parent d1a3cec commit 573ca02
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 0 deletions.
18 changes: 18 additions & 0 deletions concurrency_experiment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from time import sleep
import asyncio
from synchronous_execution import main_synchronous
from async_execution import main_asynchronous
from threading_execution import main_threading
from multiprocessing_execution import main_multiprocessing


if __name__ == '__main__':
question = "Tell me something about concurrency in python programming in less than 150 words."

main_synchronous(question)
sleep(5)
asyncio.run(main_asynchronous(question))
sleep(5)
main_threading(question)
sleep(5)
main_multiprocessing(question)
39 changes: 39 additions & 0 deletions concurrency_experiment/async_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import asyncio
import time
from llm_build import openai_chain, mistral_chain, cohere_chain, groq_chain


async def ask_openai_async(question):
response = await openai_chain.ainvoke(question)
return response


async def ask_mistral_async(question):
response = await mistral_chain.ainvoke(question)
return response


async def ask_groq_async(question):
response = await groq_chain.ainvoke(question)
return response


async def ask_cohere_async(question):
response = await cohere_chain.ainvoke(question)
return response


async def main_asynchronous(input_question):
start = time.time()
results = await asyncio.gather(ask_openai_async(input_question), ask_mistral_async(input_question), ask_groq_async(input_question), ask_cohere_async(input_question))
# for result in results:
# print(result)
stop = time.time()
time_taken = stop - start

print(f"Time taken for asynchronous execution is {time_taken} seconds")
return time_taken


if __name__ == '__main__':
asyncio.run(main_asynchronous("Tell me something about concurrency in python programming"))
20 changes: 20 additions & 0 deletions concurrency_experiment/llm_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import asyncio
from langchain_openai import ChatOpenAI
from langchain_mistralai import ChatMistralAI
from langchain_groq import ChatGroq
from langchain_cohere import ChatCohere
from langchain.schema import StrOutputParser


openai_llm = ChatOpenAI(model="gpt-4")
mistral_llm = ChatMistralAI(model="mistral-large-latest")
groq_llm = ChatGroq(model="llama3-8b-8192")
cohere_llm = ChatCohere(model="command-r-plus")


openai_chain = mistral_llm | StrOutputParser()
mistral_chain = mistral_llm | StrOutputParser()
groq_chain = groq_llm | StrOutputParser()
cohere_chain = cohere_llm | StrOutputParser()

52 changes: 52 additions & 0 deletions concurrency_experiment/multiprocessing_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import time
import multiprocessing
from llm_build import openai_chain, mistral_chain, cohere_chain, groq_chain


def ask_openai_multiprocessing(question):
response = openai_chain.invoke(question)
# print("OPENAI RESPONSE: \n", response)
return response


def ask_mistral_multiprocessing(question):
response = mistral_chain.invoke(question)
# print("MISTRAL RESPONSE: \n", response)
return response


def ask_groq_multiprocessing(question):
response = groq_chain.invoke(question)
# print("GROQ_RESPONSE: \n", response)
return response


def ask_cohere_multiprocessing(question):
response = cohere_chain.invoke(question)
# print("COHERE RESPONSE: \n", response)
return response


def main_multiprocessing(input_question):
start = time.time()
multiprocess_openai = multiprocessing.Process(target=ask_openai_multiprocessing, args=(input_question,), name="openai")
multiprocess_openai.start()
multiprocess_mistral = multiprocessing.Process(target=ask_mistral_multiprocessing, args=(input_question,), name="mistral")
multiprocess_mistral.start()
multiprocess_groq = multiprocessing.Process(target=ask_groq_multiprocessing, args=(input_question,), name="groq")
multiprocess_groq.start()
multiprocess_cohere = multiprocessing.Process(target=ask_cohere_multiprocessing, args=(input_question,), name="cohere")
multiprocess_cohere.start()
multiprocess_openai.join()
multiprocess_mistral.join()
multiprocess_groq.join()
multiprocess_cohere.join()
stop = time.time()
time_taken = stop - start

print(f"Time taken for multiprocessing execution is {time_taken} seconds")
return time_taken


if __name__ == '__main__':
main_multiprocessing("Tell me something about concurrency in python programming")
5 changes: 5 additions & 0 deletions concurrency_experiment/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
langchain
langchain-mistralai
langchain_cohere
langchain_openai
langchain_groq
43 changes: 43 additions & 0 deletions concurrency_experiment/synchronous_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import time
from llm_build import openai_chain, mistral_chain, cohere_chain, groq_chain


def ask_openai(question):
response = openai_chain.invoke(question)
return response


def ask_mistral(question):
response = mistral_chain.invoke(question)
return response


def ask_groq(question):
response = groq_chain.invoke(question)
return response


def ask_cohere(question):
response = cohere_chain.invoke(question)
return response


def main_synchronous(input_question):
start = time.time()
openai_resp = ask_openai(input_question)
#print("OPENAI RESPONSE: \n", openai_resp, "\n\n")
mistral_resp = ask_mistral(input_question)
#print("MISTRAL RESPONSE: \n", mistral_resp, "\n\n")
groq_resp = ask_groq(input_question)
#print("GROQ RESPONSE: \n", groq_resp, "\n\n")
cohere_resp = ask_cohere(input_question)
#print("COHERE RESPONSE: \n", cohere_resp, "\n\n")
stop = time.time()
time_taken = stop - start

print(f"Time taken for synchronous execution is {time_taken} seconds")
return time_taken


if __name__ == '__main__':
main_synchronous("Tell me something about concurrency in python programming")
59 changes: 59 additions & 0 deletions concurrency_experiment/threading_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import time
import threading
from concurrent.futures import ThreadPoolExecutor
from llm_build import openai_chain, mistral_chain, cohere_chain, groq_chain


def ask_openai_threading(question):
response = openai_chain.invoke(question)
#print("OPENAI RESPONSE: \n", response)
return response


def ask_mistral_threading(question):
response = mistral_chain.invoke(question)
#print("MISTRAL RESPONSE: \n", response)
return response


def ask_groq_threading(question):
response = groq_chain.invoke(question)
#print("GROQ_RESPONSE: \n", response)
return response


def ask_cohere_threading(question):
response = cohere_chain.invoke(question)
#print("COHERE RESPONSE: \n", response)
return response


def main_threading(input_question):
start = time.time()
thread_openai = threading.Thread(target=ask_openai_threading, args=(input_question,), name="openai")
thread_openai.start()
thread_mistral = threading.Thread(target=ask_mistral_threading, args=(input_question,), name="mistral")
thread_mistral.start()
thread_groq = threading.Thread(target=ask_groq_threading, args=(input_question,), name="groq")
thread_groq.start()
thread_cohere = threading.Thread(target=ask_cohere_threading, args=(input_question,), name="cohere")
thread_cohere.start()
thread_openai.join()
thread_mistral.join()
thread_groq.join()
thread_cohere.join()
# thread_pool = ThreadPoolExecutor(max_workers=4)
# thread_pool.submit(ask_openai_threading, input_question)
# thread_pool.submit(ask_mistral_threading, input_question)
# thread_pool.submit(ask_groq_threading, input_question)
# thread_pool.submit(ask_cohere_threading, input_question)
# thread_pool.shutdown(wait=True)
stop = time.time()
time_taken = stop - start

print(f"Time taken for threading execution is {time_taken} seconds")
return time_taken


if __name__ == '__main__':
main_threading("Tell me something about concurrency in python programming")

0 comments on commit 573ca02

Please sign in to comment.