pgleeson commited on
Commit
9822412
·
1 Parent(s): c84aa71

More restructuring to use openworm_ai

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. ring.py +24 -69
requirements.txt CHANGED
@@ -16,4 +16,4 @@ langchain_anthropic
16
  langchain_cohere
17
  streamlit
18
  llama_index
19
- openworm-ai>=0.2.3
 
16
  langchain_cohere
17
  streamlit
18
  llama_index
19
+ openworm-ai>=0.2.4
ring.py CHANGED
@@ -3,47 +3,37 @@ import os
3
  from langchain.prompts import PromptTemplate
4
  from langchain_core.output_parsers import StrOutputParser
5
 
 
 
 
 
 
 
 
 
 
6
  from openworm_ai.utils.llms import LLM_GPT35
7
  from openworm_ai.utils.llms import LLM_GPT4
8
  from openworm_ai.utils.llms import LLM_GPT4o
 
 
 
 
 
9
 
10
- from openworm_ai.utils.llms import get_llm as get_llm_via_openworm_ai
11
 
 
12
 
 
13
 
14
  LLM_LLAMA2 = "LLAMA2"
15
  LLM_GEMINI = "gemini-1.5-pro"
16
- LLM_AI21 = "AI21"
17
  LLM_CLAUDE2 = "Claude2.1"
18
- LLM_COHERE = "Cohere"
19
-
20
- OPENAI_LLMS = [LLM_GPT35, LLM_GPT4, LLM_GPT4o]
21
 
22
- PREF_ORDER_LLMS = (
23
- LLM_GEMINI,
24
- LLM_LLAMA2,
25
- LLM_GPT35,
26
- LLM_GPT4,
27
- LLM_GPT4o,
28
- LLM_AI21,
29
- LLM_CLAUDE2,
30
- LLM_COHERE,
31
- )
32
 
 
 
33
 
34
- def requires_openai_key(llm_ver):
35
- return llm_ver in OPENAI_LLMS
36
-
37
-
38
- def get_openai_api_key():
39
- # if openai_api_key_sb == None or len(openai_api_key_sb)==0:
40
- openai_api_key = os.environ.get("OPENAI_API_KEY")
41
- if openai_api_key is None:
42
- openai_api_key = str(open("../oaik", "r").readline())
43
- # else:
44
- # openai_api_key = openai_api_key_sb
45
-
46
- return openai_api_key
47
 
48
 
49
  def get_llamaapi_key():
@@ -52,32 +42,15 @@ def get_llamaapi_key():
52
  return llamaapi_key
53
 
54
 
55
- def get_gemini_api_key():
56
- gemini_api_key = os.environ.get("GEMINIAPI_KEY")
57
-
58
- return gemini_api_key
59
-
60
-
61
- def get_ai21_api_key():
62
- ai21_api_key = os.environ.get["AI21_API_KEY"]
63
-
64
- return ai21_api_key
65
-
66
-
67
  def get_anthropic_key():
68
  anthropic_api_key = os.environ.get["ANTHROPIC_API_KEY"]
69
 
70
  return anthropic_api_key
71
 
72
 
73
- def get_cohere_key():
74
- cohere_api_key = os.environ.get["COHERE_API_KEY"]
75
-
76
- return cohere_api_key
77
-
78
-
79
  def get_llm(llm_ver, temperature):
80
- if llm_ver in [LLM_GPT35, LLM_GPT4, LLM_GPT4o]:
 
81
 
82
  llm = get_llm_via_openworm_ai(llm_ver, temperature)
83
 
@@ -99,43 +72,25 @@ def get_llm(llm_ver, temperature):
99
  llm = ChatLlamaAPI(client=llama)
100
 
101
  elif llm_ver == LLM_GEMINI:
 
 
102
  from langchain_google_genai import ChatGoogleGenerativeAI
103
-
104
  llm = ChatGoogleGenerativeAI(
105
- model=LLM_GEMINI, google_api_key=get_gemini_api_key()
106
  )
107
 
108
- elif llm_ver == LLM_AI21:
109
- from langchain_ai21 import AI21LLM
110
-
111
- llm = AI21LLM(model="j2-ultra")
112
-
113
  elif llm_ver == LLM_CLAUDE2:
114
  from langchain_anthropic import AnthropicLLM
115
 
116
  llm = AnthropicLLM(model="claude-2.1")
117
 
118
- elif llm_ver == LLM_COHERE:
119
- from langchain_cohere import ChatCohere
120
-
121
- llm = ChatCohere()
122
 
123
  return llm
124
 
125
 
126
- GENERAL_QUERY_PROMPT_TEMPLATE = """Answer the following question. Provide succinct, yet scientifically accurate
127
- answers. Question: {question}
128
-
129
- Answer: """
130
-
131
- GENERAL_QUERY_LIMITED_PROMPT_TEMPLATE = """You are a neuroscientist who is answering questions about the worm C. elegans. Provide succinct, yet scientifically accurate
132
- answers. If the question is not related to biology, physics or chemistry, then don't answer the question, but instead explain that you
133
- can currently only answer questions related to C. elegans. Question: {question}
134
-
135
- Answer: """
136
-
137
 
138
  def generate_response(input_text, llm_ver, temperature, only_celegans):
 
139
  template = (
140
  GENERAL_QUERY_LIMITED_PROMPT_TEMPLATE
141
  if only_celegans
 
3
  from langchain.prompts import PromptTemplate
4
  from langchain_core.output_parsers import StrOutputParser
5
 
6
+ from openworm_ai.utils.llms import GENERAL_QUERY_PROMPT_TEMPLATE
7
+ from openworm_ai.utils.llms import GENERAL_QUERY_LIMITED_PROMPT_TEMPLATE
8
+
9
+ from openworm_ai.utils.llms import get_llm as get_llm_via_openworm_ai
10
+ from openworm_ai.utils.llms import get_gemini_api_key
11
+ from openworm_ai.utils.llms import requires_openai_key
12
+ from openworm_ai.utils.llms import get_openai_api_key
13
+
14
+
15
  from openworm_ai.utils.llms import LLM_GPT35
16
  from openworm_ai.utils.llms import LLM_GPT4
17
  from openworm_ai.utils.llms import LLM_GPT4o
18
+ from openworm_ai.utils.llms import LLM_GEMINI_2F
19
+ from openworm_ai.utils.llms import LLM_OLLAMA_LLAMA32_1B
20
+ from openworm_ai.utils.llms import LLM_OLLAMA_MISTRAL
21
+ from openworm_ai.utils.llms import LLM_OLLAMA_OLMO2_7B
22
+ from openworm_ai.utils.llms import LLM_COHERE
23
 
 
24
 
25
+ OPENAI_LLMS = [LLM_GPT35, LLM_GPT4, LLM_GPT4o]
26
 
27
+ OPENWORM_AI_LLMS = OPENAI_LLMS + [LLM_GEMINI_2F, LLM_OLLAMA_LLAMA32_1B, LLM_OLLAMA_MISTRAL, LLM_OLLAMA_OLMO2_7B, LLM_COHERE]
28
 
29
  LLM_LLAMA2 = "LLAMA2"
30
  LLM_GEMINI = "gemini-1.5-pro"
 
31
  LLM_CLAUDE2 = "Claude2.1"
 
 
 
32
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ PREF_ORDER_LLMS = [LLM_GEMINI, LLM_LLAMA2] + OPENWORM_AI_LLMS + [LLM_CLAUDE2,
35
+ LLM_COHERE]
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  def get_llamaapi_key():
 
42
  return llamaapi_key
43
 
44
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def get_anthropic_key():
46
  anthropic_api_key = os.environ.get["ANTHROPIC_API_KEY"]
47
 
48
  return anthropic_api_key
49
 
50
 
 
 
 
 
 
 
51
  def get_llm(llm_ver, temperature):
52
+
53
+ if llm_ver in OPENWORM_AI_LLMS:
54
 
55
  llm = get_llm_via_openworm_ai(llm_ver, temperature)
56
 
 
72
  llm = ChatLlamaAPI(client=llama)
73
 
74
  elif llm_ver == LLM_GEMINI:
75
+ k = get_gemini_api_key()
76
+ print(('1111_%s'%k)[::-1])
77
  from langchain_google_genai import ChatGoogleGenerativeAI
 
78
  llm = ChatGoogleGenerativeAI(
79
+ model=LLM_GEMINI, google_api_key=k
80
  )
81
 
 
 
 
 
 
82
  elif llm_ver == LLM_CLAUDE2:
83
  from langchain_anthropic import AnthropicLLM
84
 
85
  llm = AnthropicLLM(model="claude-2.1")
86
 
 
 
 
 
87
 
88
  return llm
89
 
90
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  def generate_response(input_text, llm_ver, temperature, only_celegans):
93
+ print('---- Gen response to: [%s] for [%s]' % (input_text, llm_ver))
94
  template = (
95
  GENERAL_QUERY_LIMITED_PROMPT_TEMPLATE
96
  if only_celegans