Skip to content

Commit 1609c84

Browse files
authored
Merge pull request #20 from openworm/development
Consolidating LLM definitions, etc.
2 parents f92b172 + 8794d17 commit 1609c84

File tree

7 files changed

+70
-139
lines changed

7 files changed

+70
-139
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ venv/
1414
/openworm_ai/quiz/samples/Ollama_phi3_latest_10questions_celegans.json
1515
/store
1616
/openworm_ai/corpus/wormatlas/ignore
17+
/openworm_ai/quiz/samples/GPT4o_10questions_general.json
18+
/openworm_ai/quiz/samples/GPT4o_10questions_science.json

openworm_ai/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Version of the Python module.
2-
__version__ = "0.2.0"
2+
__version__ = "0.2.1"
33

44

55
def print_(msg, print_it=True):

openworm_ai/quiz/QuizMaster.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
from openworm_ai.quiz.QuizModel import MultipleChoiceQuiz, Question, Answer
22

3-
from openworm_ai.quiz.TemplatesCelegans import GENERATE_Q, TEXT_ANSWER_EXAMPLE
4-
# from openworm_ai.quiz.Templates import GENERATE_Q, TEXT_ANSWER_EXAMPLE
53

64
from openworm_ai.utils.llms import ask_question_get_response
75
from openworm_ai.utils.llms import get_llm_from_argv
86

97
import random
8+
from enum import Enum
109

1110
indexing = ["A", "B", "C", "D"]
1211

12+
QuizScope = Enum(
13+
"QuizScope", [("GeneralKnowledge", 1), ("Science", 2), ("CElegans", 3)]
14+
)
15+
16+
17+
def save_quiz(num_questions, num_answers, llm_ver, quiz_scope, temperature=0):
18+
suffix = None
19+
20+
if quiz_scope == QuizScope.GeneralKnowledge:
21+
from openworm_ai.quiz.Templates import GENERATE_Q, TEXT_ANSWER_EXAMPLE
22+
23+
suffix = "_general"
24+
elif quiz_scope == QuizScope.Science:
25+
from openworm_ai.quiz.TemplatesScience import GENERATE_Q, TEXT_ANSWER_EXAMPLE
26+
27+
suffix = "_science"
28+
elif quiz_scope == QuizScope.CElegans:
29+
from openworm_ai.quiz.TemplatesCelegans import GENERATE_Q, TEXT_ANSWER_EXAMPLE
30+
31+
suffix = "_celegans"
1332

14-
def save_quiz(num_questions, num_answers, llm_ver, temperature=0):
1533
question = (
1634
GENERATE_Q.replace("<QUESTION_NUMBER>", str(num_questions)).replace(
1735
"<ANSWER_NUMBER>", str(num_answers)
@@ -22,13 +40,14 @@ def save_quiz(num_questions, num_answers, llm_ver, temperature=0):
2240
response = ask_question_get_response(question, llm_ver, temperature)
2341

2442
quiz = MultipleChoiceQuiz(
25-
title="GPT4o_%iquestions_celegans" % num_questions,
43+
title="GPT4o_%iquestions%s" % (num_questions, suffix),
2644
source="Generated by %s, temperature: %s" % (llm_ver, temperature),
2745
)
2846

2947
last_question = None
3048

3149
indexing = ["1", "2", "3", "4"]
50+
3251
for line in response.split("\n"):
3352
if len(line.strip()) > 0:
3453
if "QUESTION" in line or line[-1] == "?":
@@ -51,8 +70,8 @@ def save_quiz(num_questions, num_answers, llm_ver, temperature=0):
5170
print(quiz.to_yaml())
5271

5372
quiz.to_json_file(
54-
"openworm_ai/quiz/samples/%s_%iquestions_celegans.json"
55-
% (llm_ver.replace(":", "_"), num_questions)
73+
"openworm_ai/quiz/samples/%s_%iquestions%s.json"
74+
% (llm_ver.replace(":", "_"), num_questions, suffix)
5675
)
5776

5877

@@ -140,6 +159,7 @@ def save_quiz(num_questions, num_answers, llm_ver, temperature=0):
140159
print(
141160
f"\n The LLM {llm_ver} got {total_correct} out of {total_qs} questions correct ({'%.2f %%' % (100 * total_correct / total_qs)})!\n"
142161
)
162+
143163
# make this into a method which returns a dictionary of all the "stats" that lists the llm, correct/incorrect answers
144164
# this can be used to plot comparison of variety of llms on general knowledge
145165
else:
@@ -148,4 +168,4 @@ def save_quiz(num_questions, num_answers, llm_ver, temperature=0):
148168
if a.isnumeric():
149169
num = int(a)
150170
print(f"Using LLM {llm_ver} for saving quiz with {num} questions")
151-
save_quiz(num, 4, llm_ver, temperature=0.2)
171+
save_quiz(num, 4, llm_ver, quiz_scope=QuizScope.CElegans, temperature=0.2)

openworm_ai/quiz/QuizMasterCorpus.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,4 @@ def save_quiz(num_questions=100, num_answers=4, llm_ver=LLM_GPT4o, temperature=0
196196
)
197197

198198
else:
199-
print(f"Debug: Using LLM {llm_ver} for saving quiz")
200199
save_quiz(100, 4, llm_ver, temperature=0.2)

openworm_ai/quiz/Templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import sys
5252

5353
question = (
54-
GENERATE_Q.replace("<QUESTION_NUMBER>", "5").replace("<ANSWER_NUMBER>", "4")
54+
GENERATE_Q.replace("<QUESTION_NUMBER>", "100").replace("<ANSWER_NUMBER>", "4")
5555
+ TEXT_ANSWER_EXAMPLE
5656
)
5757

openworm_ai/quiz/quiz_all.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def load_llms():
4848
# LLM_OLLAMA_FALCON2 - 'only an assistant with no acess to external resources',
4949
# LLM_OLLAMA_CODELLAMA - understands only a fraction of questions, doesnt understand prompts
5050
] # Defined constants
51-
print(f"Debug: Loaded LLMs -> {llms}")
5251
return llms
5352

5453

openworm_ai/utils/llms.py

Lines changed: 39 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,45 @@
44
from langchain.prompts import PromptTemplate
55
from langchain_core.output_parsers import StrOutputParser
66

7+
LLM_CMD_LINE_ARGS = {}
8+
79
LLM_GPT35 = "GPT3.5"
810
LLM_GPT4 = "GPT4"
911
LLM_GPT4o = "GPT4o"
12+
1013
LLM_LLAMA2 = "LLAMA2"
14+
LLM_CMD_LINE_ARGS["-l"] = LLM_LLAMA2
1115
LLM_GEMINI = "gemini-2.0-flash"
16+
LLM_CMD_LINE_ARGS["-g"] = LLM_GEMINI
1217
LLM_AI21 = "AI21"
18+
LLM_CMD_LINE_ARGS["-a"] = LLM_AI21
1319
LLM_CLAUDE37 = "claude-3-7-sonnet-20250219"
20+
LLM_CMD_LINE_ARGS["-c"] = LLM_CLAUDE37
1421
LLM_COHERE = "Cohere"
22+
LLM_CMD_LINE_ARGS["-co"] = LLM_COHERE
23+
1524
LLM_OLLAMA_LLAMA32 = "Ollama:llama3.2"
25+
LLM_CMD_LINE_ARGS["-o-l32"] = LLM_OLLAMA_LLAMA32
1626
LLM_OLLAMA_LLAMA32_1B = "Ollama:llama3.2:1b"
27+
LLM_CMD_LINE_ARGS["-o-l321b"] = LLM_OLLAMA_LLAMA32_1B
1728
LLM_OLLAMA_MISTRAL = "Ollama:mistral"
29+
LLM_CMD_LINE_ARGS["-o-m"] = LLM_OLLAMA_MISTRAL
1830
LLM_OLLAMA_TINYLLAMA = "Ollama:tinyllama"
19-
31+
LLM_CMD_LINE_ARGS["-o-t"] = LLM_OLLAMA_TINYLLAMA
2032
LLM_OLLAMA_PHI3 = "Ollama:phi3:latest"
33+
LLM_CMD_LINE_ARGS["-o-phi3"] = LLM_OLLAMA_PHI3
2134
LLM_OLLAMA_PHI4 = "Ollama:phi4:latest"
35+
LLM_CMD_LINE_ARGS["-o-phi4"] = LLM_OLLAMA_PHI4
36+
LLM_OLLAMA_GEMMA = "Ollama:gemma:7b"
37+
LLM_CMD_LINE_ARGS["-ge"] = LLM_OLLAMA_GEMMA
2238
LLM_OLLAMA_GEMMA2 = "Ollama:gemma2:latest"
39+
LLM_CMD_LINE_ARGS["-ge2"] = LLM_OLLAMA_GEMMA2
2340
LLM_OLLAMA_GEMMA3 = "Ollama:gemma3:4b"
41+
LLM_CMD_LINE_ARGS["-ge3"] = LLM_OLLAMA_GEMMA3
2442
LLM_OLLAMA_DEEPSEEK = "Ollama:deepseek-r1:7b"
25-
LLM_OLLAMA_GEMMA = "Ollama:gemma:7b"
43+
LLM_CMD_LINE_ARGS["-o-dsr1"] = LLM_OLLAMA_DEEPSEEK
2644
LLM_OLLAMA_QWEN = "Ollama:qwen:4b"
45+
LLM_CMD_LINE_ARGS["-qw"] = LLM_OLLAMA_QWEN
2746
LLM_OLLAMA_CODELLAMA = "Ollama:codellama:latest"
2847
LLM_OLLAMA_FALCON2 = "Ollama:falcon2:latest"
2948

@@ -99,17 +118,14 @@ def get_cohere_key():
99118

100119

101120
def get_llm(llm_ver, temperature):
102-
print(f"Debug: get_llm received {llm_ver}")
103121
if llm_ver == LLM_GPT35:
104-
print("Debug: Using GPT-3.5")
105122
from langchain_openai import OpenAI
106123

107124
return OpenAI(temperature=temperature, openai_api_key=get_openai_api_key())
108125

109126
elif llm_ver == LLM_GPT4:
110127
from langchain_openai import ChatOpenAI
111128

112-
print("Debug: Using GPT-4")
113129
return ChatOpenAI(
114130
model_name="gpt-4",
115131
openai_api_key=get_openai_api_key(),
@@ -118,7 +134,6 @@ def get_llm(llm_ver, temperature):
118134
elif llm_ver == LLM_GPT4o:
119135
from langchain_openai import ChatOpenAI
120136

121-
print("Debug: Using GPT-4o")
122137
return ChatOpenAI(
123138
model_name="gpt-4o",
124139
openai_api_key=get_openai_api_key(),
@@ -144,7 +159,6 @@ def get_llm(llm_ver, temperature):
144159
elif llm_ver == LLM_GEMINI:
145160
from langchain_google_genai import ChatGoogleGenerativeAI
146161

147-
print("Debug: Using Gemini Flash")
148162
return ChatGoogleGenerativeAI(
149163
model="gemini-2.0-flash",
150164
google_api_key=get_gemini_api_key(), # Retrieve API key
@@ -159,7 +173,6 @@ def get_llm(llm_ver, temperature):
159173
elif llm_ver == LLM_CLAUDE37:
160174
from langchain_anthropic import ChatAnthropic
161175

162-
print("Debug: Using Claude 3.7 Sonnet")
163176
return ChatAnthropic(
164177
model_name="claude-3-7-sonnet-20250219",
165178
anthropic_api_key=get_anthropic_key(), # Retrieve API key
@@ -171,75 +184,23 @@ def get_llm(llm_ver, temperature):
171184

172185
llm = ChatCohere()
173186

174-
elif llm_ver == LLM_OLLAMA_LLAMA32_1B:
175-
from langchain_ollama.llms import OllamaLLM
176-
177-
llm = OllamaLLM(model="llama3.2:1b")
178-
179-
elif llm_ver == LLM_OLLAMA_MISTRAL:
180-
from langchain_ollama.llms import OllamaLLM
181-
182-
llm = OllamaLLM(model="mistral")
183-
184-
elif llm_ver == LLM_OLLAMA_TINYLLAMA:
185-
from langchain_ollama.llms import OllamaLLM
186-
187-
llm = OllamaLLM(model="tinyllama")
188-
189-
elif llm_ver == LLM_OLLAMA_PHI3:
190-
from langchain_ollama.llms import OllamaLLM
191-
192-
print("Debug: Using Phi-3")
193-
llm = OllamaLLM(model="phi3:latest", temperature=temperature)
194-
195-
elif llm_ver == LLM_OLLAMA_PHI4:
196-
from langchain_ollama.llms import OllamaLLM
197-
198-
print("Debug: Using Phi-4")
199-
llm = OllamaLLM(model="phi4:latest", temperature=temperature)
200-
201-
elif llm_ver == LLM_OLLAMA_GEMMA2:
202-
from langchain_ollama.llms import OllamaLLM
203-
204-
print("Debug: Using Gemma-2")
205-
llm = OllamaLLM(model="gemma2:latest", temperature=temperature)
206-
207-
elif llm_ver == LLM_OLLAMA_GEMMA3:
208-
from langchain_ollama.llms import OllamaLLM
209-
210-
print("Debug: Using Gemma-3")
211-
llm = OllamaLLM(model="gemma3:4b", temperature=temperature)
212-
213-
elif llm_ver == LLM_OLLAMA_DEEPSEEK:
214-
from langchain_ollama.llms import OllamaLLM
215-
216-
print("Debug: Using DeepSeek")
217-
return OllamaLLM(model="deepseek-r1:7b", temperature=temperature)
218-
219-
elif llm_ver == LLM_OLLAMA_GEMMA:
220-
from langchain_ollama.llms import OllamaLLM
221-
222-
print("Debug: Using Gemma")
223-
return OllamaLLM(model="gemma:7b", temperature=temperature)
224-
225-
elif llm_ver == LLM_OLLAMA_QWEN:
226-
from langchain_ollama.llms import OllamaLLM
227-
228-
print("Debug: Using Qwen")
229-
return OllamaLLM(model="qwen:4b", temperature=temperature)
230-
231-
elif llm_ver == LLM_OLLAMA_CODELLAMA:
232-
from langchain_ollama.llms import OllamaLLM
233-
234-
print("Debug: Using CodeLlama")
235-
return OllamaLLM(model="codellama:latest", temperature=temperature)
236-
237-
elif llm_ver == LLM_OLLAMA_FALCON2:
187+
elif llm_ver in [
188+
LLM_OLLAMA_LLAMA32_1B,
189+
LLM_OLLAMA_MISTRAL,
190+
LLM_OLLAMA_TINYLLAMA,
191+
LLM_OLLAMA_PHI3,
192+
LLM_OLLAMA_PHI4,
193+
LLM_OLLAMA_GEMMA,
194+
LLM_OLLAMA_GEMMA2,
195+
LLM_OLLAMA_GEMMA3,
196+
LLM_OLLAMA_DEEPSEEK,
197+
LLM_OLLAMA_QWEN,
198+
LLM_OLLAMA_CODELLAMA,
199+
LLM_OLLAMA_FALCON2,
200+
]:
238201
from langchain_ollama.llms import OllamaLLM
239202

240-
print("Debug: Using Falcon2")
241-
return OllamaLLM(model="falcon2:latest", temperature=temperature)
242-
203+
llm = OllamaLLM(model=llm_ver.split(":", 1)[1])
243204
return llm
244205

245206

@@ -340,66 +301,16 @@ def generate_panel_response(input_text, llm_panelists, llm_panel_chair, temperat
340301
def get_llm_from_argv(argv):
341302
llm_ver = LLM_GPT4o
342303

343-
if "-g" in argv:
344-
llm_ver = LLM_GEMINI
345-
346-
if "-ge" in argv:
347-
llm_ver = LLM_OLLAMA_GEMMA
348-
349-
if "-ge2" in argv:
350-
llm_ver = LLM_OLLAMA_GEMMA2
351-
352-
if "-ge3" in argv:
353-
llm_ver = LLM_OLLAMA_GEMMA3
354-
355-
if "-qw" in argv:
356-
llm_ver = LLM_OLLAMA_QWEN
357-
358-
if "-l" in argv:
359-
llm_ver = LLM_LLAMA2
360-
361-
if "-a" in argv:
362-
llm_ver = LLM_AI21
363-
364-
if "-cl" in argv:
365-
llm_ver = LLM_CLAUDE37
366-
367-
if "-co" in argv:
368-
llm_ver = LLM_COHERE
369-
370-
if "-o-l32" in argv:
371-
llm_ver = LLM_OLLAMA_LLAMA32
372-
373-
if "-o-l321b" in argv:
374-
llm_ver = LLM_OLLAMA_LLAMA32_1B
375-
print(f"DEBUG: Selected LLM Version = {llm_ver}")
376-
return llm_ver
377-
378-
if "-o-m" in argv:
379-
llm_ver = LLM_OLLAMA_MISTRAL
380-
381-
if "-o-t" in argv:
382-
llm_ver = LLM_OLLAMA_TINYLLAMA
383-
384-
if "-o-phi3" in argv:
385-
llm_ver = LLM_OLLAMA_PHI3
386-
print(f"DEBUG: Selected LLM Version = {llm_ver}")
387-
388-
if "-o-phi4" in argv:
389-
llm_ver = LLM_OLLAMA_PHI4
390-
391-
if "-o-dsr1" in argv:
392-
llm_ver = LLM_OLLAMA_DEEPSEEK
393-
394-
print(f"Debug: get_llm_from_argv selected {llm_ver}")
304+
for arg in LLM_CMD_LINE_ARGS:
305+
if arg in argv:
306+
llm_ver = LLM_CMD_LINE_ARGS[arg]
395307

396308
return llm_ver
397309

398310

399311
def ask_question_get_response(
400312
question, llm_ver, temperature=0, only_celegans=False, print_question=True
401313
):
402-
print(f"Debug: ask_question_get_response received llm_ver={llm_ver}")
403314
print("--------------------------------------------------------")
404315
if print_question:
405316
print("Asking question:\n %s" % question)

0 commit comments

Comments
 (0)