Spaces:
Running
Running
Restructure
Browse files- .gitignore +1 -0
- app.py +14 -81
- ring.py +83 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/__pycache__
|
app.py
CHANGED
|
@@ -1,103 +1,36 @@
|
|
| 1 |
import streamlit
|
| 2 |
import streamlit as st
|
| 3 |
-
from langchain_community.llms import OpenAI
|
| 4 |
|
| 5 |
-
from
|
| 6 |
-
from
|
|
|
|
| 7 |
|
| 8 |
-
import
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
__version__ = '0.1.3'
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
LLM_LLAMA2 = 'LLAMA2'
|
| 15 |
|
| 16 |
st.title("Project Sydney")
|
| 17 |
st.text("OpenWorm LLM v%s - work in progress!"%__version__)
|
| 18 |
|
| 19 |
openai_api_key = None
|
| 20 |
|
| 21 |
-
with st.sidebar:
|
| 22 |
-
openai_api_key_sb = st.text_input("OpenAI API Key", type="password")
|
| 23 |
-
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def get_openai_api_key():
|
| 27 |
-
|
| 28 |
-
if openai_api_key_sb == None or len(openai_api_key_sb)==0:
|
| 29 |
-
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
| 30 |
-
if openai_api_key==None:
|
| 31 |
-
openai_api_key = str(open('../oaik','r').readline())
|
| 32 |
-
else:
|
| 33 |
-
openai_api_key = openai_api_key_sb
|
| 34 |
-
|
| 35 |
-
return openai_api_key
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def get_llamaapi_key():
|
| 39 |
-
|
| 40 |
-
llamaapi_key = os.environ.get('LLAMAAPI_KEY')
|
| 41 |
-
|
| 42 |
-
return llamaapi_key
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def generate_response(input_text, llm_ver, temperature):
|
| 46 |
-
|
| 47 |
-
template = """You are a neuroscientist who is answering questions about the worm C. elegans. Provide succinct, yet scientifically accurate
|
| 48 |
-
answers. If the question is not related to biology, physics or chemistry, then state that you
|
| 49 |
-
can currently only answer questions related to C. elegans. Question: {question}
|
| 50 |
-
|
| 51 |
-
Answer: """
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
prompt = PromptTemplate(
|
| 55 |
-
template=template,
|
| 56 |
-
input_variables=['question']
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
if llm_ver==LLM_GPT35:
|
| 60 |
-
llm = OpenAI(temperature=temperature, openai_api_key=get_openai_api_key())
|
| 61 |
-
|
| 62 |
-
elif llm_ver==LLM_GPT4:
|
| 63 |
-
from langchain_openai import ChatOpenAI
|
| 64 |
-
llm = ChatOpenAI(model_name='gpt-4', openai_api_key=get_openai_api_key(), temperature=temperature)
|
| 65 |
-
|
| 66 |
-
elif llm_ver==LLM_LLAMA2:
|
| 67 |
-
from llamaapi import LlamaAPI
|
| 68 |
-
import asyncio
|
| 69 |
-
|
| 70 |
-
# Create a new event loop
|
| 71 |
-
loop = asyncio.new_event_loop()
|
| 72 |
-
|
| 73 |
-
# Set the event loop as the current event loop
|
| 74 |
-
asyncio.set_event_loop(loop)
|
| 75 |
-
|
| 76 |
-
llama = LlamaAPI(get_llamaapi_key())
|
| 77 |
-
|
| 78 |
-
from langchain_experimental.llms import ChatLlamaAPI
|
| 79 |
-
|
| 80 |
-
llm = ChatLlamaAPI(client=llama)
|
| 81 |
-
|
| 82 |
-
llm_chain = LLMChain(
|
| 83 |
-
prompt=prompt,
|
| 84 |
-
llm=llm
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
response = llm_chain.invoke(input_text)['text']
|
| 88 |
-
|
| 89 |
-
st.info(response)
|
| 90 |
|
| 91 |
|
| 92 |
with st.form("my_form"):
|
| 93 |
-
text = st.text_area("Enter text:", "What is the primary role of the C. elegans neuron
|
| 94 |
-
llm_ver = st.selectbox('Which LLM version?', (LLM_GPT35, LLM_GPT4, LLM_LLAMA2))
|
| 95 |
|
| 96 |
temperature = st.text_input("Temperature", value=0.1)
|
| 97 |
|
| 98 |
submitted = st.form_submit_button("Submit")
|
| 99 |
|
| 100 |
-
if not get_openai_api_key():
|
| 101 |
st.info("Please add your OpenAI API key to continue.")
|
| 102 |
elif submitted:
|
| 103 |
-
generate_response(text, llm_ver, temperature)
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
|
| 4 |
+
from ring import LLM_GPT35
|
| 5 |
+
from ring import LLM_GPT4
|
| 6 |
+
from ring import LLM_LLAMA2
|
| 7 |
|
| 8 |
+
from ring import requires_openai_key
|
| 9 |
+
from ring import get_openai_api_key
|
| 10 |
+
from ring import generate_response
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
__version__ = '0.1.4'
|
| 14 |
+
|
|
|
|
| 15 |
|
| 16 |
st.title("Project Sydney")
|
| 17 |
st.text("OpenWorm LLM v%s - work in progress!"%__version__)
|
| 18 |
|
| 19 |
openai_api_key = None
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
with st.form("my_form"):
|
| 24 |
+
text = st.text_area("Enter text:", "What is the primary role of the C. elegans neuron AVBL?")
|
| 25 |
+
llm_ver = st.selectbox('Which LLM version should I use?', (LLM_GPT35, LLM_GPT4, LLM_LLAMA2))
|
| 26 |
|
| 27 |
temperature = st.text_input("Temperature", value=0.1)
|
| 28 |
|
| 29 |
submitted = st.form_submit_button("Submit")
|
| 30 |
|
| 31 |
+
if requires_openai_key(llm_ver) and not get_openai_api_key():
|
| 32 |
st.info("Please add your OpenAI API key to continue.")
|
| 33 |
elif submitted:
|
| 34 |
+
response = generate_response(text, llm_ver, temperature)
|
| 35 |
+
|
| 36 |
+
st.info(response)
|
ring.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from langchain_community.llms import OpenAI
|
| 5 |
+
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
from langchain.chains import LLMChain
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
LLM_GPT35 = 'GPT3.5'
|
| 11 |
+
LLM_GPT4 = 'GPT4'
|
| 12 |
+
LLM_LLAMA2 = 'LLAMA2'
|
| 13 |
+
|
| 14 |
+
OPENAI_LLMS = [LLM_GPT35, LLM_GPT4]
|
| 15 |
+
|
| 16 |
+
def requires_openai_key(llm_ver):
|
| 17 |
+
|
| 18 |
+
return llm_ver in OPENAI_LLMS
|
| 19 |
+
|
| 20 |
+
def get_openai_api_key():
|
| 21 |
+
|
| 22 |
+
#if openai_api_key_sb == None or len(openai_api_key_sb)==0:
|
| 23 |
+
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
| 24 |
+
if openai_api_key==None:
|
| 25 |
+
openai_api_key = str(open('../oaik','r').readline())
|
| 26 |
+
#else:
|
| 27 |
+
# openai_api_key = openai_api_key_sb
|
| 28 |
+
|
| 29 |
+
return openai_api_key
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_llamaapi_key():
|
| 33 |
+
|
| 34 |
+
llamaapi_key = os.environ.get('LLAMAAPI_KEY')
|
| 35 |
+
|
| 36 |
+
return llamaapi_key
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def generate_response(input_text, llm_ver, temperature):
|
| 40 |
+
|
| 41 |
+
template = """You are a neuroscientist who is answering questions about the worm C. elegans. Provide succinct, yet scientifically accurate
|
| 42 |
+
answers. If the question is not related to biology, physics or chemistry, then state that you
|
| 43 |
+
can currently only answer questions related to C. elegans. Question: {question}
|
| 44 |
+
|
| 45 |
+
Answer: """
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
prompt = PromptTemplate(
|
| 49 |
+
template=template,
|
| 50 |
+
input_variables=['question']
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if llm_ver==LLM_GPT35:
|
| 54 |
+
llm = OpenAI(temperature=temperature, openai_api_key=get_openai_api_key())
|
| 55 |
+
|
| 56 |
+
elif llm_ver==LLM_GPT4:
|
| 57 |
+
from langchain_openai import ChatOpenAI
|
| 58 |
+
llm = ChatOpenAI(model_name='gpt-4', openai_api_key=get_openai_api_key(), temperature=temperature)
|
| 59 |
+
|
| 60 |
+
elif llm_ver==LLM_LLAMA2:
|
| 61 |
+
from llamaapi import LlamaAPI
|
| 62 |
+
import asyncio
|
| 63 |
+
|
| 64 |
+
# Create a new event loop
|
| 65 |
+
loop = asyncio.new_event_loop()
|
| 66 |
+
|
| 67 |
+
# Set the event loop as the current event loop
|
| 68 |
+
asyncio.set_event_loop(loop)
|
| 69 |
+
|
| 70 |
+
llama = LlamaAPI(get_llamaapi_key())
|
| 71 |
+
|
| 72 |
+
from langchain_experimental.llms import ChatLlamaAPI
|
| 73 |
+
|
| 74 |
+
llm = ChatLlamaAPI(client=llama)
|
| 75 |
+
|
| 76 |
+
llm_chain = LLMChain(
|
| 77 |
+
prompt=prompt,
|
| 78 |
+
llm=llm
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
response = llm_chain.invoke(input_text)['text']
|
| 82 |
+
|
| 83 |
+
return response
|