Spaces:
Running
Running
File size: 5,393 Bytes
25dc001 543cb3a a832cb6 25dc001 e31a853 25dc001 b9c182b 83083b0 543cb3a b9c182b 25dc001 cdf7ed2 25dc001 07d335e 25dc001 b9c182b 25dc001 b9c182b 25dc001 b9c182b 25dc001 b9c182b 83083b0 543cb3a 83083b0 07d335e 25dc001 b9c182b 25dc001 b9c182b 25dc001 b9c182b 25dc001 b9c182b 25dc001 83083b0 cdf7ed2 07d335e b9c182b 07d335e b9c182b 07d335e b9c182b 07d335e e31a853 25dc001 e31a853 07d335e b9c182b 07d335e b9c182b 07d335e e31a853 07d335e b9c182b 07d335e b9c182b 07d335e b9c182b 07d335e b9c182b 07d335e e31a853 07d335e b9c182b 07d335e b9c182b 07d335e b9c182b 07d335e b9c182b 07d335e b9c182b 07d335e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import os
from getpass import getpass
from langchain_ai21 import AI21LLM
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
LLM_GPT35 = "GPT3.5"
LLM_GPT4 = "GPT4"
LLM_GPT4o = "GPT4o"
LLM_LLAMA2 = "LLAMA2"
LLM_GEMINI = "Gemini"
LLM_AI21 = "AI21"
OPENAI_LLMS = [LLM_GPT35, LLM_GPT4, LLM_GPT4o]
PREF_ORDER_LLMS = (LLM_GEMINI, LLM_LLAMA2, LLM_GPT35, LLM_GPT4, LLM_GPT4o, LLM_AI21)
def requires_openai_key(llm_ver):
return llm_ver in OPENAI_LLMS
def get_openai_api_key():
# if openai_api_key_sb == None or len(openai_api_key_sb)==0:
openai_api_key = os.environ.get("OPENAI_API_KEY")
if openai_api_key == None:
openai_api_key = str(open("../oaik", "r").readline())
# else:
# openai_api_key = openai_api_key_sb
return openai_api_key
def get_llamaapi_key():
llamaapi_key = os.environ.get("LLAMAAPI_KEY")
return llamaapi_key
def get_gemini_api_key():
gemini_api_key = os.environ.get("GEMINIAPI_KEY")
return gemini_api_key
def get_ai21_api_key():
ai21_api_key = os.environ.get["AI21_API_KEY"]
return ai21_api_key
def get_llm(llm_ver, temperature):
if llm_ver == LLM_GPT35:
llm = OpenAI(temperature=temperature, openai_api_key=get_openai_api_key())
elif llm_ver == LLM_GPT4:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model_name="gpt-4",
openai_api_key=get_openai_api_key(),
temperature=temperature,
)
elif llm_ver == LLM_GPT4o:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model_name="gpt-4o",
openai_api_key=get_openai_api_key(),
temperature=temperature,
)
elif llm_ver == LLM_LLAMA2:
from llamaapi import LlamaAPI
import asyncio
# Create a new event loop
loop = asyncio.new_event_loop()
# Set the event loop as the current event loop
asyncio.set_event_loop(loop)
llama = LlamaAPI(get_llamaapi_key())
from langchain_experimental.llms import ChatLlamaAPI
llm = ChatLlamaAPI(client=llama)
elif llm_ver == LLM_GEMINI:
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="gemini-pro", google_api_key=get_gemini_api_key()
)
elif llm_ver == LLM_AI21:
from langchain_ai21 import AI21LLM
llm = AI21LLM(
model="j2-ultra"
)
return llm
GENERAL_QUERY_PROMPT_TEMPLATE = """Answer the following question. Provide succinct, yet scientifically accurate
answers. Question: {question}
Answer: """
GENERAL_QUERY_LIMITED_PROMPT_TEMPLATE = """You are a neuroscientist who is answering questions about the worm C. elegans. Provide succinct, yet scientifically accurate
answers. If the question is not related to biology, physics or chemistry, then don't answer the question, but instead explain that you
can currently only answer questions related to C. elegans. Question: {question}
Answer: """
def generate_response(input_text, llm_ver, temperature, only_celegans):
template = (
GENERAL_QUERY_LIMITED_PROMPT_TEMPLATE
if only_celegans
else GENERAL_QUERY_PROMPT_TEMPLATE
)
prompt = PromptTemplate(template=template, input_variables=["question"])
llm = get_llm(llm_ver, temperature)
llm_chain = prompt | llm | StrOutputParser()
response = llm_chain.invoke(input_text)
return response
def generate_panel_response(input_text, llm_panelists, llm_panel_chair, temperature):
responses = {}
for llm_ver in llm_panelists:
prompt = PromptTemplate(
template=GENERAL_QUERY_PROMPT_TEMPLATE, input_variables=["question"]
)
llm = get_llm(llm_ver, temperature)
llm_chain = prompt | llm | StrOutputParser()
responses[llm_ver] = llm_chain.invoke(input_text)
panel_chair_prompt = """You are a neuroscientist chairing a panel discussion on the nematode C. elegans. A researcher has asked the following question:
{question}
and %i experts on the panel have give their answers.
""" % (
len(llm_panelists)
)
for llm_ver in llm_panelists:
panel_chair_prompt += """
The panelist named Dr. %s has provided the answer: %s
""" % (
llm_ver,
responses[llm_ver],
)
panel_chair_prompt += (
"""
Please generate a brief answer to the researcher's question based on their responses, pointing out where there is any inconsistency"""
+ """ in their answers, and using your own knowledge of C. elegans to try to resolve it."""
)
print(panel_chair_prompt)
prompt = PromptTemplate(template=panel_chair_prompt, input_variables=["question"])
llm = get_llm(llm_panel_chair, temperature)
llm_chain = prompt | llm | StrOutputParser()
response_chair = llm_chain.invoke(input_text)
response = """**%s**: %s""" % (llm_panel_chair, response_chair)
response += """
-----------------------------------
_Individual responses:_
"""
for llm_ver in responses:
response += """
_**%s**:_ _%s_
""" % (
llm_ver,
responses[llm_ver].strip().replace("\n", " "),
)
return response
|