Skip to content

Commit 49580cd

Browse files
committed
Improved Parallelization Example
1 parent 5509f5c commit 49580cd

1 file changed

Lines changed: 104 additions & 40 deletions

File tree

Lines changed: 104 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,132 @@
11
import os
2-
from openai import OpenAI
2+
from openai import OpenAI, AsyncOpenAI
33
from dotenv import load_dotenv
44
load_dotenv(override=True)
55
import time
6+
import asyncio
67

7-
GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"
8-
google_api_key = os.getenv("gemini")
9-
gemini_judge = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)
108

11-
messages = [{'role': 'user',
12-
'content': """"Create a difficult reasoning question for to judge LLMs and their outputs,
13-
keep question short, only ask question don't provide reason why you asked a question,
14-
Now respond this way do not include markdown format"""}]
159

16-
response = gemini_judge.chat.completions.create(model="gemma-3-27b-it", messages= messages)
10+
async def create_question():
1711

18-
question = f"""{response.choices[0].message.content}"""
12+
huihui_judge = AsyncOpenAI(base_url=os.getenv("huihui_base_url"), api_key="not_needed")
1913

14+
messages = [
15+
{
16+
"role": "user",
17+
"content": (
18+
"Create a difficult reasoning question to judge LLMs and their outputs, "
19+
"keep question short, only ask question don't provide reason why you asked a question, "
20+
"Now respond this way do not include markdown format"
21+
"Do not repeat Question"
22+
)
23+
}
24+
]
2025

21-
model_to_use = ["gemma-3-12b-it", "gemini-2.0-flash-thinking-exp-1219",
22-
"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21"]
26+
response = await huihui_judge.chat.completions.create(model= os.getenv("current_huihui_model"),
27+
messages= messages, stream= False)
28+
print("Created Question...")
2329

24-
LLM1 = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)
30+
return f"""{response.choices[0].message.content}"""
31+
# return "For beginners is C better or Rust?" # Sample Question
2532

26-
LLM2 = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)
2733

28-
LLM3 = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)
34+
question = asyncio.run(create_question())
2935

30-
LLM4 = OpenAI(base_url=GEMINI_BASE_URL, api_key=google_api_key)
3136

32-
messages = [{'role': 'user',
33-
'content': f"""Answer this question: {question}"""}]
37+
async def answer_question():
3438

35-
LLM1_response = LLM1.chat.completions.create(model= model_to_use[0], messages= messages)
39+
mistral = AsyncOpenAI(base_url= os.getenv("mistral_base_url"),
40+
api_key= os.getenv("mistral"))
3641

37-
time.sleep(5)
42+
huihui_llama_local = AsyncOpenAI(base_url=os.getenv("huihui_base_url"), api_key="not_needed")
3843

39-
LLM2_response = LLM2.chat.completions.create(model= model_to_use[1], messages= messages)
44+
gemini = AsyncOpenAI(base_url=os.getenv("gemini_base_url"), api_key= os.getenv("gemini"))
4045

41-
time.sleep(5)
46+
messages_huihui = [ # this is a local model and hallucinates a lot
47+
# this is why i had to add specific instructions to it
48+
{
49+
"role": "user",
50+
"content": (f"""Answer this question to best of knowledge: {question},
51+
Now respond this way do not include markdown format,
52+
Do not keep repeating yourself in answer""")
53+
}
54+
]
4255

43-
LLM3_response = LLM3.chat.completions.create(model= model_to_use[2], messages= messages)
56+
messages = [
57+
{
58+
"role": "user",
59+
"content": f"Answer this question to best of knowledge: {question}"
60+
}
61+
]
62+
63+
mistral_model_name = os.getenv("mistral_model_name")
64+
huihui_model_name = os.getenv("current_huihui_model")
65+
gemini_model_name = os.getenv("gemini-2.5-flash")
4466

45-
time.sleep(5)
67+
print("Answering Question Parallely...")
4668

47-
LLM4_response = LLM4.chat.completions.create(model= model_to_use[3], messages= messages)
69+
mistral_response = await mistral.chat.completions.create(model= mistral_model_name,
70+
messages=messages,
71+
stream=False)
4872

49-
time.sleep(5)
73+
huihui_response = await huihui_llama_local.chat.completions.create(model= huihui_model_name,
74+
messages=messages_huihui,
75+
stream=False)
5076

51-
llm_responses = {model_to_use[0]: LLM1_response.choices[0].message.content,
52-
model_to_use[1]: LLM2_response.choices[0].message.content,
53-
model_to_use[2]: LLM3_response.choices[0].message.content,
54-
model_to_use[3]: LLM4_response.choices[0].message.content}
77+
gemini_response = await gemini.chat.completions.create(model=gemini_model_name,
78+
messages=messages,
79+
stream=False)
5580

56-
messages = [{'role': 'user',
57-
'content': f"""Which LLM answered this question that you asked best?, this is the question: {question},
58-
{model_to_use[0]} answered this: {llm_responses[model_to_use[0]]},
59-
{model_to_use[1]} answered this: {llm_responses[model_to_use[1]]},
60-
{model_to_use[2]} answered this: {llm_responses[model_to_use[2]]},
61-
{model_to_use[3]} answered this: {llm_responses[model_to_use[3]]}
62-
"""}]
81+
print("Local Models do take time though...")
6382

64-
time.sleep(5)
83+
return {mistral_model_name: mistral_response.choices[0].message.content,
84+
huihui_model_name: huihui_response.choices[0].message.content,
85+
gemini_model_name: gemini_response.choices[0].message.content}
6586

66-
decision_response = gemini_judge.chat.completions.create(model="gemma-3-27b-it", messages= messages)
87+
answer = asyncio.run(answer_question())
6788

68-
print(decision_response.choices[0].message.content)
89+
time.sleep(5) # added because of gemini, so we delay multiple requests to gemini
90+
91+
async def judge_question():
92+
gemini_judge = AsyncOpenAI(base_url=os.getenv("gemini_base_url"), api_key=os.getenv("gemini"))
93+
94+
print(f"Models used: {answer.keys()}")
95+
96+
messages = [
97+
{
98+
"role": "user",
99+
"content": ( f"""Judge which LLM answered this question: {question} best?
100+
{answer}""")
101+
}
102+
]
103+
104+
print("Judging Question...")
105+
106+
response = await gemini_judge.chat.completions.create(model=os.getenv("gemma-3n-e4b-it"),
107+
messages=messages, stream=False)
108+
109+
return f"""{response.choices[0].message.content}"""
110+
111+
112+
print(asyncio.run(judge_question()))
113+
114+
"""
115+
Sample Output:
116+
Created Question...
117+
Answering Question Parallely...
118+
Local Models do take time though...
119+
Models used: dict_keys(['ministral-8b-2410', 'huihuillama3.1-8k', 'gemini-2.5-flash'])
120+
Judging Question...
121+
The LLM that answered the question is **gemini-2.5-flash**.
122+
123+
Here's why:
124+
125+
* **Strong Emphasis on Rust for Beginners:** Gemini-2.5-flash clearly states that "For **most beginners**, Rust is a better choice than C..." and provides detailed reasons why, focusing on memory safety, tooling (Cargo), and error messages. This aligns with the current trend and the benefits Rust offers to newcomers.
126+
* **Detailed Comparison:** The response provides a thorough and well-organized comparison of C and Rust, outlining pros and cons for beginners in a structured manner.
127+
* **Addresses the "Best" Language:** Gemini-2.5-flash explicitly states Rust is better for most beginners and provides specific scenarios where C might be considered, effectively addressing the "best" aspect of the question.
128+
* **Modern Language Focus:** The language used and the points made (like modern features in Rust) reflect a contemporary understanding of introductory programming education.
129+
* **Structure and Formatting:** The use of headings, bullet points, and bold text makes the information easy to read and digest, a characteristic of Gemini's responses.
130+
131+
While the other LLM (`ministral-8b-2410`) provides a reasonable comparison, it leans more towards a more traditional view of starting with C for foundational knowledge. Gemini-2.5-flash's perspective is more aligned with the current recommendation for beginners in systems programming.
132+
"""

0 commit comments

Comments
 (0)